Math Rendering Examples

The examples start always with a code box showing the commands, which is followed by the resulting Sphinx output.

Inline math

Some inline math :math:`x_1 + x_2 + ... + x_n, n \in \mathbb{Z}`,
followed by text.

Some inline math \(x_1 + x_2 + ... + x_n, n \in \mathbb{Z}\), followed by text.

Macros

You can define macros directly in your math directive.

.. math::

    \def \x {\mathbf{x}}
    \def \w {\omega}
    \def \d {\operatorname{d}\!}

    P(\x,\w) = \oint_{\partial V} D(\x_0,\w) G(\x-\x_0,\w) \d A(\x_0)
\[\def \x {\mathbf{x}} \def \w {\omega} \def \d {\operatorname{d}\!} P(\x,\w) = \oint_{\partial V} D(\x_0,\w) G(\x-\x_0,\w) \d A(\x_0)\]

If you want to use them in the whole document, the best is to define them in conf.py as part of the katex_options, see LaTeX Macros. Afterwards, you can use them in every math directive.

Aligned environment

.. math::

    \begin{aligned}
        \dot{x} & = \sigma(y-x) \\
        \dot{y} & = \rho x - y - xz \\
        \dot{z} & = -\beta z + xy
    \end{aligned}
\[\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}\]

Array environment

.. math::

    \begin{array}{c:c:c:c:c:c}
        \Gamma & \Delta & \Theta & \Lambda & \Xi & \Pi \\ \hdashline
        \gamma & \delta & \theta & \lambda & \xi & \pi
    \end{array}
\[\begin{array}{c:c:c:c:c:c} \Gamma & \Delta & \Theta & \Lambda & \Xi & \Pi \\ \hdashline \gamma & \delta & \theta & \lambda & \xi & \pi \end{array}\]

Case definitions

.. math::

    f(n) = \begin{cases}
        \frac{n}{2}, & \text{if } n\text{ is even} \\
        3n+1,        & \text{if } n\text{ is odd}
    \end{cases}
\[ f(n) = \begin{cases} \frac{n}{2}, & \text{if } n\text{ is even} \\ 2n+1, & \text{if } n\text{ is odd} \end{cases}\]

Matrices

A simple matrix defined with the pmatrix environment:

.. math::

    \begin{pmatrix}
        a_{11} & a_{12} & a_{13}\\
        a_{21} & a_{22} & a_{23}\\
        a_{31} & a_{32} & a_{33}
    \end{pmatrix}
\[\begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{pmatrix}\]

The pmatrix* environment is not available, but you can use the array environment for more complex matrices:

.. math::

    \def \msum {-\textstyle\sum}
    \def \psum {\phantom{-}\textstyle\sum}
    I_{ik} = \left(
    \begin{array}{lll}
        \psum m (y^2+z^2) & \msum m x y       & \msum m x z         \\
        \msum m y x       & \psum m (x^2+z^2) & \msum m y z         \\
        \msum m z x       & \msum m z y       & \psum m (x^2 + y^2)
    \end{array}
    \right)
\[\def \msum {-\textstyle\sum} \def \psum {\phantom{-}\textstyle\sum} I_{ik} = \left( \begin{array}{lll} \psum m (y^2+z^2) & \msum m x y & \msum m x z \\ \msum m y x & \psum m (x^2+z^2) & \msum m y z \\ \msum m z x & \msum m z y & \psum m (x^2 + y^2) \end{array} \right)\]

Equation numbers

Some of Maxwell’s equations are given in (1), (2), and (3).

.. math::
    :label: gauss-law

    \nabla \cdot \mathbf{E} = 4 \pi \rho

.. math::
    :label: gauss-law-magnetism

    \nabla \cdot \mathbf{B} = 0

.. math::
    :label: maxwell-faraday-equation

    \nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t}
(1)\[\nabla \cdot \mathbf{E} = 4 \pi \rho\]
(2)\[\nabla \cdot \mathbf{B} = 0\]
(3)\[\nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t}\]

Fraction

.. math::

    1 - 2 \phi_{i,j} = \frac{4 N^{AA,aa}_{i,j}
                             + N^{Aa}_{i}
                             + N^{Aa}_{j}
                             - 2 N^{Aa,Aa}_{i,j}}
                            {\sum_{s \in S_{i,j}} 4 p_s (1 - p_s)}
\[1 - 2 \phi_{i,j} = \frac{4 N^{AA,aa}_{i,j} + N^{Aa}_{i} + N^{Aa}_{j} - 2 N^{Aa,Aa}_{i,j}} {\sum_{s \in S_{i,j}} 4 p_s (1 - p_s)}\]