1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
# Math and equations
Math can be written in the common LaTeX markup.
There are several syntaxes to define inline and block math,
depending on your preference.
## Math role and directive
The `math` {{role}} and {{directive}} are used to define inline and block math respectively.
The directive supports multiple equations, which should be separated by a blank line.
Each single equation can have multiple aligned lines, which should be separated by `\\` characters, and each line can have multiple `&` characters to align the equations.
The `label` option can also be used to reference the equation later on, with the `eq` role.
:::{myst-example}
Since Pythagoras, we know that {math}`a^2 + b^2 = c^2`.
```{math}
:label: mymath
(a + b)^2 = a^2 + 2ab + b^2
(a + b)^2 &= (a + b)(a + b) \\
&= a^2 + 2ab + b^2
```
The equation {eq}`mymath` is a quadratic equation.
:::
:::{seealso}
Sphinx documentation on the [role](inv:sphinx:rst:role#math) and [directive](inv:sphinx:rst:directive#math),
and [math support for HTML outputs in Sphinx](inv:sphinx#math-support).
:::
## Dollar delimited math
Enabling the [dollarmath](#syntax/math/dollar) extension will allow parsing the following syntax:
- Inline math: `$...$`
- Display (block) math: `$$...$$`
Additionally if `myst_dmath_allow_labels=True` is set (the default):
- Display (block) math with equation label: `$$...$$ (1)`
:::{myst-example}
:highlight: latex
$$
(a + b)^2 &= (a + b)(a + b) \\
&= a^2 + 2ab + b^2
$$ (mymath2)
The equation {eq}`mymath2` is also a quadratic equation.
:::
:::{admonition} Escaping Dollars
:class: tip dropdown
Math can be escaped (negated) by adding a `\` before the first symbol, e.g. `\$a$` renders as \$a\$.
Escaping can also be used inside math, e.g. `$a=\$3$` renders as $a=\$3$.
Conversely `\\` will negate the escaping, so `\\$a$` renders as \\$a$.
:::
## Direct LaTeX Math
Enabling the [amsmath](#syntax/amsmath) extension will directly parse the following top-level math environments:
> equation, multiline, gather, align, alignat, flalign, matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix, eqnarray.
As expected, environments ending in `*` will not be numbered, for example:
:::{myst-example}
:highlight: latex
\begin{gather*}
a_1=b_1+c_1\\
a_2=b_2+c_2-d_2+e_2
\end{gather*}
\begin{align}
a_{11}& =b_{11}&
a_{12}& =b_{12}\\
a_{21}& =b_{21}&
a_{22}& =b_{22}+c_{22}
\end{align}
:::
|