3.2.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. Define a method _latex_(self) that returns a LaTeX representation of your object. It should be something that can be typeset correctly within math mode. Do not include opening and closing $'s.

  2. Often objects are built up out of other Sage objects, and these components should be typeset using the latex function. 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.

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

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

An example template for a _latex_ method follows:

class X:
   ...
   def _latex_(self):
       r"""
       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))

As shown in the example, latex(a) will produce LaTeX code representing the object a. Calling view(a) will display the typeset version of this.

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