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

Refactor produce_latex_macro #39184

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/sage/misc/latex_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
contain '\newcommand' lines for each of the entries in ``macros``.
"""

import importlib


def produce_latex_macro(name, *sample_args):
r"""
Expand All @@ -69,7 +71,7 @@ def produce_latex_macro(name, *sample_args):
sage: produce_latex_macro('GF', 37)
'\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}'

If the Sage object is not in the global name space, describe it
If the Sage object is not in the global namespace, describe it
like so::

sage: produce_latex_macro('sage.rings.finite_rings.finite_field_constructor.FiniteField', 3)
Expand All @@ -84,22 +86,16 @@ def produce_latex_macro(name, *sample_args):
else:
module, real_name = names_split
newcommand = '\\newcommand{\\' + real_name + '}'
count = 0
args = "("
for x in sample_args:
count += 1
args += str(x) + ','
args += ')'
exec('from ' + module + ' import ' + real_name)
if count:
defn = '[' + str(count) + ']{'
defn += eval('str(LatexCall()(' + real_name + args + '))') + '}'
sage_object = getattr(importlib.import_module(module), real_name)
if sample_args:
defn = '[' + str(len(sample_args)) + ']{'
defn += str(LatexCall()(sage_object(*sample_args))) + '}'
else:
defn = '{' + eval('str(LatexCall()(' + real_name + '))') + '}'
count = 0
for x in sample_args:
count += 1
defn = defn.replace(str(x), "#" + str(count))
defn = '{' + str(LatexCall()(sage_object)) + '}'
for i, x in enumerate(sample_args):
s = str(x)
assert s in defn
defn = defn.replace(s, "#" + str(i+1))
return newcommand + defn


Expand Down
Loading