Ticket #12156: 12156_latex_pretty_print.patch

File 12156_latex_pretty_print.patch, 5.5 KB (added by jdemeyer, 10 years ago)
  • sage/misc/all.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1323942049 -3600
    # Node ID fd045541a56c3a6a9b0ca5193a59dd15af6169bb
    # Parent  b72663c760492cc8c360d328a6d370c8f88d2069
    Pretty print LatexExpr directly, import LatexExpr globally
    
    diff --git a/sage/misc/all.py b/sage/misc/all.py
    a b  
    144144                        parent)
    145145
    146146
    147 from latex import latex, view, pretty_print, pretty_print_default
     147from latex import LatexExpr, latex, view, pretty_print, pretty_print_default
    148148 
    149 # disabled -- nobody uses mathml
    150 #from mathml ml
    151 
    152149from trace import trace
    153150
    154151from constant_function import ConstantFunction
  • sage/misc/latex.py

    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b  
    77"""
    88
    99#*****************************************************************************
    10 #
    11 #   Sage
    12 #
    1310#       Copyright (C) 2005 William Stein <wstein@gmail.com>
    1411#
    1512#  Distributed under the terms of the GNU General Public License (GPL)
    16 #
    17 #    This code is distributed in the hope that it will be useful,
    18 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
    19 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    20 #    General Public License for more details.
    21 #
    22 #  The full text of the GPL is available at:
    23 #
     13#  as published by the Free Software Foundation; either version 2 of
     14#  the License, or (at your option) any later version.
    2415#                  http://www.gnu.org/licenses/
    2516#*****************************************************************************
    2617
     18
    2719EMBEDDED_MODE = False
    2820
    2921COMMON_HEADER = \
     
    380372
    381373class LatexExpr(str):
    382374    """
    383     LaTeX expression.  This is a string.
     375    A class for LaTeX expressions, i.e. the result of a :func:`latex`
     376    call.  A ``LatexExpr`` can also be generated directly from a string.
     377    This string is then pretty-printed (see :func:`pretty_print`) as-is.
    384378
    385379    EXAMPLES::
    386380
    387         sage: from sage.misc.latex import LatexExpr
    388         sage: LatexExpr(3)
    389         3
    390         sage: LatexExpr(False)
    391         False
    392         sage: LatexExpr(pi)
    393         pi
    394         sage: LatexExpr(3.14)
    395         3.14000000000000
    396         sage: LatexExpr("abc")
    397         abc
     381        sage: latex(x^20 + 1)
     382        x^{20} + 1
     383        sage: LatexExpr(r"\frac{x^2 + 1}{x - 2}")
     384        \frac{x^2 + 1}{x - 2}
     385
     386    ``LatexExpr`` simply converts to string without doing anything
     387    extra, it does *not* call :func:`latex`::
     388
     389        sage: latex(ZZ)
     390        \Bold{Z}
     391        sage: LatexExpr(ZZ)
     392        Integer Ring
     393
     394    The result of :func:`latex` is of type ``LatexExpr``::
     395
     396        sage: L = latex(x^20 + 1)
     397        sage: L
     398        x^{20} + 1
     399        sage: type(L)
     400        <class 'sage.misc.latex.LatexExpr'>
    398401    """
    399402    def __repr__(self):
    400403        """
    401404        EXAMPLES::
    402405
    403             sage: from sage.misc.latex import LatexExpr
    404406            sage: LatexExpr("abc").__repr__()
    405407            'abc'
    406408        """
     
    410412        """
    411413        EXAMPLES::
    412414
    413             sage: from sage.misc.latex import LatexExpr
    414             sage: LatexExpr("abc")._latex_()
    415             'abc'
     415            sage: latex(LatexExpr("abc"))
     416            abc
    416417        """
    417418        return str(self)
    418419
     
    16791680            sage: JSMath().eval(type(3), mode='inline')
    16801681            <html>...\verb|&lt;type|\phantom{x}\verb|'sage.rings.integer.Integer'&gt;|</span></html>
    16811682        """
    1682         # If x is already a LaTeX expression, i.e. the output of latex(blah),
    1683         # we will treat it as a string, so that we can see the code itself.
    1684         if isinstance(x, LatexExpr):
    1685             x = str(x)
    16861683        # Now get a regular LaTeX representation of x...
    16871684        x = latex(x)
    16881685        # ... and make it suitable for jsMath, which has issues with < and >.
     
    18751872        'pdf'
    18761873
    18771874    """
    1878     if isinstance(objects, LatexExpr):
    1879         s = str(objects)
     1875    if tightpage == True:
     1876        latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}',
     1877                         'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'}
    18801878    else:
    1881         if tightpage == True:
    1882             latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}',
    1883                              'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'}
    1884         else:
    1885             latex_options = {}
    1886         s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options)
     1879        latex_options = {}
     1880    s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options)
    18871881    # notebook
    18881882    if EMBEDDED_MODE and viewer is None:
    18891883        jsMath_okay = True
     
    21302124
    21312125    INPUT:
    21322126
    2133     - ``object`` - a Sage object
    2134 
    2135     This function is used in the notebook when the ``Typeset`` button is
     2127    - ``object`` -- a Sage object
     2128
     2129    This function is used in the notebook when the "Typeset" button is
    21362130    checked.
    21372131
    21382132    EXAMPLES::
    21392133
    2140         sage: from sage.misc.latex import pretty_print
    21412134        sage: pretty_print(ZZ)  # indirect doctest
    21422135        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</span></html>
     2136
     2137    To typeset LaTeX code as-is, use :class:`LatexExpr`::
     2138
     2139        sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}"))
     2140        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{x^2 + 1}{x - 2}</span></html>
    21432141    """
    21442142    if object is None:
    21452143        return