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 Dec 23, 2024
2 parents db7fe4d + 4c83331 commit d37ab39
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tarball=configure-VERSION.tar.gz
sha1=a03b8a505678cba0d652514d739bd32eb30bb925
sha256=6525b44fea6b9d0238ca4790e8be5168e8d08c350787704a59ada9b6075a1f0f
sha1=36b1c4a6f0660389b7bff12a39892442f8e42c05
sha256=533fcd20eecaaffa05d16198103114882a010283eff01fa79cb723cad3de7633
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
291e54bf234b1753909f22a043e91a4a639693c1
cb8b29303e0a2dd95a49eaa273ed74e7feba7c4c
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 d37ab39

Please sign in to comment.