12.8 Latex printing support

Module: sage.misc.latex

Latex printing support

In order to support latex formating, an object should define a special method _latex_(self) that returns a string.

Module-level Functions

coeff_repr( c)

have_dvipng( )

jsmath( x, [mode=display])

Attempt to nicely render an arbitrary SAGE object wih jsmath typesetting. Tries to call ._latex_() on x. If that fails, it will render a string representation of x.

Input:

x
- the object to render
mode
- 'display' for displaymath or 'inline' for inline math

Output: A string of html that contains the LaTeX represntation of x. In the notebook this gets embedded into the cell.

sage: f = maxima('1/(x^2+1)')
sage: g = f.integrate()
sage: jsmath(f)
 <html><div class="math">\frac{1}{x^2+1}</div></html>
sage: jsmath(g, 'inline')
 <html><span class="math">\tan^{-1} x</span></html>
sage: jsmath('\int' + latex(f) + '\ dx=' + latex(g))
 <html><div class="math">\int\frac{1}{x^2+1}\ dx=\tan^{-1} x</div></html>

Author Log:

latex( x)

latex_variable_name( x)

Return latex version of a variable name.

Here are some guiding principles for usage of this function: 1) If the variable is a single letter, that is the latex version. 2) If the variable name is suffixed by a number, we put the number in the subscript. 3) If the variable name contains an '_' we start the subscript at the underscore. Note that #3 trumps rule #2. 4) If a component of the variable is a greek letter, escape it properly. 5) Recurse nicely with subscripts.

Refer to the examples section for how these rules might play out in practice.

sage: import sage.misc.latex as latex_module
sage: latex_variable_name = latex_module.latex_variable_name
sage: latex_variable_name('a')
'a'
sage: latex_variable_name('abc')
'\\mbox{abc}'
sage: latex_variable_name('sigma')
'\\sigma'
sage: latex_variable_name('sigma_k')
'\\sigma_{k}'
sage: latex_variable_name('sigma389')
'\\sigma_{389}'
sage: latex_variable_name('beta_00')
'\\beta_{00}'
sage: latex_variable_name('Omega84')
'\\Omega_{84}'
sage: latex_variable_name('sigma_alpha')
'\\sigma_{\\alpha}'
sage: latex_variable_name('nothing1')
'\\mbox{nothing}_{1}'
sage: latex_variable_name('nothing_abc')
'\\mbox{nothing}_{\\mbox{abc}}'
sage: latex_variable_name('alpha_beta_gamma12')
'\\alpha_{\\beta_{\\gamma_{12}}}'

Author Log:

latex_varify( a)

png( x, filename, [density=150], [debug=False], [brk=0], [do_in_background=True], [tiny=False])

Create a png image representation of x and save to the given filename.

pretty_print( object)

Try to pretty print the object in an intelligent way. For many things, this will convert the object to latex inside of html and rely on a latex-aware front end (like jsMath) to render the text

pretty_print_default( [enable=True])

Enable or disable default pretty printing. Pretty printing means rendering things so that jsMath or some other latex-aware front end can render real math.

print_or_typeset( object)

'view' or 'print' the object depending on the situation.

In particular, if in notebook mode with the typeset box checked, view the object. Otherwise, print the object.

Input: object: anything

sage: sage.misc.latex.print_or_typeset(3)
3
sage: sage.misc.latex.EMBEDDED_MODE=True
sage: sage.misc.latex.print_or_typeset(3)
3
sage: sys.displayhook = sage.misc.latex.pretty_print
sage: sage.misc.latex.print_or_typeset(3)
<html><span class="math">3</span></html>
sage: sage.misc.latex.EMBEDDED_MODE=False

repr_lincomb( symbols, coeffs)

Compute a latex representation of a linear combination of some formal symbols.

Input:

symbols
- list of symbols
coeffs
- list of coefficients of the symbols

Output:
str
- a string

sage: t = PolynomialRing(QQ, 't').0
sage: from sage.misc.latex import repr_lincomb
sage: repr_lincomb(['a', 's', ''], [-t, t - 2, t^12 + 2])
'-t\text{a} + \left(t - 2\right)\text{s} + \left(t^{12} + 2\right)\text{}'

typeset( x)

view( objects, [title=SAGE], [zoom=4], [expert=True], [debug=False], [sep=], [tiny=False])

Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed.

Input:

objects
- list (or object)
title
- string (default: 'SAGE'): title for the document
zoom
- zoom factor, passed on to xdvi if used
expert
- bool (default: True): mode passed on to xdvi
debug
- bool (default: False): print verbose output
sep
- string (default: ''): separator between math objects
tiny
- bool (default: False): use tiny font.

Output: Display typeset objects.

This function behaves differently depending on whether in notebook mode or not.

If not in notebook mode, this opens up a window displaying a dvi (or pdf) file, displaying the following: the title string is printed, centered, at the top. Beneath that, each object in objects is typeset on its own line, with the string sep typeset between these lines.

If the program xdvi is used to display the dvi file, then the values of expert and zoom are passed on to it. On OS X displays a pdf.

If in notebook mode, this uses jmath to display the output in the notebook. Only the first argument, objects, is relevant; the others are ignored. If objects is a list, the result is typeset as a Python list, e.g. [12, -3.431] - each object in the list is not printed on its own line.

sage: sage.misc.latex.EMBEDDED_MODE=True
sage: view(3)
<html><span class="math">3</span></html>
sage: sage.misc.latex.EMBEDDED_MODE=False

Class: JSMath

class JSMath
A simple object for rendering LaTeX input using JSMath.

Functions: eval

Special Functions: __call__

Class: JSMathExpr

class JSMathExpr
An arbitrary JSMath expression that can be nicely concatenated.
JSMathExpr( self, y)

Special Functions: __add__,$ \,$ __init__,$ \,$ __radd__,$ \,$ __repr__

Class: Latex

class Latex
nodetex Enter, e.g.,
        %latex
        The equation $y^2 = x^3 + x$ defines an elliptic curve.
        We have $2006 = \sage{factor(2006)}$.
in an input cell to get a typeset version (care of slitex). Use %latex_debug to get debugging output.

Use latex(...) to typeset a SAGE object.

Use %slide instead to typeset slides.

Latex( self, [debug=False], [slide=False], [density=150])

Functions: eval

eval( self, x, [strip=False], [filename=None], [debug=None], [density=None], [locals=])

Input:

x
- string to evaluate.
strip
- ignored
filename
- output filename
debug
- whether to print verbose debugging output
density
- how big output image is.
locals
- extra local variables used when evaluating Sage .. code in x.

Special Functions: __call__,$ \,$ __init__,$ \,$ _latex_preparse

Class: LatexExpr

class LatexExpr
LatexExpr( self, x)

Special Functions: __init__,$ \,$ __repr__,$ \,$ _latex_

See About this document... for information on suggesting changes.