LaTeX is a typesetting language for producing scientific documents. We introduce a very small part of the language for writing mathematical notation. Jupyter notebook recognizes LaTeX code written in markdown cells and renders the symbols in the browser using the MathJax JavaScript library.
Mathematics Inline and Display
Jupyter Book uses MathJax for typesetting math in your HTML book build. This allows you to have LaTeX-style mathematics in your online content. This page shows you a few ways to control this. It provides a very easy-to-use interface and lots of other functionalities like markdown, latex, inline plots, etc. Apart from these, it even provides a list of useful magic commands which let us perform a bunch of tasks from the jupyter notebook itself which developers need to do in the command prompt/shell.
Enclose LaTeX code in dollar signs $ ... $
to display math inline. For example, the code $int_a^b f(x) = F(b) - F(a)$
renders inline as $ int_a^b f(x) dx = F(b) - F(a) $.
Enclose LaTeX code in double dollar signs $$ ... $$
to display expressions in a centered paragraph. For example:
renders as
$$f'(a) = lim_{x to a} frac{f(x) - f(a)}{x-a}$$
See the LaTeX WikiBook for more information (especially the section on mathematics).
Common Symbols
Below we give a partial list of commonly used mathematical symbols. Most other symbols can be inferred from these examples. See the LaTeX WikiBook (Mathematics) and the Detexify App to find any symbol you can think of!
Syntax | Output |
---|---|
$x_n$ | $x_n$ |
$x^2$ | $x^2$ |
$infty$ | $infty$ |
$frac{a}{b}$ | $frac{a}{b}$ |
$partial$ | $partial$ |
$alpha$ | $alpha$ |
$beta$ | $beta$ |
$gamma$ | $gamma$ |
$Gamma$ | $Gamma$ |
$Delta$ | $Delta$ |
$sin$ | $sin$ |
$cos$ | $cos$ |
$tan$ | $tan$ |
$sum_{n=0}^{infty}$ | $sum_{n=0}^{infty}$ |
$prod_{n=0}^{infty}$ | $prod_{n=0}^{infty}$ |
$int_a^b$ | $int_a^b$ |
$lim_{x to a}$ | $lim_{x to a}$ |
$mathrm{Hom}$ | $mathrm{Hom}$ |
$mathbf{v}$ | $mathbf{v}$ |
$mathbb{Z}$ | $mathbb{Z}$ |
$mathscr{L}$ | $mathscr{L}$ |
$mathfrak{g}$ | $mathfrak{g}$ |
$dots$ | $dots$ |
$vdots$ | $vdots$ |
$ddots$ | $ddots$ |
Matrices and Brackets
Create a matrix without brackets:
$$begin{matrix} a & b c & d end{matrix}$$
Create a matrix with round brackets:
$$begin{pmatrix} a & b c & d end{pmatrix}$$
Create a matrix with square brackets:
$$begin{bmatrix}1 & 2 & 1 3 & 0 & 1 0 & 2 & 4end{bmatrix}$$
Use left
and right
to enclose an arbitrary expression in brackets:
$$left( frac{p}{q} right)$$
Examples
Derivative
The derivative $f'(a)$ of the function $f(x)$ at the point $x=a$ is the limit
$$f'(a) = lim_{x to a} frac{f(x) - f(a)}{x - a}$$
Continuity
A function $f(x)$ is continuous at a point $x=a$ if
$$lim_{x to a^-} f(x) = f(a) = lim_{x to a^+} f(x)$$
MacLaurin Series
The MacLaurin series for $e^x$ is
$$e^x = sum_{k=0}^{infty} frac{x^k}{k!}$$
Jacobian Matrix
The Jacobian matrix of the function $mathbf{f}(x_1, dots, x_n)$ is
$$mathbf{J} = frac{d mathbf{f}}{d mathbf{x}} =left[ frac{partial mathbf{f}}{partial x_1}cdots frac{partial mathbf{f}}{partial x_n} right] =begin{bmatrix}frac{partial f_1}{partial x_1} & cdots & frac{partial f_1}{partial x_n} vdots & ddots & vdots frac{partial f_m}{partial x_1} & cdots & frac{partial f_m}{partial x_n}end{bmatrix}$$
Exercises
Write LaTeX code to display the angle sum identity
$$cos(alpha pm beta) = cos alpha cos beta mp sin alpha sin beta$$
Write LaTeX code to display the indefinite integral
$$int frac{1}{1 + x^2} , dx = arctan x + C$$
Write LaTeX code to display the Navier-Stokes Equation for Incompressible Flow
$$frac{partial mathbf{u}}{partial t} + (mathbf{u} cdot nabla) mathbf{u} - nu nabla^2 mathbf{u} = - nabla w + mathbf{g}$$
Write LaTeX code to display Green's Theorem
$$oint_C (L dx + M dy) = iint_D left( frac{partial M}{partial x} - frac{partial L}{partial y} right) dx , dy$$
Write LaTeX code to display the Prime Number Theorem
$$lim_{x to infty} frac{pi(x)}{ frac{x}{log(x)}} = 1$$
Write LaTeX code to display the general formula for Taylor series
$$sum_{n=0}^{infty} frac{f^{(n)}(a)}{n!} (x-a)^n$$
Write LaTeX code to display Stokes' Theorem
$$int_{partial Omega} omega = int_{Omega} d omega$$
Write LaTeX code to display the adjoint property of the tensor product
$$mathrm{Hom}(U otimes V,W) cong mathrm{Hom}(U, mathrm{Hom}(V,W))$$
Write LaTeX code to display the definition of the Laplace transform
$$mathscr{L} { f(t) } = F(s) = int_0^{infty} f(t) e^{-st} dt$$
Write LaTeX code to display the inverse matrix formula
$$begin{bmatrix} a & b c & d end{bmatrix}^{-1} = frac{1}{ad - bc} begin{bmatrix} d & -b -c & a end{bmatrix}$$
Write LaTeX code to display the infinite product formula
$$sin x = x prod_{n=1}^{infty} left( 1 - frac{x^2}{pi^2 n^2} right)$$
Pick your favourite math course and write the notes from your last class in LaTeX.
Jupyter Book uses MathJax for typesetting math in your HTML book build.This allows you to have LaTeX-style mathematics in your online content.This page shows you a few ways to control this.
See also
For more information about equation numbering,see the MathJax equation numbering documentation.
Tip
By default MathJax version 2 is currently used.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%:
See the Sphinx documentation for details.
In-line math¶
To insert in-line math use the $
symbol within a Markdown cell.For example, the text $this_{is}^{inline}$
will produce: (this_{is}^{inline}).
Math blocks¶
You can also include math blocks for separate equations. This allows you to focus attentionon more complex or longer equations, as well as link to them in your pages. To use a blockequation, wrap the equation in either $$
or begin
statements.
For example,
results in:
Latex-style math¶
You can enable parsing LaTeX-style math blocks with the amsmath
MyST extension. Enable it by adding the following to _config.yml
Once enabled, you can define math blocks like so:
which results in:
Latex In Jupyter Notebook Tutorial
See also
The MyST guides to dollar math syntax, LaTeX math syntax, and how MyST-Parser works with MathJax.
For advanced use, also see how to define MathJax TeX Macros.
Numbering equations¶
If you’d like to number equations so that you can refer to them later, use the math directive.It looks like this:
For example, the following code:
will generate
Alternatively you can use the dollar math syntax with a prefixed label:
which generates
Install Latex For Jupyter Notebook
Note
Labels cannot start with an integer, or they won’t be able to be referenced andwill throw a warning message if referenced. For example, :label:1
and :label:1eq
cannotbe referenced.
Linking to equations¶
If you have created an equation with a label, you can link to it from within your text(and across pages!).
You can refer to the equation using the label that you’ve provided by usingthe {eq}
role. For example:
Jupyter Notebook Command Line
results in
A link to an equation directive: (3)
A link to a dollar math block: (4)
Latex On Jupiter Notebooks
Note
Latex Include Jupyter Notebook
labels
inside LaTeX environment are not currently identified, and so cannot be referenced.We hope to implement this in a future update (see executablebooks/MyST-Parser#202)!