Skip to content

Commit

Permalink
sagemathgh-39184: Refactor produce_latex_macro
Browse files Browse the repository at this point in the history
    
Alternative to sagemath#39181 .

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes. (no behavior change)
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39184
Reported by: user202729
Reviewer(s):
  • Loading branch information
Release Manager committed Jan 6, 2025
2 parents 4a61fa8 + f3208ef commit e7d4f4d
Showing 1 changed file with 12 additions and 16 deletions.
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

0 comments on commit e7d4f4d

Please sign in to comment.