LaTeX Math in Markdown

Markdown has no maths of its own. Renderers that support it borrow LaTeX syntax and hand it to a typesetting engine — here that engine is KaTeX, the same one GitHub uses.

Every formula below is rendered live. Copy any of them into a paste and you will get the same output.

Inline and block math

Single dollar signs put a formula in the flow of a sentence. Double dollar signs give it its own centred line.

You write
Inline: the checksum is $c = \sum_{i=1}^{n} b_i \bmod 256$.

Block:

$$
c = \sum_{i=1}^{n} b_i \bmod 256
$$
You get

Inline: the checksum is $c = \sum_{i=1}^{n} b_i \bmod 256$.

Block:

c=i=1nbimod256c = \sum_{i=1}^{n} b_i \bmod 256

Notice the operators are spaced differently in block form. KaTeX uses display style there — bigger sums, limits above and below rather than beside.

Superscripts and subscripts

A caret raises, an underscore lowers. Braces group anything longer than a single character.

You write
$x^2$, $x_i$, $x^{2n}$, $x_{i+1}$

$$
2^{256} \approx 1.16 \times 10^{77}
$$
You get

$x^2$, $x_i$, $x^{2n}$, $x_{i+1}$

22561.16×10772^{256} \approx 1.16 \times 10^{77}

Forgetting the braces is the most common mistake: `$x^10$` renders as x¹0, not x¹⁰.

Fractions and roots

Fractions take numerator and denominator as separate arguments. Roots take an optional index.

You write
$$
\frac{a}{b} \quad \sqrt{x} \quad \sqrt[3]{x} \quad \frac{\partial f}{\partial x}
$$

$$
\text{collision} \approx \frac{n^2}{2 \cdot 36^8}
$$
You get
abxx3fx\frac{a}{b} \quad \sqrt{x} \quad \sqrt[3]{x} \quad \frac{\partial f}{\partial x}collisionn22368\text{collision} \approx \frac{n^2}{2 \cdot 36^8}

Use `\dfrac` to force full-size fractions inline, where they would otherwise be shrunk to fit the line.

Greek letters and symbols

Backslash plus the name. Capitalise the first letter for the uppercase form.

You write
$\alpha, \beta, \gamma, \delta, \theta, \lambda, \mu, \pi, \sigma, \phi, \omega$

$\Gamma, \Delta, \Theta, \Lambda, \Sigma, \Phi, \Omega$

$\infty \quad \partial \quad \nabla \quad \pm \quad \times \quad \div \quad \neq \quad \leq \quad \approx$
You get

$\alpha, \beta, \gamma, \delta, \theta, \lambda, \mu, \pi, \sigma, \phi, \omega$

$\Gamma, \Delta, \Theta, \Lambda, \Sigma, \Phi, \Omega$

$\infty \quad \partial \quad \nabla \quad \pm \quad \times \quad \div \quad \neq \quad \leq \quad \approx$

Sums, products and integrals

Limits attach with the same caret and underscore used for superscripts and subscripts.

You write
$$
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
$$

$$
\prod_{i=1}^{n} i = n! \qquad \int_{0}^{\infty} e^{-x} \, dx = 1
$$
You get
i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}i=1ni=n!0exdx=1\prod_{i=1}^{n} i = n! \qquad \int_{0}^{\infty} e^{-x} \, dx = 1

`\,` inserts a thin space. Putting one before `dx` is the convention that makes an integral look typeset rather than typed.

Matrices

Ampersands separate columns, double backslashes end rows. The environment name picks the brackets.

You write
$$
\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}
\quad
\begin{pmatrix} a & b \\ c & d \end{pmatrix}
\quad
\begin{vmatrix} a & b \\ c & d \end{vmatrix}
$$
You get
[1001](abcd)abcd\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \quad \begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad \begin{vmatrix} a & b \\ c & d \end{vmatrix}

`bmatrix` gives square brackets, `pmatrix` parentheses, `vmatrix` vertical bars for a determinant, and `matrix` no delimiters at all.

Multi-line equations

The aligned environment lines up a series of steps on a chosen character — usually the equals sign.

You write
$$
\begin{aligned}
  \text{key} &= \text{PBKDF2}(pw, salt, 310000) \\
  ct &= \text{AES-GCM}(key, iv, pt) \\
  \text{stored} &= \text{base64}(salt \Vert iv \Vert ct)
\end{aligned}
$$
You get
key=PBKDF2(pw,salt,310000)ct=AES-GCM(key,iv,pt)stored=base64(saltivct)\begin{aligned} \text{key} &= \text{PBKDF2}(pw, salt, 310000) \\ ct &= \text{AES-GCM}(key, iv, pt) \\ \text{stored} &= \text{base64}(salt \Vert iv \Vert ct) \end{aligned}

The ampersand marks the alignment point on each line. Put it immediately before the character you want stacked.

Text inside math

Plain words in math mode render as a run of italic variables. Wrap them in `\text` to get real prose.

You write
Wrong: $iterations = 310000$

Right: $\text{iterations} = 310{,}000$

$$
\text{time} = \frac{\text{guesses}}{\text{rate}}
$$
You get

Wrong: $iterations = 310000$

Right: $\text{iterations} = 310{,}000$

time=guessesrate\text{time} = \frac{\text{guesses}}{\text{rate}}

Braces around a comma in a number, as in `310{,}000`, stop KaTeX adding a space after it.

When dollar signs are just money

A dollar sign in ordinary prose can accidentally open a formula. Escape it with a backslash.

You write
Escaped: the plan costs \$0 and always will.

Inline math still works: $E = mc^2$.
You get

Escaped: the plan costs $0 and always will.

Inline math still works: $E = mc^2$.

This is why inline math is off by default in many renderers. Two prices in one sentence otherwise turn the text between them into a formula.

Share a formula

Paste markdown with maths in it and get a link that renders properly — no screenshots of equations, no broken symbols. Encryption available if the work is not public yet.