Math in Markdown: LaTeX syntax cheat sheet with examples

Write math in Markdown with LaTeX: inline and display delimiters, a cheat sheet of fractions, sums and matrices, and dollar-sign pitfalls.

Inline and display math

Markdown has no math of its own. Most tools that support math use LaTeX notation between dollar signs:

Markdown
The area of a circle is $A = \pi r^2$.

$$
\int_0^1 x^2 \, dx = \frac{1}{3}
$$
  • Inline math goes between single dollar signs and sits in the line of text.
  • Display math goes between double dollar signs, usually on their own lines, and is centred on a line of its own.

Write the LaTeX first and check it rendered, because a missing brace breaks the whole expression.

Cheat sheet: the commands you'll use most

Every command below renders in KaTeX and MathJax. Copy the LaTeX column between dollar signs.

To writeLaTeX
Superscript, powerx^2, e^{i\pi}
Subscriptx_1, a_{n+1}
Fraction\frac{a}{b}
Square root, nth root\sqrt{x}, \sqrt[3]{x}
Multiplicationa \times b, a \cdot b
Not equal, less or equal, approximately\neq, \leq, \geq, \approx
Plus or minus\pm
Greek letters\alpha, \beta, \theta, \pi, \Sigma, \Omega
Sum\sum_{i=1}^{n} i
Product\prod_{i=1}^{n} x_i
Integral\int_a^b f(x)\,dx
Limit\lim_{x \to 0} \frac{\sin x}{x}
Infinity\infty
Arrows\to, \Rightarrow, \iff
Sets\in, \notin, \subset, \cup, \cap, \emptyset
Absolute value\lvert x \rvert
Vector, hat, bar\vec{v}, \hat{x}, \bar{x}
Text inside math\text{if } x > 0
Spacinga\,b (thin), a\quad b (wide)
Brackets that grow\left( \frac{a}{b} \right)

Braces group more than one character: x^10 gives x¹ followed by 0, while x^{10} gives x¹⁰.

Matrices, cases and aligned equations

These need display math. Ampersands separate columns, and \\ ends a row.

A matrix, with round brackets (pmatrix) or square brackets (bmatrix):

Code
$$
A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}
$$

A function defined by cases:

Code
$$
f(x) = \begin{cases} x^2 & \text{if } x \geq 0 \\ -x & \text{otherwise} \end{cases}
$$

Several equations aligned on the equals sign:

Code
$$
\begin{aligned}
(a+b)^2 &= (a+b)(a+b) \\
        &= a^2 + 2ab + b^2
\end{aligned}
$$

Where math renders

Math is an extension, not part of CommonMark, so support varies:

App or siteMath supportEngine
GitHub$, $$ and math code blocksMathJax
VS Code (built-in preview)$ and $$KaTeX
Obsidian$ and $$MathJax
Typora$$ blocks; inline $ after enabling it in preferencesMathJax
Aero Markdown$ and $$, offlineKaTeX
Many plain viewers and editorsShows the LaTeX source—

GitHub has two extra forms, documented in its math guide: a fenced code block with the language math, and inline math written as dollar-backtick, as in $`\sqrt{3}`$, for expressions whose characters would otherwise be read as Markdown. Both show as code elsewhere, so use them only for files that live on GitHub.

Dollar signs, underscores and other pitfalls

  • Prices become math. "It costs $5, or $10 with shipping" can render the text between the dollar signs as an equation in some apps. Escape real dollar signs as \$. Pandoc and some other renderers follow a stricter rule: an opening $ must be followed by a non-space, a closing $ must follow a non-space and must not be followed by a digit. That rule protects "$5 and $10", but escaping protects every case.
  • Spaces inside the delimiters. $ x^2 $ is not math in renderers that follow that rule. Write $x^2$.
  • Underscores turn into italics. In some renderers, $a_1$ and $b_1$ is read as emphasis before it is read as math. Escaping the underscore or using GitHub's dollar-backtick form fixes it.
  • Pipes in tables. A | inside math in a table cell starts a new cell. Use \lvert and \rvert, or \vert.
  • Unsupported commands. Packages and custom macros from a LaTeX document (\usepackage, \newcommand in some apps) may not exist in the renderer. Test the exact commands you use.

To check an equation before you commit or send it, paste it into the LaTeX Markdown preview: an error shows in red where it happens. For reading finished documents with math on a Mac, see reading math in Markdown on Mac.

Frequently asked questions

How do I write math in Markdown?

Put LaTeX between dollar signs. Write $x^2$ for math inside a sentence, and put $$ on the lines before and after an equation to display it on its own line. The app showing the file must support math for it to render.

Does GitHub support math in Markdown?

Yes. GitHub renders LaTeX between $ and $$ delimiters in Markdown files, issues, pull requests and discussions. It also accepts a fenced code block with the language math, and $...$ for inline math that contains Markdown characters.

What is the difference between KaTeX and MathJax?

Both render LaTeX math in web pages. KaTeX is faster and supports most math commands; MathJax supports more LaTeX and is used by GitHub and Obsidian. An equation that works in one usually works in the other, but rare commands may not.

How do I write a dollar sign in Markdown with math enabled?

Escape it as \$. Some renderers, such as Pandoc, also ignore a closing dollar sign that is followed by a digit, which protects “$5 and $10”, but escaping is the only rule that works everywhere.

How do I write a matrix in Markdown?

Use a matrix environment inside display math: \begin{pmatrix} a & b \\ c & d \end{pmatrix}. Ampersands separate columns, a double backslash ends a row; bmatrix uses square brackets.

Why does my equation show as red text or raw LaTeX?

Red text is a LaTeX error, such as a missing brace or an unsupported command. Raw LaTeX with dollar signs means the app doesn't render math at all, or the delimiters weren't recognised, often because of a space just inside the dollar signs.