# Ticket #11775: trac_11775-pretty_print_multiple_args.3.patch

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

Latest patch based on sage-4.7.2 (Updated to insert space)

• ## sage/misc/latex.py

# HG changeset patch
# User P Purkayastha <ppurka@gmail.com>
# Date 1323841229 -28800
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. 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:: 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. 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 \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)))
""" 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 -- bool (default: False): this can be used to display all the objects, passed as a tuple, as a single object. 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. -  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:: \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)
""" # 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: 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 - 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. \newcommand{\Bold}[1]{\mathbf{#1}}3 sage: view(3, mode='display')
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: if pdflatex is True: engine = "pdflatex" 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: 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"""