See the Printing section in Tutorial for introduction into printing.
This guide documents the printing system in SymPy and how it works internally.
The main class responsible for printing is Printer (see also its source code):
Generic printer driver
This is a generic printer driver. It’s job is to provide infrastructure for implementing new printers easily.
Basically, if you want to implement a printer, all you have to do is:
Subclass Printer.
In your subclass, define _print_<CLASS> methods
For each class you want to provide printing to, define an appropriate method how to do it. For example if you want a class FOO to be printed in it’s own way, define _print_FOO:
...
this should return how FOO instance e is printed
Also, if BAR is a subclass of FOO, _print_FOO(bar) will be called for instance of BAR, if no _print_BAR is provided. Thus, usually, we don’t need to provide printing routines for every class we want to support – only generic routine has to be provided for a set of classes.
A good example for this are functions - for example PrettyPrinter only defines _print_Function, and there is no _print_sin, _print_tan, etc...
On the other hand, a good printer will probably have to define separate routines for Symbol, Atom, Number, Integral, Limit, etc...
If convenient, override self.emptyPrinter
This callable will be called to obtain printing result as a last resort, that is when no appropriate _print_<CLASS> was found for an expression.
internal dispatcher
It’s job is to loop through expr classes (class + it’s bases), and try to dispatch the work to _print_<EXPR_CLASS>
e.g., suppose we have the following class hierarchy:
Basic | Atom | Number | Rational
then, for expr=Rational(...), in order to dispatch, we will try calling printer methods as shown in the figure below:
p._print(expr) | |-- p._print_Rational(expr) | |-- p._print_Number(expr) | |-- p._print_Atom(expr) | `-- p._print_Basic(expr)
if ._print_Rational method exists in the printer, then it is called, and the result is returned back.
otherwise, we proceed with trying Rational bases in the inheritance order.
if nothing exists, we just return:
p.emptyPrinter(expr)
Pretty printing subsystem is implemented in sympy.printing.pretty.pretty by the PrettyPrinter class deriving from Printer. It relies on modules sympy.printing.pretty.stringPict, and sympy.printing.pretty.pretty_symbology for rendering nice-looking formulas.
The module stringPict provides a base class stringPict and a derived class prettyForm that ease the creation and manipulation of formulas that span across multiple lines.
The module pretty_symbology provides primitives to construct 2D shapes (hline, vline, etc) together with a technique to use unicode automatically when possible.
This class is responsible for MathML printing. See sympy.printing.mathml.
More info on mathml content: http://www.w3.org/TR/MathML2/chapter4.html
This class implements LaTeX printing. See sympy.printing.latex.
You can print to a grkmathview widget using the function print_gtk located in sympy.printing.gtk (it requires to have installed gtkmatmatview and libgtkmathview-bin in some systems).
GtkMathView accepts MathML, so this rendering depends on the mathml representation of the expression
Usage:
from sympy import *
print_gtk(x**2 + 2*exp(x**3))
This class implements Python printing. Usage:
In [1]: print_python(5*x**3 + sin(x))
x = Symbol('x')
e = sin(x) + 5*x**3
Useful function is preview:
View expression in PNG, DVI, PostScript or PDF form.
This will generate LaTeX representation of the given expression and compile it using available TeX distribiution. Then it will run appropriate viewer for the given output format or use the user defined one. If you prefer not to use external viewer then you can use combination of ‘png’ output and ‘pyglet’ viewer. By default PostScript output is generated.
By default pretty Euler fonts are used for typesetting (they were used to typeset the well known “Concrete Mathematics” book). If you prefer default AMS fonts or your system lacks ‘eulervm’ LaTeX package then unset ‘euler’ keyword argument.
To use viewer auto-detection, lets say for ‘png’ output, issue:
>> from sympy import *
>> x, y = symbols("xy")
>> preview(x + y, output='png')
This will choose ‘pyglet by default. To select different one:
>> preview(x + y, output='png', viewer='gimp')
The ‘png’ format is considered special. For all other formats the rules are sligtly different. As an example we will take ‘dvi’ output format. If you would run:
>> preview(x + y, output='dvi')
then ‘view’ will look for available ‘dvi’ viewers on your system (predefined in the function, so it will try evince, first, then kdvi and xdvi). If nothing is found you will need to set the viewer explicitely:
>> preview(x + y, output='dvi', viewer='superior-dvi-viwer')
This will skip auto-dection and will run user specified ‘superior-dvi-viwer’. If ‘view’ fails to find it on your system it will gracefully raise an exception.
And convenience functions:
Calls preview(expr, output=”png”, ...)
This is a convenience function. See the docstring of preview for more information.
Calls preview(expr, output=”pdf”, ...)
This is a convenience function. See the docstring of preview for more information.
Calls preview(expr, output=”dvi”, ...)
This is a convenience function. See the docstring of preview for more information.