Markdown Math: How to Write LaTeX Equations in Markdown

Written by Last updated

Markdown has no math syntax of its own. To write equations, you put LaTeX between dollar signs and let the renderer (KaTeX or MathJax) draw it. Use $...$ for a formula inside a sentence and $$...$$ for an equation on its own line.

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

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

This only works where the app or site has a math renderer turned on. GitHub, Obsidian, Jupyter and most documentation tools do. A plain CommonMark parser does not, and shows the dollar signs as text. The platform table below tells you where your Markdown will render.

🟒 Practice as you read: Open the Markdown Editor β€” it supports KaTeX math in live preview.

Open EditorDownload PDF

Inline and block math

Inline math Block math
Delimiters $...$ $$...$$
Where it shows Inside the sentence On its own centred line
Use for Variables, short formulas, units Derivations, matrices, long equations

Inline

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

Output

Live Preview

The area of a circle is A=Ο€r2A = \pi r^2.

Block

$$
E = mc^2
$$

Output

Live Preview

E=mc2E = mc^2

Rules that save debugging time:

  • Put $$ on its own line, with the equation on the lines between. Do not leave blank lines inside the block, because many renderers break on them.
  • Inline math should not start or end with a space inside the dollars. Write $x^2$, not $ x^2 $. Some renderers (Pandoc, for one) will not treat the spaced version as math.
  • Close every delimiter. One missing $ can turn the rest of the paragraph into math.

Copy-paste syntax

Everything below is plain LaTeX math, so it works in KaTeX and MathJax alike.

You want Write Shows
Fraction \frac{a}{b} ab\frac{a}{b}
Power x^2, x^{10} x2x^2, x10x^{10}
Subscript x_1, H_2O x1x_1, H2OH_2O
Square root \sqrt{x}, \sqrt[3]{x} x\sqrt{x}, x3\sqrt[3]{x}
Greek letters \alpha \beta \theta \pi \Omega αβθπΩ\alpha \beta \theta \pi \Omega
Multiply, not equal a \times b, a \ne b a×ba \times b, a≠ba \ne b
Less/greater or equal a \le b, a \ge b a≀ba \le b, aβ‰₯ba \ge b
Sum \sum_{i=1}^{n} i βˆ‘i=1ni\sum_{i=1}^{n} i
Integral \int_0^1 x\,dx ∫01x dx\int_0^1 x\,dx
Limit \lim_{x \to 0} \frac{\sin x}{x} lim⁑xβ†’0sin⁑xx\lim_{x \to 0} \frac{\sin x}{x}
Text inside math \text{if } x > 0 ifΒ x>0\text{if } x > 0
Brackets that scale \left( \frac{a}{b} \right) (ab)\left( \frac{a}{b} \right)

Remember that exponents and subscripts with more than one character need braces: x^{10}, not x^10.

Matrix#

$$
\begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}
$$

Output

Live Preview

[1234]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

Multi-line equations that line up#

Use aligned when you want several steps lined up on the equals sign.

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

Output

Live Preview

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

Piecewise functions#

$$
f(x) =
\begin{cases}
x^2 & \text{if } x \ge 0 \\
-x  & \text{if } x < 0
\end{cases}
$$

Output

Live Preview

f(x)={x2ifΒ xβ‰₯0βˆ’xifΒ x<0f(x) = \begin{cases} x^2 & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}

Where Markdown math works

Math is not part of the CommonMark spec. Each platform adds it with its own renderer, so check yours before you write a long document.

Platform Inline $...$ Block $$...$$ Renderer Notes
GitHub Yes Yes MathJax Also supports a math code block. See the GitHub section below.
Obsidian Yes Yes MathJax Works in notes by default. See the Obsidian guide.
Jupyter Notebook Yes Yes MathJax Works in Markdown cells.
VS Code preview Yes Yes KaTeX Depends on version and extensions. See the VS Code guide.
Hugo Needs setup Needs setup KaTeX or MathJax You enable math in config and add the script yourself.
Plain CommonMark parser No No None Dollar signs appear as text.

Math on GitHub

GitHub renders math in issues, discussions, pull requests, wikis and Markdown files, using MathJax. It gives you three ways to write it:

Inline with dollars: $a^2 + b^2 = c^2$

Inline with backticks: $`a_1 + a_2`$

Block with dollars:

$$
\int_0^1 x^2\,dx
$$

Block as a code fence:

```math
\int_0^1 x^2\,dx
```

Use the backtick form $`...`$ when your formula contains characters Markdown also uses, such as _ and *. The dollar form can let Markdown interpret those characters before the math renderer sees them.

To show a literal dollar sign next to a formula on the same line, escape it as \$ inside the math, or wrap it in a <span> outside the math. Always preview in the real place you will publish, because support differs between GitHub features.

More GitHub-only formatting is on the GitHub Flavored Markdown page.

KaTeX or MathJax?

You usually do not choose. Your platform ships one of them. If you do choose:

KaTeX MathJax
Speed Faster, renders instantly Slower
LaTeX coverage Most common commands Wider coverage
Pick it when Docs, notes, websites with many equations You need rarer LaTeX commands

If a command works on GitHub but shows an error in another tool, the other tool is probably running KaTeX. Check its list of supported functions or swap the command.

Why your equation is not rendering

What you see Likely cause Fix
Raw $...$ text on the page Platform has no math renderer, or it is switched off Check the platform table. Enable the math plugin or setting.
Subscripts vanish, text turns italic Markdown processed _ or * before the math renderer On GitHub use $`...`$. Elsewhere try escaping the underscore as \_ or move the formula into a block.
A price like $5 and $10 turns into math The renderer read the two dollar signs as a pair Escape each price as \$5.
Block equation looks broken Blank line inside $$, or $$ not on its own line Remove blank lines, put $$ on separate lines.
Matrix rows run together The parser ate the \\ row break Try \\\\ in that tool, or use a different renderer.
Red error text or the raw command The command is not supported by that renderer Look up the command in the KaTeX or MathJax docs.
Formula inside a table cell splits the column The pipe character, as in an absolute value, is read as a column break Use \vert instead of the pipe, or move the equation out of the table.

Math in tables: simple expressions like $x^2$ usually work. Matrices and multi-line equations do not fit in a cell, so put them above or below the table as block math.

Quick check before you publish

  • Inline math uses single $, block math uses $$ on separate lines.
  • Multi-character exponents and subscripts have braces.
  • You previewed the page on the platform where readers will see it.

Frequently Asked Questions

  1. 1

    Does GitHub Markdown support LaTeX?

    Yes, for math. GitHub renders $...$ inline, $$...$$ as a block, and a fenced math code block, using MathJax. It works in issues, pull requests, discussions, wikis and Markdown files. It supports math notation, not a full LaTeX document with packages.

  2. 2

    Why does my Markdown show dollar signs instead of an equation?

    The place you are viewing it has no math renderer, or it is turned off. Standard CommonMark does not include math. Check that your editor or site supports KaTeX or MathJax, then preview again.

  3. 3

    How do I write a dollar sign in Markdown when math is on?

    Escape it with a backslash: \$5. On GitHub, inside a formula use \$ and outside a formula wrap the sign in a span tag. Without escaping, two dollar signs on one line can be read as an equation.

  4. 4

    How do I line up several equations in Markdown?

    Put them in a block and use \begin{aligned} ... \end{aligned}. Put & before the equals sign to line up each step and end each line except the last with \\.

  5. 5

    Can I put math in a Markdown table?

    Short inline math such as $x^2$ usually works. Avoid the pipe character inside a cell, because it splits the column; write \vert for absolute value. Place matrices and long equations outside the table as block math.