Maths in Markdown: formulas with $, $$, \( \) and \[ \]

Notes, homework, lab reports and AI answers often have formulas. In a Markdown file they are written in LaTeX, like $E = mc^2$. A plain text viewer shows this as raw code. A reader that typesets maths shows the real formula.

This guide shows how to write markdown math, which delimiters to use, and what goes wrong.

Inline and display formulas

There are two kinds of formula:

The famous formula is $E = mc^2$ and it stays in the sentence.

$$\int_0^1 x\,dx = \tfrac{1}{2}$$

The four delimiters

Delimiters are the marks around the formula. You will meet four:

DelimitersKindExample
$…$inline$a^2 + b^2 = c^2$
$$…$$display$$a^2 + b^2 = c^2$$
\(…\)inline\(a^2 + b^2 = c^2\)
\[…\]display\[a^2 + b^2 = c^2\]

These are conventions. Maths is not part of the CommonMark or GitHub Flavored Markdown specification, so each tool decides what it reads.

```math
\sum_{k=1}^n k = \frac{n(n+1)}{2}
```

If you are not sure, use $…$ and $$…$$. They have the widest support.

LaTeX bits you will need

Copy these and change the letters.

You wantYou write
Fraction\frac{a}{b}
Powerx^2, and x^{10} for more than one character
Subscriptx_1, and x_{max}
Square root\sqrt{x}, and \sqrt[3]{x} for a cube root
Greek letters\alpha \beta \pi \Delta
Sum\sum_{i=1}^{n} i
Integral\int_0^1 x\,dx
Text in a formula\text{speed}

A matrix:

$$
\begin{pmatrix} a & b \\ c & d \end{pmatrix}
$$

Several lines lined up on the equals sign:

$$
\begin{aligned}
x + y &= 5 \\
x - y &= 1
\end{aligned}
$$

The & marks the point where the lines align. \\ starts a new line.

Chemistry with \ce{}

Chemical formulas use \ce{}. It comes from the mhchem package. Put it inside a formula:

$\ce{H2O}$ and $\ce{CrO4^2-}$

$$\ce{CH4 + 2O2 -> CO2 + 2H2O}$$

$$\ce{A <=> B}$$

Numbers become subscripts, ^ sets a charge, -> draws an arrow and <=> draws an equilibrium arrow. The tool must support mhchem, as not all do.

Common mistakes

A dollar sign for money. Two prices in one line can look like a formula:

It costs $5 and the other costs $10.

A tool that reads single dollars may treat 5 and the other costs as maths. Escape each money dollar with a backslash:

It costs \$5 and the other costs \$10.

On GitHub, you can also write <span>$</span>5. Inside a formula on GitHub, a backslash works: $\sqrt{\$4}$.

Spaces just inside the dollars. Write $x$, not $ x $. Some tools do not read a formula when a space follows the opening $. Keeping the formula tight avoids the problem.

A blank line inside a display formula. A blank line ends a paragraph, and many tools then stop reading the formula. Keep the whole $$ … $$ block on consecutive lines, with no empty line in the middle.

A $$ formula glued to text. For a display formula, put $$ on its own line, with an empty line before and after.

Reading maths in Markdown

PilcrowMD typesets formulas on your device. It does not use the internet or a hidden web page.

To fix a formula, switch to the editor, change the LaTeX, and switch back to see the result.

PilcrowMD for Mac (beta)

Common questions

Does maths from ChatGPT, Claude or Gemini work in Markdown? Yes, if the answer is saved as Markdown and your reader knows the delimiters it uses. Many assistants write \( \) and \[ \]. If your reader only knows dollars, replace them with $…$ and $$…$$.

What is the difference between $ and $$? One dollar on each side gives an inline formula, inside the sentence. Two dollars give a display formula, on its own line.

Why does my formula show as plain text? The most common reasons are a reader without maths support, delimiters the reader does not know, a space just inside the $, or a blank line inside a display formula. Check these in that order.

Also on Mac: PilcrowMD for Mac (beta)