# 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 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)" 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. - 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:: \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)))
\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. - 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:: 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. - x - a Sage object -  globals -- a globals dictionary -  globals - a globals dictionary -  locals - extra local variables used when evaluating Sage code in x. -  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:: \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: 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 -  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. \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:: 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: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') 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 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. 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"""