Markdown Math: How to Write LaTeX Equations in Markdown
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.
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 .
Block
$$
E = mc^2
$$
Output
Live Preview
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} |
|
| Power | x^2, x^{10} |
, |
| Subscript | x_1, H_2O |
, |
| Square root | \sqrt{x}, \sqrt[3]{x} |
, |
| Greek letters | \alpha \beta \theta \pi \Omega |
|
| Multiply, not equal | a \times b, a \ne b |
, |
| Less/greater or equal | a \le b, a \ge b |
, |
| Sum | \sum_{i=1}^{n} i |
|
| Integral | \int_0^1 x\,dx |
|
| Limit | \lim_{x \to 0} \frac{\sin x}{x} |
|
| Text inside math | \text{if } x > 0 |
|
| Brackets that scale | \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
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
Piecewise functions#
$$
f(x) =
\begin{cases}
x^2 & \text{if } x \ge 0 \\
-x & \text{if } x < 0
\end{cases}
$$
Output
Live Preview
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 | MathJax | Also supports a math code block. See the GitHub section below. |
||
| Obsidian | MathJax | Works in notes by default. See the Obsidian guide. | ||
| Jupyter Notebook | MathJax | Works in Markdown cells. | ||
| VS Code preview | 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 | 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
Does GitHub Markdown support LaTeX?
Yes, for math. GitHub renders
$...$inline,$$...$$as a block, and a fencedmathcode 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
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
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
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
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\vertfor absolute value. Place matrices and long equations outside the table as block math.
