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.
Inline: the checksum is $c = \sum_{i=1}^{n} b_i \bmod 256$.
Block:
$$
c = \sum_{i=1}^{n} b_i \bmod 256
$$Inline: the checksum is $c = \sum_{i=1}^{n} b_i \bmod 256$.
Block:
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.
$x^2$, $x_i$, $x^{2n}$, $x_{i+1}$
$$
2^{256} \approx 1.16 \times 10^{77}
$$$x^2$, $x_i$, $x^{2n}$, $x_{i+1}$
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.
$$
\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}
$$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.
$\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$$\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.
$$
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
$$
$$
\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.
$$
\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.
$$
\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.
Wrong: $iterations = 310000$
Right: $\text{iterations} = 310{,}000$
$$
\text{time} = \frac{\text{guesses}}{\text{rate}}
$$Wrong: $iterations = 310000$
Right: $\text{iterations} = 310{,}000$
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.
Escaped: the plan costs \$0 and always will.
Inline math still works: $E = mc^2$.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.