Ticket #11775: trac_11775-pretty_print_multiple_args.4.patch

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

Rebased to #12156 (fixed documentation)

  • sage/misc/latex.py

    # HG changeset patch
    # User P Purkayastha <ppurka@gmail.com>
    # Date 1323841229 -28800
    # Node ID 85838cfb9e9501fa2acf8e840b0300e99d074292
    # Parent  c26aeb64b77e00ba38533058460e1ec541404aef
    trac 11775: make pretty_print accept multiple arguments (rebased to #12156)
    
    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     INPUT: ``x`` - a tuple
     179    INPUT:
     180
     181    - ``x`` - a tuple
     182
     183    - ``combine_all`` - boolean (Default: False) If combine_all is True,
     184      then it does not return a tuple and instead returns a string with all
     185      the elements separated by a single space. It does not collapse tuples
     186      which are inside tuples.
    180187
    181188    EXAMPLES::
    182189
    183190        sage: from sage.misc.latex import tuple_function
    184191        sage: tuple_function((1,2,3))
    185192        '\\left(1, 2, 3\\right)'
     193        sage: tuple_function((1,2,3), combine_all=True) # trac 11775
     194        '1 2 3'
     195        sage: tuple_function(((1,2),3), combine_all=True) # trac 11775
     196        '\\left(1, 2\\right) 3'
    186197    """
     198    if combine_all:
     199        return " ".join([latex(v) for v in x])
    187200    return "\\left(" + ", ".join([latex(v) for v in x]) + "\\right)"
    188201
    189202
     
    886899        self.__engine = engine
    887900        self.__density = density
    888901
    889     def __call__(self, x):
     902    def __call__(self, x, combine_all=False):
    890903        r"""
    891904        Return a :class:`LatexExpr` built out of the argument ``x``.
    892905
     
    894907
    895908        - ``x`` - a Sage object
    896909
     910        - ``combine_all`` - boolean (Default: False) If combine_all is True
     911          and the input is a tuple, then it does not return a tuple and
     912          instead returns a string with all the elements separated by
     913          a single space.
     914
    897915        OUTPUT: a LatexExpr built from ``x``
    898916
    899917        EXAMPLES::
     
    904922            \mathrm{False}
    905923            sage: print latex([x,2])
    906924            \left[x, 2\right]
     925            sage: latex((x,2), combine_all=True) # trac 11775
     926            x 2
    907927        """
    908928        if has_latex_attr(x):
    909929            return LatexExpr(x._latex_())
    910930        try:
    911931            f = latex_table[type(x)]
     932            if type(x) == tuple:
     933                return LatexExpr(f(x, combine_all=combine_all))
    912934            return LatexExpr(f(x))
    913935        except KeyError:
    914936            return LatexExpr(str_function(str(x)))
     
    17581780        <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</div></html>
    17591781    """
    17601782
    1761     def __call__(self, x):
     1783    def __call__(self, x, combine_all=False):
    17621784        r"""
    17631785        Render LaTeX input using JSMath.  This returns a :class:`JSMathExpr`.
    17641786
     
    17661788
    17671789        - ``x`` - a Sage object
    17681790
     1791        - ``combine_all`` - boolean (Default: False): If combine_all is
     1792          True and the input is a tuple, then it does not return a tuple
     1793          and instead returns a string with all the elements separated by
     1794          a single space.
     1795
    17691796        OUTPUT: a JSMathExpr
    17701797
    17711798        EXAMPLES::
     
    17761803            sage: str(JSMath().eval(ZZ[x], mode='display')) == str(JSMath()(ZZ[x]))
    17771804            True
    17781805        """
    1779         return self.eval(x)
    1780 
    1781     def eval(self, x, globals=None, locals=None, mode='display'):
     1806        return self.eval(x, combine_all=combine_all)
     1807
     1808    def eval(self, x, globals=None, locals=None, mode='display',
     1809            combine_all=False):
    17821810        r"""
    17831811        Render LaTeX input using JSMath.  This returns a :class:`JSMathExpr`.
    17841812
     
    17861814
    17871815        - ``x`` - a Sage object
    17881816
    1789         -  ``globals`` -- a globals dictionary
     1817        -  ``globals`` - a globals dictionary
    17901818
    17911819        -  ``locals`` - extra local variables used when
    17921820           evaluating Sage code in ``x``.
     
    17941822        -  ``mode`` - string (optional, default 'display): 'display'
    17951823           for displaymath or 'inline' for inline math
    17961824
     1825        - ``combine_all`` - boolean (Default: False): If combine_all is
     1826          True and the input is a tuple, then it does not return a tuple
     1827          and instead returns a string with all the elements separated by
     1828          a single space.
     1829
    17971830        OUTPUT: a JSMathExpr
    17981831
    17991832        EXAMPLES::
     
    18051838            <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    18061839            sage: JSMath().eval(type(3), mode='inline')
    18071840            <html>...\verb|&lt;type|\phantom{x}\verb|'sage.rings.integer.Integer'&gt;|</span></html>
     1841            sage: JSMath().eval((1,3), mode='display', combine_all=True)
     1842            <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}1 3</div></html>
    18081843        """
    18091844        # Get a regular LaTeX representation of x...
    1810         x = latex(x)
     1845        x = latex(x, combine_all=combine_all)
    18111846        # ... and make it suitable for jsMath, which has issues with < and >.
    18121847        x = x.replace('<', '&lt;').replace('>', '&gt;')
    18131848        # In jsMath:
     
    18851920        x = str(x)
    18861921    return html(delimiter + x + delimiter)
    18871922
    1888 def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, engine=None, viewer = None, tightpage = None, mode='inline', **kwds):
     1923def view(objects, title='SAGE', debug=False, sep='', tiny=False,
     1924        pdflatex=None, engine=None, viewer = None, tightpage = None,
     1925        mode='inline', combine_all=False, **kwds):
    18891926    r"""nodetex
    18901927    Compute a latex representation of each object in objects, compile,
    18911928    and display typeset. If used from the command line, this requires
     
    19111948
    19121949    -  ``engine`` - 'latex', 'pdflatex', or 'xelatex'
    19131950
    1914     -  ``viewer`` -- string or None (default: None): specify a viewer to
     1951    -  ``viewer`` - string or None (default: None): specify a viewer to
    19151952       use; currently the only options are None and 'pdf'.
    19161953
    19171954    -  ``tightpage`` - bool (default: False): use the LaTeX package
    19181955       'preview' with the 'tightpage' option.
    19191956
    1920     - ``mode`` -- string (default: 'inline'): 'display' for
     1957    - ``mode`` - string (default: 'inline'): 'display' for
    19211958      displaymath or 'inline' for inline math
    19221959
     1960    - ``combine_all`` - bool (default: False): If combine_all is True and
     1961      the input is a tuple, then it does not return a tuple and instead
     1962      returns a string with all the elements separated by a single space.
     1963
    19231964
    19241965    OUTPUT: Display typeset objects.
    19251966
     
    19852026        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    19862027        sage: view(3, mode='display')
    19872028        <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html>
     2029        sage: view((x,2), combine_all=True) # trac 11775
     2030        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}x 2</span></html>
    19882031        sage: sage.misc.latex.EMBEDDED_MODE = False
    19892032
    19902033    TESTS::
     
    20122055                jsMath_okay = False
    20132056            if not jsMath_okay:
    20142057                break
    2015         if jsMath_okay:
    2016             print JSMath().eval(objects, mode=mode)  # put comma at end of line?
     2058        if jsMath_okay:  # put comma at end of line in print below?
     2059            print JSMath().eval(objects, mode=mode, combine_all=combine_all)
    20172060        else:
    20182061            if pdflatex is True:
    20192062                engine = "pdflatex"
     
    22402283    else:
    22412284        print(object)
    22422285
    2243 def pretty_print (object):
     2286def pretty_print (*args):
    22442287    r"""
    2245     Try to pretty print an object in an intelligent way.  For graphics
     2288    Try to pretty print the arguments in an intelligent way.  For graphics
    22462289    objects, this returns their default representation.  For other
    22472290    objects, in the notebook, this calls the :func:`view` command,
    22482291    while from the command line, this produces an html string suitable
     
    22502293
    22512294    INPUT:
    22522295
    2253     - ``object`` -- a Sage object
     2296    - ``objects`` - The input can be any Sage object, a list or tuple of
     2297      Sage objects, or Sage objects passed in as separate arguments.
    22542298
    22552299    This function is used in the notebook when the "Typeset" button is
    22562300    checked.
     
    22592303
    22602304        sage: pretty_print(ZZ)  # indirect doctest
    22612305        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</span></html>
     2306        sage: pretty_print("Integers = ", ZZ) # trac 11775
     2307        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|Integers|\phantom{x}\verb|=| \Bold{Z}</span></html>
    22622308
    22632309    To typeset LaTeX code as-is, use :class:`LatexExpr`::
    22642310
    22652311        sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}"))
    22662312        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{x^2 + 1}{x - 2}</span></html>
    22672313    """
    2268     if object is None:
    2269         return
    2270     import __builtin__
    2271     __builtin__._=object
    2272 
    2273     from sage.plot.all import Graphics
    2274     from sage.plot.plot3d.base import Graphics3d
    2275     if isinstance(object, (Graphics, Graphics3d)):
    2276         print repr(object)
    2277         return
    2278     else:
    2279         if EMBEDDED_MODE:
    2280             view(object)
     2314    # view s if it is not empty. Used twice.
     2315    def _show_s(s):
     2316        if s != []:
     2317            if EMBEDDED_MODE:
     2318                view(tuple(s), combine_all=True)
     2319            else:
     2320                print JSMath().eval(tuple(s), mode='inline',
     2321                        combine_all=True)
     2322
     2323    s = []
     2324    for object in args:
     2325        if object is None:
     2326            continue
     2327        import __builtin__
     2328        __builtin__._=object
     2329
     2330        from sage.plot.plot import Graphics
     2331        from sage.plot.plot3d.base import Graphics3d
     2332        if isinstance(object, (Graphics, Graphics3d)):
     2333            _show_s(s)
     2334            s = []
     2335            print repr(object)
     2336
    22812337        else:
    2282             print JSMath().eval(object, mode='inline')
    2283         return
     2338            s.append(object)
     2339
     2340    _show_s(s)
     2341    return
    22842342
    22852343def pretty_print_default(enable=True):
    22862344    r"""