# 4 SymPy and LaTeX

First, import the SymPy package.

import sympy

[1] 2019-05-15 10:03:14 (5.02ms) python3 (2.18s)

## 4.1 Basic usage

SymPy symbols or expressions in a fenced code block are automatically rendered in display mode:

x = sympy.Symbol('x')
x**3

[2] 2019-05-15 10:03:14 (12.0ms) python3 (2.20s)

$$x^{3}$$

On the other hand, in an inline code, a SymPy object just returns a latex string like this {{x**2}} = x^{2}. This is intentional behavior. You can choose inline mode or display mode.

This is an inline mode example: ${{x**2+1/x+1}}$

This is an inline mode example: $x^{2} + 1 + \frac{1}{x}$

This is a display mode example:

$${{x**2+1/x+1}}$$

This is a display mode example:

x^{2} + 1 + \frac{1}{x}

You can concatenate SymPy objects and/or normal latex source in the same line to make an expression form you prefer:

$${{x**2}} - {{x}} + {{x}} + \frac1y$$
x^{2} - x + x + \frac1y

Here, $-x+x$ does not cancel out automatically.

## 4.2 LatexPrinter

You can pass keyward arguments to LatexPrinter by "commented filter" notation.

fold_short_frac option: Emit p/q instead of \frac{p}{q}

Example: ${{1/x}}$ vs. ${{1/x # fold_short_frac=True}}$

fold_short_frac option: Emit p/q instead of \frac{p}{q}

Example: $\frac{1}{x}$ vs. $1 / x$

In a fenced code block, you can use normal option notation:

python fold_frac_powers=True
x**sympy.Rational(2, 3)

x**sympy.Rational(2, 3)

[13] 2019-05-15 10:03:14 (13.0ms) (fold_frac_powers=True) python3 (2.32s)

$$x^{2/3}$$

## 4.3 Numbering

Pheasant uses MathJax's Automatic Equation Numbering. Use the custom header syntax like the figure and table. Also, you can add a tag for link.

#Eq f(x) = {{x**2}} {#eq-a#}

$$f(x) = x^{2} \label{eq-a}$$

Using starred form, the equation won’t be numbered like original LaTeX.

#Eq* f(x) = {{x**2}}

\begin{equation*} f(x) = x^{2} \end{equation*}

As usual, you can refer to equation: See Eq. {#eq-a#}

As usual, you can refer to equation: See Eq. \eqref{eq-a}

Also, you can use native latex syntax. From MathJax document:

In equation \eqref{eq:sample}, we find the value of an interesting integral:

#Eq \int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15} \label{eq:sample}

In equation \eqref{eq:sample}, we find the value of an interesting integral:

$$\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15} \label{eq:sample}$$