Ticket #11775: trac_11775-pretty_print_multiple_args_mathjax.patch

File trac_11775-pretty_print_multiple_args_mathjax.patch, 8.8 KB (added by ppurka, 10 years ago)

Apply only to mathjax-ified sage{nb}

  • sage/misc/latex.py

    # HG changeset patch
    # User Punarbasu Purkayastha <ppurka@gmail.com>
    # Date 1330189086 -28800
    # Node ID 7c0237f6509d7cff55c14b4676de3c844a85f643
    # Parent  0bae00abf2731eb6ed239d8781ef86ecc202b59d
    make pretty_print accept multiple arguments
    
    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b  
    172172    return "\\left[" + ", ".join([latex(v) for v in x]) + "\\right]"
    173173
    174174
    175 def tuple_function(x):
     175def tuple_function(x, combine_all=False):
    176176    r"""
    177177    Returns the LaTeX code for a tuple ``x``.
    178178
     179    If combine_all is True, then it does not return a tuple and instead
     180    returns a string with all the elements combined into one. It doesn't
     181    collapse tuples which are inside tuples.
     182
    179183    INPUT: ``x`` - a tuple
    180184
    181185    EXAMPLES::
     
    183187        sage: from sage.misc.latex import tuple_function
    184188        sage: tuple_function((1,2,3))
    185189        '\\left(1, 2, 3\\right)'
     190        sage: tuple_function((1,2,3), combine_all=True)
     191        '1 2 3'
     192        sage: tuple_function(((1,2),3), combine_all=True)
     193        '\\left(1, 2\\right) 3'
    186194    """
     195    if combine_all:
     196        return " ".join([latex(v) for v in x])
    187197    return "\\left(" + ", ".join([latex(v) for v in x]) + "\\right)"
    188198
    189199
     
    848858        self.__engine = engine
    849859        self.__density = density
    850860
    851     def __call__(self, x):
     861    def __call__(self, x, combine_all=False):
    852862        r"""
    853863        Return a :class:`LatexExpr` built out of the argument ``x``.
    854864
     865        If combine_all is True and the input is a tuple, then it does not
     866        return a tuple and instead returns a string with all the elements
     867        separated by no space.
     868
    855869        INPUT:
    856870
    857871        - ``x`` - a Sage object
     
    866880            \mathrm{False}
    867881            sage: print latex([x,2])
    868882            \left[x, 2\right]
     883            sage: latex((x,2), combine_all=True)
     884            x 2
    869885        """
    870886        if has_latex_attr(x):
    871887            return LatexExpr(x._latex_())
    872888        try:
    873889            f = latex_table[type(x)]
     890            if type(x) == tuple:
     891                return LatexExpr(f(x, combine_all=combine_all))
    874892            return LatexExpr(f(x))
    875893        except KeyError:
    876894            return LatexExpr(str_function(str(x)))
     
    17171735        <html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</script></html>
    17181736    """
    17191737
    1720     def __call__(self, x):
     1738    def __call__(self, x, combine_all=False):
    17211739        r"""
    17221740        Render LaTeX input using MathJax.  This returns a :class:`MathJaxExpr`.
    17231741
     
    17251743
    17261744        - ``x`` - a Sage object
    17271745
     1746        - ``combine_all`` -- bool (default: False): this can be used to
     1747          display all the objects, passed as a tuple, as a single object.
     1748
    17281749        OUTPUT: a MathJaxExpr
    17291750
    17301751        EXAMPLES::
     
    17351756            sage: str(MathJax().eval(ZZ[x], mode='display')) == str(MathJax()(ZZ[x]))
    17361757            True
    17371758        """
    1738         return self.eval(x)
    1739 
    1740     def eval(self, x, globals=None, locals=None, mode='display'):
     1759        return self.eval(x, combine_all=combine_all)
     1760
     1761    def eval(self, x, globals=None, locals=None, mode='display',
     1762            combine_all=False):
    17411763        r"""
    17421764        Render LaTeX input using MathJax.  This returns a :class:`MathJaxExpr`.
    17431765
     
    17531775        -  ``mode`` - string (optional, default 'display): 'display'
    17541776           for displaymath or 'inline' for inline math
    17551777
     1778        - ``combine_all`` -- bool (default: False): this can be used to
     1779          display all the objects, passed as a tuple, as a single object.
     1780
    17561781        OUTPUT: a MathJaxExpr
    17571782
    17581783        EXAMPLES::
     
    17641789            <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}3</script></html>
    17651790            sage: MathJax().eval(type(3), mode='inline')
    17661791            <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{ < type 'sage.rings.integer.Integer' > }</script></html>
     1792            sage: MathJax().eval((1,3), mode='display', combine_all=True)
     1793            <html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}1 3</script></html>
    17671794        """
    17681795        # Get a regular LaTeX representation of x...
    1769         x = latex(x)
     1796        x = latex(x, combine_all=combine_all)
    17701797        # ... and make it suitable for MathJax and html
    17711798        x = x.replace('<', '&lt;').replace('>', '&gt;')
    17721799        # In MathJax:
     
    17851812                         + _Latex_prefs._option['macros']
    17861813                         + '%s</script></html>'%x)
    17871814
    1788 def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, engine=None, viewer = None, tightpage = None, mode='inline', **kwds):
     1815def view(objects, title='SAGE', debug=False, sep='', tiny=False,
     1816        pdflatex=None, engine=None, viewer = None, tightpage = None,
     1817        mode='inline', combine_all=False, **kwds):
    17891818    r"""nodetex
    17901819    Compute a latex representation of each object in objects, compile,
    17911820    and display typeset. If used from the command line, this requires
     
    18201849    - ``mode`` -- string (default: 'inline'): 'display' for
    18211850      displaymath or 'inline' for inline math
    18221851
     1852    - ``combine_all`` -- bool (default: False): this can be used to
     1853      display all the objects, passed as a tuple, as a single object.
     1854
    18231855
    18241856    OUTPUT: Display typeset objects.
    18251857
     
    18851917        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}3</script></html>
    18861918        sage: view(3, mode='display')
    18871919        <html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}3</script></html>
     1920        sage: view((x,2), combine_all=True)
     1921        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}x 2</script></html>
    18881922        sage: sage.misc.latex.EMBEDDED_MODE = False
    18891923
    18901924    TESTS::
     
    19121946                MathJax_okay = False
    19131947            if not MathJax_okay:
    19141948                break
    1915         if MathJax_okay:
    1916             print MathJax().eval(objects, mode=mode)  # put comma at end of line?
     1949        if MathJax_okay:  # put comma at end of line in print below?
     1950            print MathJax().eval(objects, mode=mode, combine_all=combine_all)
    19171951        else:
    19181952            if pdflatex is True:
    19191953                engine = "pdflatex"
     
    21402174    else:
    21412175        print(object)
    21422176
    2143 def pretty_print (object):
     2177def pretty_print(*args):
    21442178    r"""
    2145     Try to pretty print an object in an intelligent way.  For graphics
     2179    Try to pretty print the arguments in an intelligent way.  For graphics
    21462180    objects, this returns their default representation.  For other
    21472181    objects, in the notebook, this calls the :func:`view` command,
    21482182    while from the command line, this produces an html string suitable
     
    21502184
    21512185    INPUT:
    21522186
    2153     - ``object`` -- a Sage object
     2187    - ``objects`` - The input can be any Sage object, a list or tuple of
     2188      Sage objects, or Sage objects passed in as separate arguments.
    21542189
    21552190    This function is used in the notebook when the "Typeset" button is
    21562191    checked.
     
    21592194
    21602195        sage: pretty_print(ZZ)  # indirect doctest
    21612196        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</script></html>
     2197        sage: pretty_print("Integers = ", ZZ)
     2198        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|Integers|\phantom{\verb!x!}\verb|=| \Bold{Z}</script></html>
    21622199
    21632200    To typeset LaTeX code as-is, use :class:`LatexExpr`::
    21642201
    21652202        sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}"))
    21662203        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{x^2 + 1}{x - 2}</script></html>
    21672204    """
    2168     if object is None:
    2169         return
    2170     import __builtin__
    2171     __builtin__._=object
    2172 
    2173     from sage.plot.plot import Graphics
    2174     from sage.plot.plot3d.base import Graphics3d
    2175     if isinstance(object, (Graphics, Graphics3d)):
    2176         print repr(object)
    2177         return
    2178     else:
    2179         if EMBEDDED_MODE:
    2180             view(object)
     2205    # view s if it is not empty. Used twice.
     2206    def _show_s(s):
     2207        if s != []:
     2208            if EMBEDDED_MODE:
     2209                view(tuple(s), combine_all=True)
     2210            else:
     2211                print MathJax().eval(tuple(s), mode='inline',
     2212                        combine_all=True)
     2213
     2214    s = []
     2215    for object in args:
     2216        if object is None:
     2217            continue
     2218        import __builtin__
     2219        __builtin__._=object
     2220
     2221        from sage.plot.plot import Graphics
     2222        from sage.plot.plot3d.base import Graphics3d
     2223        if isinstance(object, (Graphics, Graphics3d)):
     2224            _show_s(s)
     2225            s = []
     2226            print repr(object)
     2227
    21812228        else:
    2182             print MathJax().eval(object, mode='inline')
    2183         return
     2229            s += [object]
     2230
     2231    _show_s(s)
     2232    return
    21842233
    21852234def pretty_print_default(enable=True):
    21862235    r"""