# HG changeset patch # User P Purkayastha # 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/sage/misc/latex.py +++ b/sage/misc/latex.py @@ -180,10 +180,14 @@ return "\\left[" + ", ".join([latex(v) for v in x]) + "\\right]" -def tuple_function(x): +def tuple_function(x, combine_all=False): r""" Returns the LaTeX code for a tuple ``x``. + If combine_all is True, then it does not return a tuple and instead + returns a string with all the elements combined into one. It doesn't + collapse tuples which are inside tuples. + INPUT: ``x`` - a tuple EXAMPLES:: @@ -191,7 +195,13 @@ sage: from sage.misc.latex import tuple_function sage: tuple_function((1,2,3)) '\\left(1, 2, 3\\right)' + sage: tuple_function((1,2,3), combine_all=True) # trac 11775 + '1 2 3' + sage: tuple_function(((1,2),3), combine_all=True) # trac 11775 + '\\left(1, 2\\right) 3' """ + if combine_all: + return " ".join([latex(v) for v in x]) return "\\left(" + ", ".join([latex(v) for v in x]) + "\\right)" @@ -762,10 +772,14 @@ self.__engine = engine self.__density = density - def __call__(self, x): + def __call__(self, x, combine_all=False): r""" Return a :class:`LatexExpr` built out of the argument ``x``. + If combine_all is True and the input is a tuple, then it does not + return a tuple and instead returns a string with all the elements + separated by no space. + INPUT: - ``x`` - a Sage object @@ -780,11 +794,15 @@ \mathrm{False} sage: print latex([x,2]) \left[x, 2\right] + sage: latex((x,2), combine_all=True) # trac 11775 + x 2 """ if has_latex_attr(x): return LatexExpr(x._latex_()) try: f = latex_table[type(x)] + if type(x) == tuple: + return LatexExpr(f(x, combine_all=combine_all)) return LatexExpr(f(x)) except KeyError: return LatexExpr(str_function(str(x))) @@ -1631,7 +1649,7 @@
\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}
""" - def __call__(self, x): + def __call__(self, x, combine_all=False): r""" Render LaTeX input using JSMath. This returns a :class:`JSMathExpr`. @@ -1639,6 +1657,9 @@ - ``x`` - a Sage object + - ``combine_all`` -- bool (default: False): this can be used to + display all the objects, passed as a tuple, as a single object. + OUTPUT: a JSMathExpr EXAMPLES:: @@ -1649,9 +1670,10 @@ sage: str(JSMath().eval(ZZ[x], mode='display')) == str(JSMath()(ZZ[x])) True """ - return self.eval(x) - - def eval(self, x, globals=None, locals=None, mode='display'): + return self.eval(x, combine_all=combine_all) + + def eval(self, x, globals=None, locals=None, mode='display', + combine_all=False): r""" Render LaTeX input using JSMath. This returns a :class:`JSMathExpr`. @@ -1667,6 +1689,9 @@ - ``mode`` - string (optional, default 'display): 'display' for displaymath or 'inline' for inline math + - ``combine_all`` -- bool (default: False): this can be used to + display all the objects, passed as a tuple, as a single object. + OUTPUT: a JSMathExpr EXAMPLES:: @@ -1678,13 +1703,15 @@ \newcommand{\Bold}[1]{\mathbf{#1}}3 sage: JSMath().eval(type(3), mode='inline') ...\verb|<type|\phantom{x}\verb|'sage.rings.integer.Integer'>| + sage: JSMath().eval((1,3), mode='display', combine_all=True) +
\newcommand{\Bold}[1]{\mathbf{#1}}1 3
""" # If x is already a LaTeX expression, i.e. the output of latex(blah), # we will treat it as a string, so that we can see the code itself. if isinstance(x, LatexExpr): x = str(x) # Now get a regular LaTeX representation of x... - x = latex(x) + x = latex(x, combine_all=combine_all) # ... and make it suitable for jsMath, which has issues with < and >. x = x.replace('<', '<').replace('>', '>') # In jsMath: @@ -1762,7 +1789,9 @@ x = str(x) return html(delimiter + x + delimiter) -def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, engine=None, viewer = None, tightpage = None, mode='inline', **kwds): +def view(objects, title='SAGE', debug=False, sep='', tiny=False, + pdflatex=None, engine=None, viewer = None, tightpage = None, + mode='inline', combine_all=False, **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires @@ -1797,6 +1826,9 @@ - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math + - ``combine_all`` -- bool (default: False): this can be used to display + all the objects, passed as a tuple, as a single object. + OUTPUT: Display typeset objects. @@ -1862,6 +1894,8 @@ \newcommand{\Bold}[1]{\mathbf{#1}}3 sage: view(3, mode='display')
\newcommand{\Bold}[1]{\mathbf{#1}}3
+ sage: view((x,2), combine_all=True) # trac 11775 + \newcommand{\Bold}[1]{\mathbf{#1}}x 2 sage: sage.misc.latex.EMBEDDED_MODE = False TESTS:: @@ -1892,8 +1926,8 @@ jsMath_okay = False if not jsMath_okay: break - if jsMath_okay: - print JSMath().eval(objects, mode=mode) # put comma at end of line? + if jsMath_okay: # put comma at end of line in print below? + print JSMath().eval(objects, mode=mode, combine_all=combine_all) else: if pdflatex is True: engine = "pdflatex" @@ -2120,9 +2154,9 @@ else: print(object) -def pretty_print (object): +def pretty_print (*args): r""" - Try to pretty print an object in an intelligent way. For graphics + Try to pretty print the arguments in an intelligent way. For graphics objects, this returns their default representation. For other objects, in the notebook, this calls the :func:`view` command, while from the command line, this produces an html string suitable @@ -2130,7 +2164,8 @@ INPUT: - - ``object`` - a Sage object + - ``objects`` - The input can be any Sage object, a list or tuple of + Sage objects, or Sage objects passed in as separate arguments. This function is used in the notebook when the ``Typeset`` button is checked. @@ -2140,23 +2175,36 @@ sage: from sage.misc.latex import pretty_print sage: pretty_print(ZZ) # indirect doctest \newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z} + sage: pretty_print("Integers = ", ZZ) # trac 11775 + \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Integers|\phantom{x}\verb|=| \Bold{Z} """ - if object is None: - return - import __builtin__ - __builtin__._=object - - from sage.plot.plot import Graphics - from sage.plot.plot3d.base import Graphics3d - if isinstance(object, (Graphics, Graphics3d)): - print repr(object) - return - else: - if EMBEDDED_MODE: - view(object) + # view s if it is not empty. Used twice. + def _show_s(s): + if s != []: + if EMBEDDED_MODE: + view(tuple(s), combine_all=True) + else: + print JSMath().eval(tuple(s), mode='inline', + combine_all=True) + + s = [] + for object in args: + if object is None: + continue + import __builtin__ + __builtin__._=object + + from sage.plot.plot import Graphics + from sage.plot.plot3d.base import Graphics3d + if isinstance(object, (Graphics, Graphics3d)): + _show_s(s) + s = [] + print repr(object) else: - print JSMath().eval(object, mode='inline') - return + s += [object] + + _show_s(s) + return def pretty_print_default(enable=True): r"""