Democratizing Data
Udacity's Data Blog

A Primer on Using LaTeX in Jupyter Notebooks

Mat Leonard |

Jupyter Notebooks have become one of the dominant tools for data scientists. Notebooks allow you to have code, text, math notation, and images all in the same document. They are also easily shareable so others can repeat the analysis, such as detecting gravitational waves with LIGO. Notebooks have also been used as entire online textbooks.

One of my favorite features is using LaTeX (pronounced Lah-Tek) to include mathematical notation directly in the text, rendered with MathJax. If you haven't encountered it before, LaTeX is the standard for typesetting technical and scientific documents. With LaTeX, you can write any document but it also includes the capability to write out mathematical notation. MathJax uses the LaTeX symbols to render math directly in the browser.

This is an extremely useful feature of Jupyter notebooks, but many people lack the LaTeX experience to take advantage of it. So here I’m going to provide a primer on writing mathematical notation in LaTeX. This won’t cover everything LaTeX can do (apparently it’s Turing complete, instead I’ll give you the basics so you can get started including math notation in your notebooks.

Math typesetting basics

There are two modes for writing LaTeX in notebooks, inline and display. Inline expressions are rendered inline with text, while displayed expressions are on their own lines. Inline mode look like this: $e^{i\pi} + 1 = 0$ and display mode look like

$$e^x = \sum_{n=0}^\infty \frac{x^n}{n!}$$

The math is rendered differently depending on the mode, see the same equation as above as an inline expression: $e^x = \sum_{n=0}^\infty \frac{x^n}{n!}$

To write math inline, you use single dollar signs like this $y=mx+b$ which results in $y=mx+b$. Everything between the dollar signs is rendered as mathematical notation. To create a display expression, use two dollar signs $$P(A\mid B) = \frac{P(B\mid A)P(A)}{P(B)}$$ which looks like $$P(A\mid B) = \frac{P(B\mid A)P(A)}{P(B)}$$

Superscript and Subscript

Superscript for exponents is done with a caret, $x^2$ gives $x^2$. Sometimes you'll want multiple things in the exponent and you might try $e^2x$ but that does this $e^2x$. Instead, wrap the exponent in braces: $e^{2x}$ renders as $e^{2x}$. You can similarly use subscript text using an underscore, x_i is $x_i$. The underscore can be used without a leading character too: _{10}C_5 is $_{10}C_{5}$

Commands

Special symbols and formattings are written using commands. Each command starts with a backslash, followed by the command name. For example, to create a square root radical, $\sqrt{2\pi}$ gives $\sqrt{2\pi}$. The braces {} include the contents under the radical and \pi renders as the Greek letter pi ($\pi$). Displaying the fraction above is done with the \frac command. It takes two inputs, one for the numerator and one for the denominator each enclosed in braces.

Symbols

Symbols are written written with a backslash then the symbol name. There's a huge number of symbols so this will just be a brief listing.

  • For Greek letters use \alpha, \beta, \gamma: $\alpha$, $\beta$, $\gamma$. You can do uppercase too: \Phi, \Lambda, \Gamma: $\Phi$, $\Lambda$, $\Gamma$. Note that uppercase beta is just $\mathrm{B}$, so there is no \Beta.

  • Operators like multiplication and convolution, \times,\pm, \cup, \oplus: $\times$, $\pm$, $\cup$, $\oplus$

  • Trigonometry functions like \sin, \cosh, \arctan: $\sin$, $\cosh$, $\arctan$. You want to use these commands instead of just writing sin, otherwise you get $sin(kx - \omega t)$ instead of $\sin(kx - \omega t)$

  • Relations such as \leq, \geq, \approx, \neq: $\leq$, $\geq$, $\approx$, $\neq$

  • Triple dots like \cdots, \ldots, \ddots: $\cdots$, $\ldots$, $\ddots$

  • Other various symbols like: \infty, \nabla, \partial: $\infty$, $\nabla$, $\partial$

Here's a list of symbols available in MathJax.

Accents

  • Put a hat on it: \hat x gives $\hat x$. To put a hat over multiple letters, use \widehat{abs}: $\widehat{abs}$
  • For means, you'd typically put a bar over the variable. Use \bar x: $\bar x$. For a long bar over multiple letters use \overline{abs}: $\overline{abs}$
  • Derivatives use dots like \dot x and \ddot x: $\dot x$, $\ddot x$
  • Arrows: \vec{x}, \overrightarrow{xy}: $\vec{x}$, $\overrightarrow{xy}$

Parentheses, Brackets, Braces

Parentheses and other brackets aren't "stretchy" by default. That is, they don't stretch out to the full height of the contents as with z = (\frac{dx}{dy})^{1/3}:

$$ z = (\frac{dx}{dy})^{1/3} $$

To make brackets stretchy, use \left and \right, like this $$z = \left(\frac{dx}{dy}\right)^{1/3}$$

$$ z = \left(\frac{dx}{dy}\right)^{1/3} $$

There are some special brackets you can create that aren't on your keyboard.

  • Vertical lines with | or \vert like so $|x|$ or $\vert x \vert$. Use \mid for lines between brackets. $P(A\vert B)$ uses \vert, while $P(A\mid B)$ is P(A\mid B)
  • Angle brackets: $\langle \phi \mid \psi \rangle$ gives $\langle \phi \mid \psi \rangle$
  • Group brackets for matrices (also, hey, here's how to make a matrix):

\left\lgroup \matrix{a & b\cr c & d} \right\rgroup

$$ \left\lgroup \matrix{a & b\cr c & d} \right\rgroup $$

Typefaces and font options

  • For non-italicized Roman text, use \textrm{Roman} or \rm Roman: $\textrm{Roman}$ or $\rm Roman$

  • To change the font size: \rm\tiny tiny \Tiny Tiny \small small \normalsize normal \large lg \Large Lg \LARGE LG \huge hg \Huge Hg

$$ \rm\tiny tiny \Tiny Tiny \small small \normalsize normal \large lg \Large Lg \LARGE LG \huge hg \Huge Hg $$
  • Boldface \mathbf: $\mathbf{abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
  • Italics \mathit: $\mathit{abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
  • Sans serif \mathsf: $\mathsf{abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
  • Typewriter \mathtt: $\mathtt{abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
  • Calligraphic \mathcal: $\mathcal{abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
  • Blackboard bold \mathbb: $\mathbb{abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ}$

Spaces

Spaces between symbols are removed from LaTeX formulas: \int f(x) dx looks like $\int f(x) dx$. There is no space between the function and $dx$. You can add spaces with \,, \:, and \; for small, medium, and large spaces respectively. Adding a bit of space, \int f(x)\,dx gives $\int f(x)\,dx$.

For larger spaces, use \quad and \qquad. The first is a 1em space $x\quad y$, the second is a 2 em space $x\qquad y$

Escaping characters

Sometimes you'll want to use special characters like { and _ that don't normally render in LaTeX. You need to escape them with an extra backslash. \{, \_, \$ for $\{$, $\_$, $\$$. For an actual backlash, use `\backslash` to get $\backslash$ because \\ is used for a new line.

Aligned equations

One thing I find useful is aligning multiple equations on successive lines. You can start a new line with \\ like so:

$$
a_1 = b_1 + c_1 \\
a_2 = b_2 + c_2 + d_2
$$
$$ a_1 = b_1 + c_1 \\ a_2 = b_2 + c_2 + d_2 $$

The equal signs don't line up though which can effect readability. Instead of $$, start the display expression with \begin{align} and end it with \end{align}. Then put an ampersand (&) where you want the equations to line up:

\begin{align}
a_1 & = b_1 + c_1 \\
a_2 & = b_2 + c_2 + d_2
\end{align}
\begin{align} a_1 & = b_1 + c_1 \\ a_2 & = b_2 + c_2 + d_2 \end{align}

Finishing up

Here's a comprehensive list of all symbols and commands available in MathJax (it's BIG). If you know what symbol you want, but don't know what it's called, try drawing it out with detexify.