Ticket #11775: trac_11775-pretty_print_multiple_args.5.patch

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

Rebased to sage-5.1.beta4

  • sage/misc/latex.py

    # HG changeset patch
    # User P Purkayastha <ppurka@gmail.com>
    # Date 1323841229 -28800
    # Node ID 3630cbd175f3e98863ae928c1b02f81eb2e4893c
    # Parent  c32bd63d6b03ded645f57028ae29b682db7e4b26
    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
     
    890903        self.__engine = engine
    891904        self.__density = density
    892905
    893     def __call__(self, x):
     906    def __call__(self, x, combine_all=False):
    894907        r"""
    895908        Return a :class:`LatexExpr` built out of the argument ``x``.
    896909
     
    898911
    899912        - ``x`` - a Sage object
    900913
     914        - ``combine_all`` - boolean (Default: False) If combine_all is True
     915          and the input is a tuple, then it does not return a tuple and
     916          instead returns a string with all the elements separated by
     917          a single space.
     918
    901919        OUTPUT: a LatexExpr built from ``x``
    902920
    903921        EXAMPLES::
     
    908926            \mathrm{False}
    909927            sage: print latex([x,2])
    910928            \left[x, 2\right]
     929            sage: latex((x,2), combine_all=True) # trac 11775
     930            x 2
    911931        """
    912932        if has_latex_attr(x):
    913933            return LatexExpr(x._latex_())
    914934        try:
    915935            f = latex_table[type(x)]
     936            if type(x) == tuple:
     937                return LatexExpr(f(x, combine_all=combine_all))
    916938            return LatexExpr(f(x))
    917939        except KeyError:
    918940            return LatexExpr(str_function(str(x)))
     
    17621784        <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</div></html>
    17631785    """
    17641786
    1765     def __call__(self, x):
     1787    def __call__(self, x, combine_all=False):
    17661788        r"""
    17671789        Render LaTeX input using JSMath.  This returns a :class:`JSMathExpr`.
    17681790
     
    17701792
    17711793        - ``x`` - a Sage object
    17721794
     1795        - ``combine_all`` - boolean (Default: False): If combine_all is
     1796          True and the input is a tuple, then it does not return a tuple
     1797          and instead returns a string with all the elements separated by
     1798          a single space.
     1799
    17731800        OUTPUT: a JSMathExpr
    17741801
    17751802        EXAMPLES::
     
    17801807            sage: str(JSMath().eval(ZZ[x], mode='display')) == str(JSMath()(ZZ[x]))
    17811808            True
    17821809        """
    1783         return self.eval(x)
    1784 
    1785     def eval(self, x, globals=None, locals=None, mode='display'):
     1810        return self.eval(x, combine_all=combine_all)
     1811
     1812    def eval(self, x, globals=None, locals=None, mode='display',
     1813            combine_all=False):
    17861814        r"""
    17871815        Render LaTeX input using JSMath.  This returns a :class:`JSMathExpr`.
    17881816
     
    17901818
    17911819        - ``x`` - a Sage object
    17921820
    1793         -  ``globals`` -- a globals dictionary
     1821        -  ``globals`` - a globals dictionary
    17941822
    17951823        -  ``locals`` - extra local variables used when
    17961824           evaluating Sage code in ``x``.
     
    17981826        -  ``mode`` - string (optional, default 'display): 'display'
    17991827           for displaymath or 'inline' for inline math
    18001828
     1829        - ``combine_all`` - boolean (Default: False): If combine_all is
     1830          True and the input is a tuple, then it does not return a tuple
     1831          and instead returns a string with all the elements separated by
     1832          a single space.
     1833
    18011834        OUTPUT: a JSMathExpr
    18021835
    18031836        EXAMPLES::
     
    18091842            <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    18101843            sage: JSMath().eval(type(3), mode='inline')
    18111844            <html>...\verb|&lt;type|\phantom{x}\verb|'sage.rings.integer.Integer'&gt;|</span></html>
     1845            sage: JSMath().eval((1,3), mode='display', combine_all=True)
     1846            <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}1 3</div></html>
    18121847        """
    18131848        # Get a regular LaTeX representation of x...
    1814         x = latex(x)
     1849        x = latex(x, combine_all=combine_all)
    18151850        # ... and make it suitable for jsMath, which has issues with < and >.
    18161851        x = x.replace('<', '&lt;').replace('>', '&gt;')
    18171852        # In jsMath:
     
    18891924        x = str(x)
    18901925    return html(delimiter + x + delimiter)
    18911926
    1892 def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, engine=None, viewer = None, tightpage = None, mode='inline', **kwds):
     1927def view(objects, title='SAGE', debug=False, sep='', tiny=False,
     1928        pdflatex=None, engine=None, viewer = None, tightpage = None,
     1929        mode='inline', combine_all=False, **kwds):
    18931930    r"""nodetex
    18941931    Compute a latex representation of each object in objects, compile,
    18951932    and display typeset. If used from the command line, this requires
     
    19151952
    19161953    -  ``engine`` - 'latex', 'pdflatex', or 'xelatex'
    19171954
    1918     -  ``viewer`` -- string or None (default: None): specify a viewer to
     1955    -  ``viewer`` - string or None (default: None): specify a viewer to
    19191956       use; currently the only options are None and 'pdf'.
    19201957
    19211958    -  ``tightpage`` - bool (default: False): use the LaTeX package
    19221959       'preview' with the 'tightpage' option.
    19231960
    1924     - ``mode`` -- string (default: 'inline'): 'display' for
     1961    - ``mode`` - string (default: 'inline'): 'display' for
    19251962      displaymath or 'inline' for inline math
    19261963
     1964    - ``combine_all`` - bool (default: False): If combine_all is True and
     1965      the input is a tuple, then it does not return a tuple and instead
     1966      returns a string with all the elements separated by a single space.
     1967
    19271968
    19281969    OUTPUT: Display typeset objects.
    19291970
     
    19892030        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    19902031        sage: view(3, mode='display')
    19912032        <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html>
     2033        sage: view((x,2), combine_all=True) # trac 11775
     2034        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}x 2</span></html>
    19922035        sage: sage.misc.latex.EMBEDDED_MODE = False
    19932036
    19942037    TESTS::
     
    20322075                jsMath_okay = False
    20332076            if not jsMath_okay:
    20342077                break
    2035         if jsMath_okay:
    2036             print JSMath().eval(objects, mode=mode)  # put comma at end of line?
     2078        if jsMath_okay:  # put comma at end of line in print below?
     2079            print JSMath().eval(objects, mode=mode, combine_all=combine_all)
    20372080        else:
    20382081            base_dir = os.path.abspath("")
    20392082            png_file = graphics_filename(ext='png')
     
    22502293    else:
    22512294        print(object)
    22522295
    2253 def pretty_print (object):
     2296def pretty_print (*args):
    22542297    r"""
    2255     Try to pretty print an object in an intelligent way.  For graphics
     2298    Try to pretty print the arguments in an intelligent way.  For graphics
    22562299    objects, this returns their default representation.  For other
    22572300    objects, in the notebook, this calls the :func:`view` command,
    22582301    while from the command line, this produces an html string suitable
     
    22602303
    22612304    INPUT:
    22622305
    2263     - ``object`` -- a Sage object
     2306    - ``objects`` - The input can be any Sage object, a list or tuple of
     2307      Sage objects, or Sage objects passed in as separate arguments.
    22642308
    22652309    This function is used in the notebook when the "Typeset" button is
    22662310    checked.
     
    22692313
    22702314        sage: pretty_print(ZZ)  # indirect doctest
    22712315        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</span></html>
     2316        sage: pretty_print("Integers = ", ZZ) # trac 11775
     2317        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|Integers|\phantom{x}\verb|=| \Bold{Z}</span></html>
    22722318
    22732319    To typeset LaTeX code as-is, use :class:`LatexExpr`::
    22742320
    22752321        sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}"))
    22762322        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{x^2 + 1}{x - 2}</span></html>
    22772323    """
    2278     if object is None:
    2279         return
    2280     import __builtin__
    2281     __builtin__._=object
    2282 
    2283     from sage.plot.all import Graphics
    2284     from sage.plot.plot3d.base import Graphics3d
    2285     if isinstance(object, (Graphics, Graphics3d)):
    2286         print repr(object)
    2287         return
    2288     else:
    2289         if EMBEDDED_MODE:
    2290             view(object)
     2324    # view s if it is not empty. Used twice.
     2325    def _show_s(s):
     2326        if s != []:
     2327            if EMBEDDED_MODE:
     2328                view(tuple(s), combine_all=True)
     2329            else:
     2330                print JSMath().eval(tuple(s), mode='inline',
     2331                        combine_all=True)
     2332
     2333    s = []
     2334    for object in args:
     2335        if object is None:
     2336            continue
     2337        import __builtin__
     2338        __builtin__._=object
     2339
     2340        from sage.plot.plot import Graphics
     2341        from sage.plot.plot3d.base import Graphics3d
     2342        if isinstance(object, (Graphics, Graphics3d)):
     2343            _show_s(s)
     2344            s = []
     2345            print repr(object)
     2346
    22912347        else:
    2292             print JSMath().eval(object, mode='inline')
    2293         return
     2348            s.append(object)
     2349
     2350    _show_s(s)
     2351    return
    22942352
    22952353def pretty_print_default(enable=True):
    22962354    r"""