Ticket #13504: trac_13504-latex-macros.patch

File trac_13504-latex-macros.patch, 9.3 KB (added by jhpalmieri, 9 years ago)
  • doc/common/conf.py

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1347918127 25200
    # Node ID 235ef84eee81ce82dd493023fbf97389bbd92280
    # Parent  f8a5c21307e650fcf880fa871021e0d1df3aa584
    latex_macros: don't evaluate anything of substance on startup.
    
    diff --git a/doc/common/conf.py b/doc/common/conf.py
    a b if (os.environ.get('SAGE_DOC_MATHJAX', F 
    176176    mathjax_path = 'MathJax.js?config=TeX-AMS_HTML-full,../mathjax_sage.js'
    177177
    178178    from sage.misc.latex_macros import sage_mathjax_macros
    179     html_theme_options['mathjax_macros'] = sage_mathjax_macros
     179    html_theme_options['mathjax_macros'] = sage_mathjax_macros()
    180180
    181181    from pkg_resources import Requirement, working_set
    182182    sagenb_path = working_set.find(Requirement.parse('sagenb')).location
    try: 
    288288except NameError:
    289289    pngmath_latex_preamble = ""
    290290
    291 for macro in sage_latex_macros:
     291for macro in sage_latex_macros():
    292292    # used when building latex and pdf versions
    293293    latex_elements['preamble'] += macro + '\n'
    294294    # used when building html version
  • sage/misc/latex.py

    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b def latex_extra_preamble(): 
    625625    """
    626626    from sage.misc.latex_macros import sage_latex_macros
    627627    return "\n".join([_Latex_prefs._option['preamble'],
    628                      "\n".join(sage_latex_macros),
     628                     "\n".join(sage_latex_macros()),
    629629                     _Latex_prefs._option['macros']])
    630630
    631631def _run_latex_(filename, debug=False, density=150, engine=None, png=False, do_in_background=False):
    def _run_latex_(filename, debug=False, d 
    852852        return "Error latexing slide."
    853853    return return_suffix
    854854
    855 class Latex:
    856     r"""nodetex
    857     Enter, e.g.,
    858 
    859     ::
    860 
    861         %latex
    862         The equation $y^2 = x^3 + x$ defines an elliptic curve.
    863         We have $2006 = \sage{factor(2006)}$.
    864 
    865     in an input cell in the notebook to get a typeset version. Use
    866     ``%latex_debug`` to get debugging output.
    867 
    868     Use ``latex(...)`` to typeset a Sage object.  Use :class:`LatexExpr`
    869     to typeset LaTeX code that you create by hand.
    870 
    871     Use ``%slide`` instead to typeset slides.
    872 
    873     .. warning::
    874 
    875        You must have dvipng (or dvips and convert) installed
    876        on your operating system, or this command won't work.
     855class LatexCall:
     856    """
     857    Typeset Sage objects via a ``__call__`` method to this class,
     858    typically by calling those objects' ``_latex_`` methods.  The
     859    class :class:`Latex` inherits from this. This class is used in
     860    :mod:`~sage.misc.latex_macros`, while functions from
     861    :mod:`~sage.misc.latex_macros` are used in :class:`Latex`, so this
     862    is here primarily to avoid circular imports.
    877863
    878864    EXAMPLES::
    879865
    880         sage: latex(x^20 + 1)
    881         x^{20} + 1
    882         sage: latex(FiniteField(25,'a'))
    883         \Bold{F}_{5^{2}}
    884         sage: latex("hello")
    885         \verb|hello|
    886         sage: LatexExpr(r"\frac{x^2 - 1}{x + 1} = x - 1")
    887         \frac{x^2 - 1}{x + 1} = x - 1
    888 
    889     LaTeX expressions can be added; note that a space is automatically
    890     inserted::
    891 
    892         sage: LatexExpr(r"y \neq") + latex(x^20 + 1)
    893         y \neq x^{20} + 1
     866        sage: from sage.misc.latex import LatexCall
     867        sage: LatexCall()(ZZ)
     868        \Bold{Z}
     869        sage: LatexCall().__call__(ZZ)
     870        \Bold{Z}
     871
     872    This returns an instance of the class :class:`LatexExpr`::
     873
     874        sage: type(LatexCall()(ZZ))
     875        <class 'sage.misc.latex.LatexExpr'>
    894876    """
    895     def __init__(self, debug=False, slide=False, density=150, pdflatex=None, engine=None):
    896         self.__debug = debug
    897         self.__slide = slide
    898         self.__pdflatex = pdflatex
    899         self.__engine = engine
    900         self.__density = density
    901 
    902877    def __call__(self, x, combine_all=False):
    903878        r"""
    904879        Return a :class:`LatexExpr` built out of the argument ``x``.
    class Latex: 
    935910        except KeyError:
    936911            return LatexExpr(str_function(str(x)))
    937912
     913
     914class Latex(LatexCall):
     915    r"""nodetex
     916    Enter, e.g.,
     917
     918    ::
     919
     920        %latex
     921        The equation $y^2 = x^3 + x$ defines an elliptic curve.
     922        We have $2006 = \sage{factor(2006)}$.
     923
     924    in an input cell in the notebook to get a typeset version. Use
     925    ``%latex_debug`` to get debugging output.
     926
     927    Use ``latex(...)`` to typeset a Sage object.  Use :class:`LatexExpr`
     928    to typeset LaTeX code that you create by hand.
     929
     930    Use ``%slide`` instead to typeset slides.
     931
     932    .. warning::
     933
     934       You must have dvipng (or dvips and convert) installed
     935       on your operating system, or this command won't work.
     936
     937    EXAMPLES::
     938
     939        sage: latex(x^20 + 1)
     940        x^{20} + 1
     941        sage: latex(FiniteField(25,'a'))
     942        \Bold{F}_{5^{2}}
     943        sage: latex("hello")
     944        \verb|hello|
     945        sage: LatexExpr(r"\frac{x^2 - 1}{x + 1} = x - 1")
     946        \frac{x^2 - 1}{x + 1} = x - 1
     947
     948    LaTeX expressions can be added; note that a space is automatically
     949    inserted::
     950
     951        sage: LatexExpr(r"y \neq") + latex(x^20 + 1)
     952        y \neq x^{20} + 1
     953    """
     954    def __init__(self, debug=False, slide=False, density=150, pdflatex=None, engine=None):
     955        self.__debug = debug
     956        self.__slide = slide
     957        self.__pdflatex = pdflatex
     958        self.__engine = engine
     959        self.__density = density
     960
    938961    def _relation_symbols(self):
    939962        """
    940963        Returns a dictionary whose keys are attributes of the
    class Latex: 
    10871110        """
    10881111        if t is None:
    10891112            return _Latex_prefs._option["blackboard_bold"]
    1090         from latex_macros import sage_latex_macros, sage_mathjax_macros, sage_configurable_latex_macros, convert_latex_macro_to_mathjax
    1091         global sage_latex_macros
    1092         global sage_mathjax_macros
     1113        from latex_macros import sage_configurable_latex_macros
     1114        global sage_configurable_latex_macros
    10931115        old = _Latex_prefs._option["blackboard_bold"]
    10941116        _Latex_prefs._option["blackboard_bold"] = bool(t)
    10951117        if bool(old) != bool(t):
    class Latex: 
    11011123                macro = "\\newcommand{\\Bold}[1]{\\mathbb{#1}}"
    11021124            else:
    11031125                macro = "\\newcommand{\\Bold}[1]{\\mathbf{#1}}"
    1104             sage_latex_macros.remove(old_macro)
    11051126            sage_configurable_latex_macros.remove(old_macro)
    1106             sage_latex_macros.append(macro)
    11071127            sage_configurable_latex_macros.append(macro)
    1108             sage_mathjax_macros.remove(convert_latex_macro_to_mathjax(old_macro))
    1109             sage_mathjax_macros.append(convert_latex_macro_to_mathjax(macro))
    11101128
    11111129    def matrix_delimiters(self, left=None, right=None):
    11121130        r"""nodetex
  • sage/misc/latex_macros.py

    diff --git a/sage/misc/latex_macros.py b/sage/misc/latex_macros.py
    a b def produce_latex_macro(name, *sample_ar 
    7676         sage: produce_latex_macro('sage.rings.finite_rings.constructor.FiniteField', 3)
    7777         '\\newcommand{\\FiniteField}[1]{\\Bold{F}_{#1}}'
    7878    """
    79     from sage.misc.latex import latex
     79    from sage.misc.latex import LatexCall
    8080    names_split = name.rsplit('.', 1)
    8181    if len(names_split) == 1:
    8282        module = 'sage.all'
    def produce_latex_macro(name, *sample_ar 
    9393    exec('from ' + module + ' import ' + real_name)
    9494    if count > 0:
    9595        defn = '[' + str(count) + ']{'
    96         defn += eval('str(latex(' + real_name + args + '))') + '}'
     96        defn += eval('str(LatexCall()(' + real_name + args + '))') + '}'
    9797    else:
    98         defn = '{' + eval('str(latex(' + real_name + '))') + '}'
     98        defn = '{' + eval('str(LatexCall()(' + real_name + '))') + '}'
    9999    count = 0
    100100    for x in sample_args:
    101101        count += 1
    macros = [["ZZ"], 
    170170          ["CFF"],
    171171          ]
    172172
    173 sage_latex_macros = [produce_latex_macro(*x) for x in macros]
    174173# The following is to allow customization of typesetting of rings:
    175174# mathbf vs mathbb.  See latex.py for more information.
    176175sage_configurable_latex_macros = ["\\newcommand{\\Bold}[1]{\\mathbf{#1}}"]
    177176
    178 sage_latex_macros += sage_configurable_latex_macros
     177def sage_latex_macros():
     178    r"""
     179    Return list of LaTeX macros for Sage. This just runs the function
     180    :func:`produce_latex_macro` on the list ``macros`` defined in this
     181    file, and appends ``sage_configurable_latex_macros``. To add a new
     182    macro for permanent use in Sage, modify ``macros``.
    179183
    180 # MathJax macro definitions as JavaScript, e.g., to include in HTML
    181 # script elements.
    182 sage_mathjax_macros = [convert_latex_macro_to_mathjax(m) for m in sage_latex_macros]
     184    EXAMPLES::
     185
     186        sage: from sage.misc.latex_macros import sage_latex_macros
     187        sage: sage_latex_macros()
     188        ['\\newcommand{\\ZZ}{\\Bold{Z}}', '\\newcommand{\\NN}{\\Bold{N}}', ...
     189    """
     190    return [produce_latex_macro(*x) for x in macros] + sage_configurable_latex_macros
     191
     192def sage_mathjax_macros():
     193    r"""
     194    Return list of MathJax macro definitions for Sage as
     195    JavaScript. This feeds each item output by
     196    :func:`sage_latex_macros` to
     197    :func:`convert_latex_macro_to_mathjax`.
     198
     199    EXAMPLES::
     200
     201        sage: from sage.misc.latex_macros import sage_mathjax_macros
     202        sage: sage_mathjax_macros()
     203        ['ZZ: "\\\\Bold{Z}"', 'NN: "\\\\Bold{N}"', ...
     204    """
     205    return [convert_latex_macro_to_mathjax(m) for m in sage_latex_macros()]