Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay LaTeX feature tests as long as possible #39430

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
116 changes: 86 additions & 30 deletions src/sage/misc/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,22 @@
def default_engine():
"""
Return the default latex engine and the official name of the engine.

This is determined by availability of the popular engines on the user's
system. It is assumed that at least latex is available.

This function is deprecated as part of the public API. There is
instead an internal counterpart :func:`_default_engine`, but no
stability promises are made with regards to its interface.

EXAMPLES::

sage: from sage.misc.latex import default_engine
sage: default_engine() # random
('lualatex', 'LuaLaTeX')
"""
from sage.misc.superseded import deprecation
deprecation(39351, "default_engine is being removed from the public API and replaced with the internal function _default_engine")

from sage.features.latex import pdflatex, xelatex, lualatex
if lualatex().is_present():
return 'lualatex', 'LuaLaTeX'
Expand All @@ -497,6 +503,48 @@
return 'latex', 'LaTeX'


@cached_function
def _default_engine():
r"""
Return the name of the default latex engine.

This is determined by availability of the popular engines on the
user's system. It is assumed that at least "latex" is available.

EXAMPLES::

sage: from sage.misc.latex import _default_engine
sage: _default_engine() # random
'lualatex'

TESTS:

Ensure that this (expensive) function is not necessary to obtain
the latex representation of a matrix (doing so probes the latex
options dict for the delimiters)::

sage: import sage.misc.latex
sage: real_de = sage.misc.latex._default_engine
sage: def crash():
....: raise ValueError
sage: sage.misc.latex._default_engine = crash
sage: latex(matrix.identity(QQ, 2))
\left(\begin{array}{rr}
1 & 0 \\
0 & 1
\end{array}\right)
sage: sage.misc.latex._default_engine = real_de
"""
kwankyu marked this conversation as resolved.
Show resolved Hide resolved
from sage.features.latex import pdflatex, xelatex, lualatex
if lualatex().is_present():
return 'lualatex'

Check warning on line 540 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L540

Added line #L540 was not covered by tests
orlitzky marked this conversation as resolved.
Show resolved Hide resolved
if xelatex().is_present():
return 'xelatex'

Check warning on line 542 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L542

Added line #L542 was not covered by tests
if pdflatex().is_present():
return 'pdflatex'

Check warning on line 544 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L544

Added line #L544 was not covered by tests
return 'latex'


class _Latex_prefs_object(SageObject):
"""
An object that holds LaTeX global preferences.
Expand All @@ -520,6 +568,9 @@
self.__option["macros"] = ""
self.__option["preamble"] = ""

# If None, the _default_engine() will be used.
self.__option["engine"] = None

@lazy_attribute
def _option(self):
"""
Expand All @@ -528,18 +579,16 @@
EXAMPLES::

sage: from sage.misc.latex import _Latex_prefs_object
sage: _Latex_prefs_object()._option # random
{'blackboard_bold': False,
'matrix_delimiters': ['(', ')'],
'vector_delimiters': ['(', ')'],
'matrix_column_alignment': 'r',
'macros': '',
'preamble': '',
'engine': 'lualatex',
'engine_name': 'LuaLaTeX'}
sage: sorted(_Latex_prefs_object()._option.items())
[('blackboard_bold', False),
('engine', None),
('macros', ''),
('matrix_column_alignment', 'r'),
('matrix_delimiters', ['(', ')']),
('preamble', ''),
('vector_delimiters', ['(', ')'])]

"""
self.__option["engine"] = default_engine()[0]
self.__option["engine_name"] = default_engine()[1]
return self.__option


Expand Down Expand Up @@ -649,6 +698,8 @@
"""
if engine is None:
engine = _Latex_prefs._option["engine"]
if engine is None:
engine = _default_engine()

Check warning on line 702 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L701-L702

Added lines #L701 - L702 were not covered by tests

if not engine or engine == "latex":
from sage.features.latex import latex
Expand Down Expand Up @@ -1062,10 +1113,12 @@

O.close()
if engine is None:
if self.__engine is None:
engine = self.__engine
if engine is None:

Check warning on line 1117 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1116-L1117

Added lines #L1116 - L1117 were not covered by tests
engine = _Latex_prefs._option["engine"]
else:
engine = self.__engine
if engine is None:
engine = _default_engine()

Check warning on line 1120 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1119-L1120

Added lines #L1119 - L1120 were not covered by tests

e = _run_latex_(os.path.join(base, filename + ".tex"),
debug=debug,
density=density,
Expand Down Expand Up @@ -1531,23 +1584,17 @@
'pdflatex'
"""
if e is None:
return _Latex_prefs._option["engine"]

if e == "latex":
_Latex_prefs._option["engine"] = "latex"
_Latex_prefs._option["engine_name"] = "LaTeX"
elif e == "pdflatex":
_Latex_prefs._option["engine"] = "pdflatex"
_Latex_prefs._option["engine_name"] = "PDFLaTeX"
elif e == "xelatex":
_Latex_prefs._option["engine"] = e
_Latex_prefs._option["engine_name"] = "XeLaTeX"
elif e == "lualatex":
_Latex_prefs._option["engine"] = e
_Latex_prefs._option["engine_name"] = "LuaLaTeX"
else:
e = _Latex_prefs._option["engine"]
if e is None:
return _default_engine()
else:
return e

if e not in ["latex", "pdflatex", "xelatex", "luatex"]:
raise ValueError("%s is not a supported LaTeX engine. Use latex, pdflatex, xelatex, or lualatex" % e)

_Latex_prefs._option["engine"] = e


# Note: latex used to be a separate function, which by default was
# only loaded in command-line mode: in the old notebook,
Expand Down Expand Up @@ -1846,6 +1893,9 @@
s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options)
if engine is None:
engine = _Latex_prefs._option["engine"]
if engine is None:
engine = _default_engine()

Check warning on line 1897 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1896-L1897

Added lines #L1896 - L1897 were not covered by tests

if viewer == "pdf" and engine == "latex":
engine = "pdflatex"
# command line or notebook with viewer
Expand Down Expand Up @@ -1947,6 +1997,9 @@
s = _latex_file_([x], title='', tiny=tiny, debug=debug, **latex_options)
if engine is None:
engine = _Latex_prefs._option["engine"]
if engine is None:
engine = _default_engine()

Check warning on line 2001 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L2000-L2001

Added lines #L2000 - L2001 were not covered by tests

# path name for permanent pdf output
abs_path_to_pdf = os.path.abspath(filename)
# temporary directory to store stuff
Expand Down Expand Up @@ -2007,6 +2060,9 @@
extra_preamble='\\textheight=2\\textheight')
if engine is None:
engine = _Latex_prefs._option["engine"]
if engine is None:
engine = _default_engine()

Check warning on line 2064 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L2063-L2064

Added lines #L2063 - L2064 were not covered by tests

# path name for permanent png output
abs_path_to_png = os.path.abspath(filename)
# temporary directory to store stuff
Expand Down
Loading