How to Write LaTeX in IPython Notebook (Jupyter)

Updated 5 min read

Quick answer

To write LaTeX in a Jupyter (IPython) notebook, switch a cell to Markdown and wrap the maths in dollar signs: single dollars for inline maths and double dollars for display equations. Jupyter renders them with MathJax when you run the cell. From Python code, use IPython.display.Math to show a LaTeX string as an equation.

Jupyter doesn’t run real LaTeX. It renders the maths part with MathJax, so equations work, but packages and text commands don’t. This guide shows what works and what to use instead.

What you’ll learn

Set up Jupyter

Install JupyterLab or Notebook with pip (pip install notebook, then jupyter notebook), or through Anaconda. The Jupyter install page has all options.

The steps below also work in JupyterLab, VS Code notebooks and Google Colab text cells.

Write LaTeX in Markdown cells

Use this for equations in the explanatory text of a notebook.

  1. Add a cell and change its type from Code to Markdown in the toolbar drop-down, or press M in command mode.
  2. Type your text and maths.
  3. Run the cell with Shift + Enter to render it.

For inline maths, wrap the formula in single dollar signs. The famous equation is $E = mc^2$. renders as:

Rendered Markdown cell reading The famous equation is E equals m c squared, with the formula inline

The quadratic formula is $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$. renders as:

Inline quadratic formula x equals minus b plus or minus the square root of b squared minus 4ac, over 2a

For a display equation on its own line, use double dollar signs. $$\int_a^b f(x) \,dx = F(b) - F(a)$$ renders as:

Centred display equation: the integral from a to b of f of x dx equals F of b minus F of a

$$\sum_{i=1}^{n} i = \frac{n(n + 1)}{2}$$ renders as:

Centred display equation: the sum from i equals 1 to n of i equals n times n plus 1 over 2

Result: typeset equations inside your notebook text.

What works in a Markdown cell

You writeWorks?Use instead
$...$ and $$...$$ mathsYes
\begin{aligned}...\end{aligned}, cases, matrix inside $$Yes, MathJax supports them
\text{...}, \mathbf{...}, \textbf{...} inside $...$Yes
\textbf{...}, \textit{...} outside mathsNo, shown literallyMarkdown **bold** and *italic*
\small, \large, \huge outside mathsNo, shown literallyMarkdown headings
\usepackage{...}, \documentclassNoNot available

MathJax only processes maths, so text-mode LaTeX outside dollar signs stays as plain text. Those commands do work in a real LaTeX document; see how to change the font size in LaTeX. The MathJax TeX macro list shows every supported command.

Inside maths, text commands do work. $$\textbf{This text is bold}$$ renders as:

The words This text is bold in bold, rendered by MathJax inside display maths

and $$\textit{This text is italic}$$ as:

The words This text is italic in italics, rendered by MathJax inside display maths

For text labels inside an equation, use \text{}. $$\text{Area} = \pi r^2$$ renders as:

Display equation with the upright word Area, then equals pi r squared

Warning: Words typed in maths mode without \text{} lose their spaces and turn italic. For example, $${\large{Important Note:}} \textit{...}$$ renders the note as squashed italic letters:

Rendered output where Important Note appears as spaced-out italic maths letters without a word space, followed by a smaller italic sentence

Multi-line equations and custom commands

Use this for derivations and repeated expressions.

MathJax understands most amsmath environments. Inside $$, use aligned for aligned steps:

$$
\begin{aligned}
f(x) &= (x+1)^2 \\
&= x^2 + 2x + 1
\end{aligned}
$$

Nesting align inside $$ can raise an “Erroneous nesting” error, so prefer aligned there. See multi-line equations with align for how the environments differ.

Common commands:

  • Fractions: \frac{a}{b}
  • Roots: \sqrt{x} and \sqrt[3]{x}
  • Sums and integrals: \sum_{i=1}^{n}, \int_0^1 x^2 \,dx
  • Greek letters: \alpha, \beta, \gamma
  • Arrows and operators: \rightarrow, \leftarrow, \pm, \times, \div
$$
\frac{1}{2}, \quad \sqrt{16}, \quad \sum_{i=1}^{5} i = 1 + 2 + 3 + 4 + 5, \quad \int_0^1 x^2 \,dx, \quad \alpha, \quad \beta, \quad \gamma
$$

Single display line showing one half, the square root of 16, a sum from 1 to 5, an integral from 0 to 1 of x squared, and the Greek letters alpha, beta and gamma

To define your own command, put \newcommand inside dollar signs so MathJax processes it. The definition then works in later cells:

$\newcommand{\force}{F = \frac{G m_1 m_2}{r^2}}$
$$\force$$

Display equation F equals G m1 m2 over r squared, produced by the custom force command

Display LaTeX from Python code

Use this to show calculated results as formatted maths in a code cell’s output.

IPython.display provides Math for a single formula and Latex for LaTeX with its own delimiters. Use raw strings (r"...") so backslashes aren’t treated as Python escapes:

import sympy as sp
from IPython.display import display, Math
x = sp.symbols('x')
roots = sp.solve(x**2 - 3*x + 2, x)
display(Math(rf"x_1 = {sp.latex(roots[0])}, \quad x_2 = {sp.latex(roots[1])}"))

Result: the output shows x₁ = 1, x₂ = 2 as a typeset equation.

  • sp.latex(expr) turns any SymPy expression into a LaTeX string.
  • Run sp.init_printing() once and SymPy results display as typeset maths automatically.
  • The %%latex cell magic renders a whole code cell as LaTeX, within the subset MathJax supports.

The IPython.display reference lists the other display classes.

Export a notebook with LaTeX to PDF

Use this when you need to share or print the notebook.

  • Through LaTeX: jupyter nbconvert --to pdf notebook.ipynb. This needs a TeX installation such as TeX Live or MiKTeX, plus Pandoc.
  • Through a browser: jupyter nbconvert --to webpdf notebook.ipynb. No TeX needed; it requires the nbconvert[webpdf] extra, which installs a headless Chromium driver.
  • To Word: export the notebook to Markdown or LaTeX, then convert to Word with Pandoc.

Common errors

  • Raw $...$ shown: the cell is still a Code cell, or you haven’t run it.
  • Literal \textbf{...} shown: text-mode commands only work inside maths. Use Markdown formatting.
  • Dollar signs in prices break the text: escape them as \$ so MathJax doesn’t treat them as maths.
  • SyntaxWarning: invalid escape sequence: a LaTeX string in Python isn’t raw. Prefix it with r.
  • Maths not rendered on GitHub or in other viewers: they use their own renderers and may not support every command. Check the notebook in Jupyter itself.

Browse the LaTeX guides for more LaTeX formatting help.

Frequently asked questions

Why isn't my LaTeX rendering in Jupyter?

The cell must be a Markdown cell, the maths must be inside dollar signs, and you have to run the cell with Shift+Enter. LaTeX in a code cell is treated as Python.

Can I use \usepackage in a Jupyter notebook?

No. Jupyter renders maths with MathJax, which supports maths commands only. Packages, page layout and text-mode commands outside maths are not available.

How do I number equations in a Jupyter notebook?

Add \tag{1} inside a display equation, for example $$E = mc^2 \tag{1}$$, to place a number on the right.

How do I export a notebook with LaTeX to PDF?

Run jupyter nbconvert --to pdf notebook.ipynb, which needs a TeX installation, or use --to webpdf, which renders through a headless browser instead.

Does Google Colab support LaTeX?

Yes. Colab text cells render inline and display maths written between dollar signs, the same way as Jupyter Markdown cells.