LaTeX macros#


  • John H. Palmieri (2009-03)

The code here sets up LaTeX macro definitions for use in the documentation. To add a macro, modify the list macros, near the end of this file, and then run ‘sage -b’. The entries in this list are used to produce sage_latex_macros, a list of strings of the form ‘\newcommand…’, and sage_mathjax_macros, a list of strings suitable for parsing by MathJax. The LaTeX macros are produced using the _latex_ method for each Sage object listed in macros, and the MathJax macros are produced from the LaTeX macros. The list of LaTeX macros is used in the file sage_docbuild.conf to add to the preambles of both the LaTeX file used to build the PDF version of the documentation and the LaTeX file used to build the HTML version.

Any macro defined here may be used in docstrings or in the tutorial (or other pieces of documentation). In a docstring, for example, “ZZ” in backquotes (demarking math mode) will appear as “ZZ” in interactive help, but will be typeset as “\Bold{Z}” in the reference manual.

More details on the list macros: the entries are lists or tuples of the form [name] or [name, arguments], where name is a string and arguments consists of valid arguments for the Sage object named name. For example, ["ZZ"] and ["GF", 2] produce the LaTeX macros ‘\newcommand{\ZZ}{\Bold{Z}}’ and ‘\newcommand{\GF}[1]{\Bold{F}_{#1}}’, respectively. (For the second of these, latex(GF(2)) is called and the string ‘2’ gets replaced by ‘#1’, so ["GF", 17] would have worked just as well. ["GF", p] would have raised an error, though, because p is not defined, and ["GF", 4] would have raised an error, because to define the field with four elements in Sage, you also need to specify the name of a generator.)

To see evidence of the results of the code here, run sage --docbuild tutorial latex (for example), and look at the resulting LaTeX file in SAGE_DOC/latex/en/tutorial/. The preamble should contain ‘newcommand’ lines for each of the entries in macros.


This converts a LaTeX macro definition (newcommand…) to a MathJax macro definition (MathJax.Macro…).


  • macro – LaTeX macro definition

See the web page for a description of the format for MathJax macros.


sage: from sage.misc.latex_macros import convert_latex_macro_to_mathjax
sage: convert_latex_macro_to_mathjax('\\newcommand{\\ZZ}{\\Bold{Z}}')
('ZZ', '\\Bold{Z}')
sage: convert_latex_macro_to_mathjax('\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}')
('GF', ['\\Bold{F}_{#1}', 1])
sage.misc.latex_macros.produce_latex_macro(name, *sample_args)#

Produce a string defining a LaTeX macro.


  • name – name of macro to be defined, also name of corresponding Sage object

  • sample_args – (optional) sample arguments for this Sage object


sage: from sage.misc.latex_macros import produce_latex_macro
sage: produce_latex_macro('ZZ')

If the Sage object takes arguments, then the LaTeX macro will accept arguments as well. You must pass valid arguments, which will then be converted to #1, #2, etc. in the macro definition. The following allows the use of “GF{p^n}”, for example:

sage: produce_latex_macro('GF', 37)

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

sage: produce_latex_macro('sage.rings.finite_rings.finite_field_constructor.FiniteField', 3)

Return list of LaTeX macros for Sage. This just runs the function produce_latex_macro() on the list macros defined in this file, and appends sage_configurable_latex_macros. To add a new macro for permanent use in Sage, modify macros.


sage: from sage.misc.latex_macros import sage_latex_macros
sage: sage_latex_macros()
['\\newcommand{\\ZZ}{\\Bold{Z}}', '\\newcommand{\\NN}{\\Bold{N}}', ...

Return Sage’s macro definitions for usage with MathJax.

This feeds each item output by sage_latex_macros() to convert_latex_macro_to_mathjax().


sage: from sage.misc.latex_macros import sage_mathjax_macros
sage: sage_mathjax_macros()
{'Bold': ['\\mathbf{#1}', 1], 'CC': '\\Bold{C}', ...