Ticket #11775: trac_11775-pretty_print_multiple_args.3.patch

File trac_11775-pretty_print_multiple_args.3.patch, 8.7 KB (added by ppurka, 10 years ago)

Latest patch based on sage-4.7.2 (Updated to insert space)

  • sage/misc/latex.py

    # HG changeset patch
    # User P Purkayastha <ppurka@gmail.com>
    # Date 1323841229 -28800
    # Node ID 416b28106dd71ca4fad93678b8bf3437f613e2a3
    # Parent  b4d06ecc0d74e02d18cbab4324a531cc690e6083
    trac 11775: make pretty_print accept multiple arguments
    
    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b  
    180180    return "\\left[" + ", ".join([latex(v) for v in x]) + "\\right]"
    181181
    182182
    183 def tuple_function(x):
     183def tuple_function(x, combine_all=False):
    184184    r"""
    185185    Returns the LaTeX code for a tuple ``x``.
    186186
     187    If combine_all is True, then it does not return a tuple and instead
     188    returns a string with all the elements combined into one. It doesn't
     189    collapse tuples which are inside tuples.
     190
    187191    INPUT: ``x`` - a tuple
    188192
    189193    EXAMPLES::
     
    191195        sage: from sage.misc.latex import tuple_function
    192196        sage: tuple_function((1,2,3))
    193197        '\\left(1, 2, 3\\right)'
     198        sage: tuple_function((1,2,3), combine_all=True) # trac 11775
     199        '1 2 3'
     200        sage: tuple_function(((1,2),3), combine_all=True) # trac 11775
     201        '\\left(1, 2\\right) 3'
    194202    """
     203    if combine_all:
     204        return " ".join([latex(v) for v in x])
    195205    return "\\left(" + ", ".join([latex(v) for v in x]) + "\\right)"
    196206
    197207
     
    762772        self.__engine = engine
    763773        self.__density = density
    764774
    765     def __call__(self, x):
     775    def __call__(self, x, combine_all=False):
    766776        r"""
    767777        Return a :class:`LatexExpr` built out of the argument ``x``.
    768778
     779        If combine_all is True and the input is a tuple, then it does not
     780        return a tuple and instead returns a string with all the elements
     781        separated by no space.
     782
    769783        INPUT:
    770784
    771785        - ``x`` - a Sage object
     
    780794            \mathrm{False}
    781795            sage: print latex([x,2])
    782796            \left[x, 2\right]
     797            sage: latex((x,2), combine_all=True) # trac 11775
     798            x 2
    783799        """
    784800        if has_latex_attr(x):
    785801            return LatexExpr(x._latex_())
    786802        try:
    787803            f = latex_table[type(x)]
     804            if type(x) == tuple:
     805                return LatexExpr(f(x, combine_all=combine_all))
    788806            return LatexExpr(f(x))
    789807        except KeyError:
    790808            return LatexExpr(str_function(str(x)))
     
    16311649        <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</div></html>
    16321650    """
    16331651
    1634     def __call__(self, x):
     1652    def __call__(self, x, combine_all=False):
    16351653        r"""
    16361654        Render LaTeX input using JSMath.  This returns a :class:`JSMathExpr`.
    16371655
     
    16391657
    16401658        - ``x`` - a Sage object
    16411659
     1660        - ``combine_all`` -- bool (default: False): this can be used to
     1661          display all the objects, passed as a tuple, as a single object.
     1662
    16421663        OUTPUT: a JSMathExpr
    16431664
    16441665        EXAMPLES::
     
    16491670            sage: str(JSMath().eval(ZZ[x], mode='display')) == str(JSMath()(ZZ[x]))
    16501671            True
    16511672        """
    1652         return self.eval(x)
    1653 
    1654     def eval(self, x, globals=None, locals=None, mode='display'):
     1673        return self.eval(x, combine_all=combine_all)
     1674
     1675    def eval(self, x, globals=None, locals=None, mode='display',
     1676            combine_all=False):
    16551677        r"""
    16561678        Render LaTeX input using JSMath.  This returns a :class:`JSMathExpr`.
    16571679
     
    16671689        -  ``mode`` - string (optional, default 'display): 'display'
    16681690           for displaymath or 'inline' for inline math
    16691691
     1692        - ``combine_all`` -- bool (default: False): this can be used to
     1693          display all the objects, passed as a tuple, as a single object.
     1694
    16701695        OUTPUT: a JSMathExpr
    16711696
    16721697        EXAMPLES::
     
    16781703            <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    16791704            sage: JSMath().eval(type(3), mode='inline')
    16801705            <html>...\verb|&lt;type|\phantom{x}\verb|'sage.rings.integer.Integer'&gt;|</span></html>
     1706            sage: JSMath().eval((1,3), mode='display', combine_all=True)
     1707            <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}1 3</div></html>
    16811708        """
    16821709        # If x is already a LaTeX expression, i.e. the output of latex(blah),
    16831710        # we will treat it as a string, so that we can see the code itself.
    16841711        if isinstance(x, LatexExpr):
    16851712            x = str(x)
    16861713        # Now get a regular LaTeX representation of x...
    1687         x = latex(x)
     1714        x = latex(x, combine_all=combine_all)
    16881715        # ... and make it suitable for jsMath, which has issues with < and >.
    16891716        x = x.replace('<', '&lt;').replace('>', '&gt;')
    16901717        # In jsMath:
     
    17621789        x = str(x)
    17631790    return html(delimiter + x + delimiter)
    17641791
    1765 def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, engine=None, viewer = None, tightpage = None, mode='inline', **kwds):
     1792def view(objects, title='SAGE', debug=False, sep='', tiny=False,
     1793        pdflatex=None, engine=None, viewer = None, tightpage = None,
     1794        mode='inline', combine_all=False, **kwds):
    17661795    r"""nodetex
    17671796    Compute a latex representation of each object in objects, compile,
    17681797    and display typeset. If used from the command line, this requires
     
    17971826    - ``mode`` -- string (default: 'inline'): 'display' for
    17981827      displaymath or 'inline' for inline math
    17991828
     1829    - ``combine_all`` -- bool (default: False): this can be used to display
     1830      all the objects, passed as a tuple, as a single object.
     1831
    18001832
    18011833    OUTPUT: Display typeset objects.
    18021834
     
    18621894        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    18631895        sage: view(3, mode='display')
    18641896        <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html>
     1897        sage: view((x,2), combine_all=True) # trac 11775
     1898        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}x 2</span></html>
    18651899        sage: sage.misc.latex.EMBEDDED_MODE = False
    18661900
    18671901    TESTS::
     
    18921926                jsMath_okay = False
    18931927            if not jsMath_okay:
    18941928                break
    1895         if jsMath_okay:
    1896             print JSMath().eval(objects, mode=mode)  # put comma at end of line?
     1929        if jsMath_okay:  # put comma at end of line in print below?
     1930            print JSMath().eval(objects, mode=mode, combine_all=combine_all)
    18971931        else:
    18981932            if pdflatex is True:
    18991933                engine = "pdflatex"
     
    21202154    else:
    21212155        print(object)
    21222156
    2123 def pretty_print (object):
     2157def pretty_print (*args):
    21242158    r"""
    2125     Try to pretty print an object in an intelligent way.  For graphics
     2159    Try to pretty print the arguments in an intelligent way.  For graphics
    21262160    objects, this returns their default representation.  For other
    21272161    objects, in the notebook, this calls the :func:`view` command,
    21282162    while from the command line, this produces an html string suitable
     
    21302164
    21312165    INPUT:
    21322166
    2133     - ``object`` - a Sage object
     2167    - ``objects`` - The input can be any Sage object, a list or tuple of
     2168      Sage objects, or Sage objects passed in as separate arguments.
    21342169
    21352170    This function is used in the notebook when the ``Typeset`` button is
    21362171    checked.
     
    21402175        sage: from sage.misc.latex import pretty_print
    21412176        sage: pretty_print(ZZ)  # indirect doctest
    21422177        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</span></html>
     2178        sage: pretty_print("Integers = ", ZZ) # trac 11775
     2179        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|Integers|\phantom{x}\verb|=| \Bold{Z}</span></html>
    21432180    """
    2144     if object is None:
    2145         return
    2146     import __builtin__
    2147     __builtin__._=object
    2148 
    2149     from sage.plot.plot import Graphics
    2150     from sage.plot.plot3d.base import Graphics3d
    2151     if isinstance(object, (Graphics, Graphics3d)):
    2152         print repr(object)
    2153         return
    2154     else:
    2155         if EMBEDDED_MODE:
    2156             view(object)
     2181    # view s if it is not empty. Used twice.
     2182    def _show_s(s):
     2183        if s != []:
     2184            if EMBEDDED_MODE:
     2185                view(tuple(s), combine_all=True)
     2186            else:
     2187                print JSMath().eval(tuple(s), mode='inline',
     2188                        combine_all=True)
     2189
     2190    s = []
     2191    for object in args:
     2192        if object is None:
     2193            continue
     2194        import __builtin__
     2195        __builtin__._=object
     2196
     2197        from sage.plot.plot import Graphics
     2198        from sage.plot.plot3d.base import Graphics3d
     2199        if isinstance(object, (Graphics, Graphics3d)):
     2200            _show_s(s)
     2201            s = []
     2202            print repr(object)
    21572203        else:
    2158             print JSMath().eval(object, mode='inline')
    2159         return
     2204            s += [object]
     2205
     2206    _show_s(s)
     2207    return
    21602208
    21612209def pretty_print_default(enable=True):
    21622210    r"""