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.

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=displaymath ], [tiny=False], [center=False])

Compute a latex representation of each object in objects, compile, and display using xdvi. (Requires latex and xdvi be installed.)

NOTE: In notebook mode this function simply embeds a png image in the output and doesn't do any of the following.

Input:

objects
- list (or object)
title
- string
zoom
- zoom factor, passed on to xdvi
expert
- bool (True or False): mode passed onto xdvi
debug
- bool (True of False): print verbose output
sep
- string - controls separators for math objects
tiny
- bool (default: False) use tiny font.
center
- bool (default: False) center

Output: Pops up xdvi with the objects displayed.

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.