# HG changeset patch # User P Purkayastha # 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/sage/misc/latex.py +++ b/sage/misc/latex.py @@ -172,18 +172,31 @@ 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``. - INPUT: ``x`` - a tuple + INPUT: + + - ``x`` - a tuple + + - ``combine_all`` - boolean (Default: False) If combine_all is True, + then it does not return a tuple and instead returns a string with all + the elements separated by a single space. It does not collapse tuples + which are inside tuples. EXAMPLES:: 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)" @@ -886,7 +899,7 @@ 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``. @@ -894,6 +907,11 @@ - ``x`` - a Sage object + - ``combine_all`` - boolean (Default: False) 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 + a single space. + OUTPUT: a LatexExpr built from ``x`` EXAMPLES:: @@ -904,11 +922,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))) @@ -1758,7 +1780,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`. @@ -1766,6 +1788,11 @@ - ``x`` - a Sage object + - ``combine_all`` - boolean (Default: False): 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 + a single space. + OUTPUT: a JSMathExpr EXAMPLES:: @@ -1776,9 +1803,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`. @@ -1786,7 +1814,7 @@ - ``x`` - a Sage object - - ``globals`` -- a globals dictionary + - ``globals`` - a globals dictionary - ``locals`` - extra local variables used when evaluating Sage code in ``x``. @@ -1794,6 +1822,11 @@ - ``mode`` - string (optional, default 'display): 'display' for displaymath or 'inline' for inline math + - ``combine_all`` - boolean (Default: False): 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 + a single space. + OUTPUT: a JSMathExpr EXAMPLES:: @@ -1805,9 +1838,11 @@ \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
""" # 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: @@ -1885,7 +1920,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 @@ -1911,15 +1948,19 @@ - ``engine`` - 'latex', 'pdflatex', or 'xelatex' - - ``viewer`` -- string or None (default: None): specify a viewer to + - ``viewer`` - string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - - ``mode`` -- string (default: 'inline'): 'display' for + - ``mode`` - string (default: 'inline'): 'display' for displaymath or 'inline' for inline math + - ``combine_all`` - bool (default: False): 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 a single space. + OUTPUT: Display typeset objects. @@ -1985,6 +2026,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:: @@ -2012,8 +2055,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" @@ -2240,9 +2283,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 @@ -2250,7 +2293,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. @@ -2259,28 +2303,42 @@ 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} To typeset LaTeX code as-is, use :class:`LatexExpr`:: sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}")) \newcommand{\Bold}[1]{\mathbf{#1}}\frac{x^2 + 1}{x - 2} """ - if object is None: - return - import __builtin__ - __builtin__._=object - - from sage.plot.all 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.append(object) + + _show_s(s) + return def pretty_print_default(enable=True): r"""