1. Home
  2. Blog
  3. Tutorial

How to Write Math Equations in Markdown (LaTeX and KaTeX)

Write inline and display math in Markdown with LaTeX syntax. Fractions, roots, sums, matrices, aligned equations and common pitfalls — with examples that render on GitHub and KaTeX.

Plain Markdown has no syntax for formulas, but nearly every modern Markdown tool supports LaTeX math through an extension. You write the formula in TeX notation between dollar signs, and a math renderer such as KaTeX or MathJax turns it into properly typeset mathematics.

Below you’ll find the syntax that works across GitHub, GitLab, Obsidian, Jupyter and Markdown Preview Editor, plus a reference of the commands you’ll use most.

Inline and display math

There are two kinds of math in Markdown:

  • Inline math sits inside a sentence. Wrap it in single dollar signs: $E = mc^2$.
  • Display math gets its own centered line. Wrap it in double dollar signs, usually on separate lines.
markdownThe famous formula $E = mc^2$ fits inside a sentence.

$$
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$

Rendered, the display formula looks like this:

x=−b±b2−4ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

Essential syntax

What You type Notes
Superscript x^2, e^{i\pi} Use braces for more than one character
Subscript a_1, x_{n+1}
Fraction \frac{a}{b} \dfrac for a larger fraction inline
Square root \sqrt{x}, \sqrt[3]{x}
Sum, product \sum_{i=1}^{n} i, \prod_{k} k
Integral \int_0^1 x\,dx \, adds a thin space
Limit \lim_{x \to 0} \frac{\sin x}{x}
Greek letters \alpha, \beta, \pi, \Omega Capitalize for uppercase
Relations \le, \ge, \ne, \approx, \equiv
Operators \times, \cdot, \pm, \div
Sets \in, \subset, \cup, \cap, \mathbb{R}
Arrows \to, \Rightarrow, \leftrightarrow
Text inside math \text{if } x > 0 Keeps normal spacing
Vectors, accents \vec{v}, \hat{x}, \bar{y}

Braces group things: x^10 renders as x¹0, while x^{10} renders as x¹⁰. If a formula looks wrong, check the braces first — they are the usual culprit.

Brackets that grow

Plain parentheses keep their size. Use \left and \right to make them wrap tall content:

latex\left( \frac{a}{b} \right)^2 \quad \text{vs} \quad (\frac{a}{b})^2
(ab)2vs(ab)2\left( \frac{a}{b} \right)^2 \quad \text{vs} \quad (\frac{a}{b})^2

Matrices

Matrices use environments. pmatrix adds parentheses, bmatrix square brackets and vmatrix vertical bars (a determinant). Columns are separated by &, rows by \\.

latex$$
A = \begin{pmatrix}
  1 & 2 \\
  3 & 4
\end{pmatrix}
$$
A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}

Multi-line and aligned equations

To line up several steps at the equals sign, use the aligned environment inside display math and mark the alignment point with &:

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

For piecewise functions, use cases:

latex$$
|x| = \begin{cases}
  x  & \text{if } x \ge 0 \\
  -x & \text{if } x < 0
\end{cases}
$$
∣x∣={xif x≥0−xif x<0|x| = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}

Dollar signs that aren’t math

Prices in running text can accidentally become formulas. Most renderers follow the Pandoc rule: the opening $ must not be followed by a space, and the closing $ must not be preceded by a space or followed by a digit. So “between $5 and $10” usually stays plain text. When in doubt, escape the dollar sign: \$5.

KaTeX vs MathJax

Both understand the same LaTeX syntax; they differ mainly in speed and coverage.

  • KaTeX is fast and renders synchronously, which is ideal for live preview. It supports the vast majority of commands used in documentation, notes and papers.
  • MathJax supports a larger set of LaTeX packages and is used by GitHub and many academic sites, but it’s heavier.

Markdown Preview Editor uses KaTeX and loads it only when a document actually contains a formula, so documents without math stay light. A formula written for KaTeX will almost always render on GitHub as well.

Math on GitHub

GitHub renders math in Markdown files, issues and pull requests. It supports $…$ and $$…$$, plus two alternatives for tricky cases: $`…`$ for inline math that contains characters Markdown would interpret, and a fenced code block with the language math for display equations.

Writing math faster

  • Use the math button in the Advanced toolbar of Markdown Preview Editor to insert a formula template, then edit it with the preview side by side.
  • Keep a snippet file with the formulas you use often — matrices, aligned blocks, cases.
  • Combine math with Mermaid diagrams to explain algorithms: a flowchart for the steps and a formula for each step.
  • When the document is ready, export it to HTML or PDF — the formulas are kept in the exported file.

New to Markdown in general? Start with the Markdown cheat sheet.

Frequently asked questions

How do I write an equation in Markdown?

Wrap the LaTeX formula in single dollar signs for inline math ($a^2 + b^2 = c^2$) or double dollar signs for a centered display equation. The Markdown tool must support math — GitHub, GitLab, Obsidian, Jupyter and Markdown Preview Editor do.

Why is my formula shown as plain text?

Common reasons: the tool doesn’t support math, there is a space right after the opening $ or right before the closing $, or a Markdown character such as _ or * inside the formula was interpreted as formatting. Try display math with $$ on separate lines.

How do I write a matrix in Markdown?

Use \begin{pmatrix} … \end{pmatrix} (or bmatrix, vmatrix) inside $$. Separate columns with & and rows with \\.

Does KaTeX support all LaTeX commands?

KaTeX supports most math commands used in practice, but not every LaTeX package. Commands for document layout (like \section or \usepackage) don’t apply to Markdown math at all.