Math¶
The math directive sets a LaTeX formula on a line of its own,
and the :math: role sets one inside a sentence.
ubCode typesets both with KaTeX, in the editor preview and in a built site;
a Sphinx build typesets them with MathJax.
ubc build pdf typesets display and inline maths with KaTeX’s own faces
(see Maths).
Display math¶
The formula can be the directive’s argument, its body, or both
(an argument is joined to the body with a blank line, as in Sphinx).
Blank-line-separated equations in one body are set as aligned rows,
and a formula containing a \\ line break is wrapped in a
split environment — matching what a Sphinx build produces.
Display math
.. math:: E = mc^2
.. math::
a^2 + b^2 = c^2
c = \sqrt{a^2 + b^2}
```{math} E = mc^2
```
```{math}
a^2 + b^2 = c^2
c = \sqrt{a^2 + b^2}
```
The MyST form renders live in the MyST display math.
The directive takes these options:
:label:/:name:Give the equation an identifier. In built output the block gets an
idand a hover ¶ permalink;:name:also registers a cross-reference target. A labelled equation is numbered, and the:eq:role refers to it by number (see Numbered equations).:no-wrap:(or the legacy:nowrap:)Pass the body to the typesetter verbatim — you supply the complete LaTeX environment (for example
\begin{aligned}…\end{aligned}), and no\[…\]wrapping is added.:class:Extra CSS classes to apply to the block.
A formula KaTeX cannot parse is left as its raw source, with the parse error shown in the tooltip.
Numbered equations¶
A math block with a :label: is numbered,
and :eq: (or :math:numref:, which reports nothing when the label is missing)
refers to it by its label, printing its number.
The label is matched exactly, and a title on the role is ignored.
How equations are numbered — per page or by chapter, the separator, the reference text —
is set in Numbering.
Inline math¶
The :math: role’s text is the LaTeX source, taken verbatim:
backslashes need no doubling inside the backticks.
Inline math
The roots of :math:`ax^2 + bx + c = 0`
are :math:`x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`.
The roots of \(ax^2 + bx + c = 0\) are \(x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\).
The roots of {math}`ax^2 + bx + c = 0`
are {math}`x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`.
The MyST form renders live in the MyST inline math.
Markdown’s own dollar math ($x$ and $$x$$) needs the dollarmath extension,
which this site does not enable (see MyST extensions).