Printing expressions in various formats
Basic usage
With basic use of Cadabra, you will typically display your expressions by postfixing them with a semi-colon, as inex:=A_{m n} ( B^{n} + 3 C^{n} );
\(\displaystyle{}A_{m n} \left(B^{n}+3C^{n}\right)\)
A_{m n} (B^{n} + 3C^{n})
What happens behind the scenes is that the semi-colon gets translated to a call of
display
on the
last-entered expression. It is therefore equivalent to display(ex)
\(\displaystyle{}A_{m n} \left(B^{n}+3C^{n}\right)\)
A_{m n} (B^{n} + 3C^{n})
If you do not want to display the expression, post-fix with a colon, as in
ex:=A_{m n} ( B^{n} + 3 C^{n} ):
If you want to display an expression again later, you can just write the name of the expression followed
by a semi-colon, or use the
display
function again,ex;
display(ex)
\(\displaystyle{}A_{m n} \left(B^{n}+3C^{n}\right)\)
A_{m n} (B^{n} + 3C^{n})
\(\displaystyle{}A_{m n} \left(B^{n}+3C^{n}\right)\)
A_{m n} (B^{n} + 3C^{n})
Note that while it may be tempting to use
print(ex)
, the display
function is better because
it knows about the capabilities of the interface used, and it will automatically select a text output when
you use Cadabra from the terminal, or LaTeX output when you use it in the graphical notebook.Other output formats
Cadabra expressions are standard Python objects, and as such they have a__str__
method which
converts them into a printable expression, and a __repr__
method to produce a machine readable form.
These are called by the standard str
and repr
Python functions, as the examples below show.print(str(ex))
A_{m n} (B^{n} + 3C^{n})
print(repr(ex))
\prod(A_{m n})(\sum(B^{n})(3C^{n}))
In addition there are some methods to obtain output useful in other software: Mathematica, LaTeX and Sympy:
print(ex.mma_form())
A[DNm, DNn]*(B[UPn]+3*C[UPn])
print(ex._latex_())
A_{m n} \left(B^{n}+3C^{n}\right)
print(ex.sympy_form())
A(DNm, DNn)*(B(UPn)+3*C(UPn))
Printing custom LaTeX
If you want to make sure that a string which you have created "by hand" will be processed by LaTeX and display in typeset form, use the `LaTeXString
` object. This is essentially a normal
string, but "tagged" with the information that it contains LaTeX code.s = LaTeXString(r"\int_{-\infty}^\infty \exp\left[ -a x^2 \right] {\rm d} x");
\(\displaystyle{}\int_{-\infty}^\infty \exp\left[ -a x^2 \right] {\rm d} x\)