{
"cells": [
{
"cell_type": "markdown",
"id": "c1a19485",
"metadata": {},
"source": [
"(myst-content/math)=\n",
"# Math and equations\n",
"\n",
"Jupyter Book uses [MathJax](http://docs.mathjax.org/) for typesetting math in your HTML book build.\n",
"This allows you to have LaTeX-style mathematics in your online content.\n",
"This page shows you a few ways to control this.\n",
"\n",
":::{seealso}\n",
"For more information about equation numbering,\n",
"see the [MathJax equation numbering documentation](http://docs.mathjax.org/en/v2.7-latest/tex.html#automatic-equation-numbering).\n",
":::\n",
"\n",
":::{tip}\n",
"\n",
"By default MathJax version 2 is currently used.\n",
"If you are using a lot of math, you may want to try using version 3, which claims to improve load speeds by 60 - 80%:\n",
"\n",
"```yaml\n",
"sphinx:\n",
" config:\n",
" mathjax_path: https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js\n",
"```\n",
"\n",
"See the [Sphinx documentation](sphinx:sphinx.ext.mathjax) for details.\n",
"\n",
":::\n",
"\n",
"## In-line math\n",
"\n",
"To insert in-line math use the `$` symbol within a Markdown cell.\n",
"For example, the text `$this_{is}^{inline}$` will produce: $this_{is}^{inline}$."
]
},
{
"cell_type": "markdown",
"id": "9b14ded4",
"metadata": {},
"source": [
"## Math blocks\n",
"\n",
"You can also include math blocks for separate equations. This allows you to focus attention\n",
"on more complex or longer equations, as well as link to them in your pages. To use a block\n",
"equation, wrap the equation in either `$$` or `\\begin` statements.\n",
"\n",
"For example,\n",
"\n",
"```latex\n",
"$$\n",
" \\int_0^\\infty \\frac{x^3}{e^x-1}\\,dx = \\frac{\\pi^4}{15}\n",
"$$\n",
"```\n",
"\n",
"results in:\n",
"\n",
"$$\n",
" \\int_0^\\infty \\frac{x^3}{e^x-1}\\,dx = \\frac{\\pi^4}{15}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "dd81033c",
"metadata": {},
"source": [
"(math:latex)=\n",
"### Latex-style math\n",
"\n",
"You can enable parsing LaTeX-style math blocks with the `amsmath` MyST extension. Enable it by adding the following to `_config.yml`\n",
"\n",
"```yaml\n",
"parse:\n",
" myst_enable_extensions:\n",
" # don't forget to list any other extensions you want enabled,\n",
" # including those that are enabled by default!\n",
" - amsmath\n",
"```\n",
"\n",
"Once enabled, you can define math blocks like so:\n",
"\n",
"```latex\n",
"\\begin{gather*}\n",
"a_1=b_1+c_1\\\\\n",
"a_2=b_2+c_2-d_2+e_2\n",
"\\end{gather*}\n",
"\n",
"\\begin{align}\n",
"a_{11}& =b_{11}&\n",
" a_{12}& =b_{12}\\\\\n",
"a_{21}& =b_{21}&\n",
" a_{22}& =b_{22}+c_{22}\n",
"\\end{align}\n",
"```\n",
"\n",
"which results in:\n",
"\n",
"\\begin{gather*}\n",
"a_1=b_1+c_1\\\\\n",
"a_2=b_2+c_2-d_2+e_2\n",
"\\end{gather*}\n",
"\n",
"\\begin{align}\n",
"a_{11}& =b_{11}&\n",
" a_{12}& =b_{12}\\\\\n",
"a_{21}& =b_{21}&\n",
" a_{22}& =b_{22}+c_{22}\n",
"\\end{align}\n",
"\n",
":::{seealso}\n",
"The MyST guides to [dollar math syntax](myst-parser:syntax/math), [LaTeX math syntax](myst-parser:syntax/amsmath), and [how MyST-Parser works with MathJax](myst-parser:syntax/mathjax).\n",
"\n",
"For advanced use, also see how to [define MathJax TeX Macros](sphinx/tex-macros).\n",
":::"
]
},
{
"cell_type": "markdown",
"id": "141377b6",
"metadata": {},
"source": [
"### Numbering equations\n",
"\n",
"If you'd like to number equations so that you can refer to them later, use the **math directive**.\n",
"It looks like this:\n",
"\n",
"````md\n",
"```{math}\n",
":label: my_label\n",
"my_math\n",
"```\n",
"````\n",
"\n",
"For example, the following code:\n",
"\n",
"````md\n",
"```{math}\n",
":label: my_label\n",
"w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}\n",
"```\n",
"````\n",
"\n",
"will generate\n",
"\n",
"```{math}\n",
":label: my_label\n",
"w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}\n",
"```\n",
"\n",
"Alternatively you can use the dollar math syntax with a prefixed label:\n",
"\n",
"```md\n",
"$$\n",
" w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}\n",
"$$ (my_other_label)\n",
"```\n",
"\n",
"which generates\n",
"\n",
"$$\n",
" w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}\n",
"$$ (my_other_label)\n",
"\n",
":::{note}\n",
"Labels cannot start with an integer, or they won't be able to be referenced and\n",
"will throw a warning message if referenced. For example, `:label: 1` and `:label: 1eq` cannot\n",
"be referenced.\n",
":::\n",
"\n",
"### Linking to equations\n",
"\n",
"If you have created an equation with a label, you can link to it from within your text\n",
"(and across pages!).\n",
"\n",
"You can refer to the equation using the label that you've provided by using\n",
"the `{eq}` role. For example:\n",
"\n",
"```md\n",
"- A link to an equation directive: {eq}`my_label`\n",
"- A link to a dollar math block: {eq}`my_other_label`\n",
"```\n",
"\n",
"results in\n",
"\n",
"- A link to an equation directive: {eq}`my_label`\n",
"- A link to a dollar math block: {eq}`my_other_label`\n",
"\n",
":::{note}\n",
"`\\labels` inside LaTeX environment are not currently identified, and so cannot be referenced.\n",
"We hope to implement this in a future update (see [executablebooks/MyST-Parser#202](https://github.com/executablebooks/MyST-Parser/issues/202))!\n",
":::"
]
}
],
"metadata": {
"jupytext": {
"text_representation": {
"extension": ".md",
"format_name": "myst"
}
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
},
"source_map": [
10,
44,
66,
117
],
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}