# Ticket #11775: trac_11775-pretty_print_multiple_args_mathjax.patch

File trac_11775-pretty_print_multiple_args_mathjax.patch, 8.8 KB (added by ppurka, 10 years ago)

Apply only to mathjax-ified sage{nb}

• ## sage/misc/latex.py

# HG changeset patch
# User Punarbasu Purkayastha <ppurka@gmail.com>
# Date 1330189086 -28800
# Node ID 7c0237f6509d7cff55c14b4676de3c844a85f643
# Parent  0bae00abf2731eb6ed239d8781ef86ecc202b59d
make pretty_print accept multiple arguments

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) '1 2 3' sage: tuple_function(((1,2),3), combine_all=True) '\\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) 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 MathJax.  This returns a :class:MathJaxExpr. - 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 MathJaxExpr EXAMPLES:: sage: str(MathJax().eval(ZZ[x], mode='display')) == str(MathJax()(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 MathJax.  This returns a :class:MathJaxExpr. -  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 MathJaxExpr EXAMPLES:: sage: MathJax().eval(type(3), mode='inline') sage: MathJax().eval((1,3), mode='display', combine_all=True) """ # Get a regular LaTeX representation of x... x = latex(x) x = latex(x, combine_all=combine_all) # ... and make it suitable for MathJax and html x = x.replace('<', '<').replace('>', '>') # In MathJax: + _Latex_prefs._option['macros'] + '%s'%x) 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. sage: view(3, mode='display') sage: view((x,2), combine_all=True) sage: sage.misc.latex.EMBEDDED_MODE = False TESTS:: MathJax_okay = False if not MathJax_okay: break if MathJax_okay: print MathJax().eval(objects, mode=mode)  # put comma at end of line? if MathJax_okay:  # put comma at end of line in print below? print MathJax().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: pretty_print(ZZ)  # indirect doctest sage: pretty_print("Integers = ", ZZ) To typeset LaTeX code as-is, use :class:LatexExpr:: sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}")) """ 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 MathJax().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 MathJax().eval(object, mode='inline') return s += [object] _show_s(s) return def pretty_print_default(enable=True): r"""