Skip to content

Commit

Permalink
src/sage/misc/latex.py: invoke default_engine() as late as possible
Browse files Browse the repository at this point in the history
When we try to find the current LaTeX engine, the preferences dict
will now have __options["engine"] == None to indicate that we should
fall back to the default engine. In each case we check for None, and
invoke default_engine() only if necessary.

This allows us to query the options dict without triggering the
expensive is_functional() method for the LaTeX features.
  • Loading branch information
orlitzky committed Feb 2, 2025
1 parent ff24ef5 commit 3b30304
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/sage/misc/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ def __init__(self, bb=False, delimiters=["(", ")"],
self.__option["matrix_column_alignment"] = matrix_column_alignment
self.__option["macros"] = ""
self.__option["preamble"] = ""

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

@lazy_attribute
Expand Down Expand Up @@ -648,6 +650,8 @@ def _run_latex_(filename, debug=False, density=150, engine=None, png=False, do_i
"""
if engine is None:
engine = _Latex_prefs._option["engine"]
if engine is None:
engine = default_engine()

if not engine or engine == "latex":
from sage.features.latex import latex
Expand Down Expand Up @@ -1061,10 +1065,12 @@ def eval(self, x, globals, strip=False, filename=None, debug=None,

O.close()
if engine is None:
if self.__engine is None:
engine = self.__engine
if engine is None:
engine = _Latex_prefs._option["engine"]
else:
engine = self.__engine
if engine is None:
engine = default_engine()

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

if e == "latex":
_Latex_prefs._option["engine"] = "latex"
elif e == "pdflatex":
_Latex_prefs._option["engine"] = "pdflatex"
elif e == "xelatex":
_Latex_prefs._option["engine"] = e
elif e == "lualatex":
_Latex_prefs._option["engine"] = e
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 @@ -1841,6 +1845,9 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
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()

if viewer == "pdf" and engine == "latex":
engine = "pdflatex"
# command line or notebook with viewer
Expand Down Expand Up @@ -1942,6 +1949,9 @@ def pdf(x, filename, tiny=False, tightpage=True, margin=None, engine=None, debug
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()

# path name for permanent pdf output
abs_path_to_pdf = os.path.abspath(filename)
# temporary directory to store stuff
Expand Down Expand Up @@ -2002,6 +2012,9 @@ def png(x, filename, density=150, debug=False,
extra_preamble='\\textheight=2\\textheight')
if engine is None:
engine = _Latex_prefs._option["engine"]
if engine is None:
engine = default_engine()

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

0 comments on commit 3b30304

Please sign in to comment.