2.7.1 LaTeX Representation

Every object x in Sage should support the command latex(x), so that any Sage object can be easily and accurately displayed via LaTeX. Here is how to make a class and therefore its instances support the command latex.

  1. Put import sage.misc.latex as latex at the top of the file that defines your class.

  2. Define a member function _latex_(self) that returns a LaTeX representation of your object. It should be something that can be typeset correctly within math mode.

  3. Often objects are built up out of other options. For example if c is a coefficient of your object, and you want to typeset c using latex, use latex(c) instead of c._latex_(), since c might not have a _latex_ method, and latex(c) knows how to deal with this.

  4. Don't forget to include a docstring and an example that illustrates latex generation for your object.

  5. You can use any macros included in amsmath, amssymb, amsfonts or the ones defined in SAGE_ROOT/doc/commontex/macros.tex.

  6. Use view(x) to view the typeset version of an object x.

An example template for LaTeX representation follows:

import sage.misc.latex as latex
...
class X:
   ...
   def _latex_(self):
       """
       Return \Latex representation of X.
 
       EXAMPLES:
           sage: a = X(1,2)
           sage: latex(a)
           `\\frac{1}{2}'
       """
       return `\\frac{%s}{%s}''%(latex(self.numer), latex(self.denom))

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