Math and LaTeX in Markdown

Write formulas in Markdown with the same LaTeX commands scientists and engineers use, and get properly typeset equations in your PDF. This guide covers the syntax, with examples you can load into the converter.

Markdown
Preview

Your first formula

Markdown itself has no math syntax, so md2pdf.me follows the convention used by GitHub, Jupyter notebooks and Obsidian: anything between dollar signs is LaTeX math. One dollar sign on each side puts a formula inside a sentence. Two on each side give it a line of its own, centered, which is called display math.

The area of a circle is $A = \pi r^2$, where $r$ is its radius.

The distance between two points is

$$
d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
$$

The commands inside the dollar signs are LaTeX, the typesetting language behind most scientific papers. ^ makes a superscript, _ a subscript, \pi the Greek letter and \sqrt{…} a square root. Curly braces group things, so x^{10} raises x to the tenth power, while x^10 would give x¹ followed by a zero.

md2pdf.me draws formulas with KaTeX, which runs in your browser like the rest of the converter. It only loads when a document contains math, so documents without formulas don't wait for it.

Ways to mark math

Different tools mark math in different ways, and md2pdf.me understands the common ones, so you can paste from most places without changing anything.

You typeYou getOften written by
$…$Math inside a sentenceGitHub, Jupyter, Obsidian
\(…\)Math inside a sentenceLaTeX documents, some AI tools
$$…$$An equation on its own lineGitHub, Jupyter, Obsidian
\[…\]An equation on its own lineLaTeX documents, some AI tools
$`…`$Math inside a sentenceGitHub
A code block marked ```mathAn equation on its own lineGitHub
\begin{equation}…\end{equation}A numbered equation on its own lineLaTeX documents

Put display math on lines of its own, with a blank line before and after, as in the examples on this page. If you're copying an answer from an AI assistant, the AI answers to PDF page explains how to copy it with its formulas.

Dollar signs and prices

Dollar signs also mean money, and a sentence like tickets cost $5 and parking $10 mustn't turn into a formula. So md2pdf.me only treats a single dollar sign as math when it looks like math:

That keeps prices as text, but it also means a formula in brackets, ($x$), or one followed by a colon, question mark or closing bracket stays as plain text with its dollar signs showing. When that happens, use \(…\) instead, which works next to any punctuation:

The ticket costs $5 and parking costs $10.

The slope (\(m = 2\)) tells us how fast \(y\) grows: twice as fast as \(x\).

The LaTeX you'll use most

You don't need to learn all of LaTeX to write good-looking formulas. These commands cover most school, university and business math:

You typeFor
x^2, x_i, x_{i+1}^{2n}Superscripts and subscripts
\frac{a}{b}A fraction
\sqrt{x}, \sqrt[3]{x}Square and cube roots
\alpha, \beta, \theta, \SigmaGreek letters; a capital first letter gives a capital
\times, \div, \pm, \cdotMultiplication, division, plus or minus, a centered dot
\le, \ge, \ne, \approxLess or equal, greater or equal, not equal, approximately
\sum_{i=1}^{n}, \int_a^b, \lim_{x \to 0}Sums, integrals and limits
\left( \frac{a}{b} \right)Brackets that grow to fit what's inside
\text{if } x > 0Ordinary words inside a formula
\mathbb{R}, \mathbf{v}, \vec{v}, \hat{x}Number sets, bold vectors, arrows and hats

Spaces you type inside a formula are ignored, because LaTeX decides the spacing itself. To add space, use \, for a little or \quad for a lot. Matrices and cases are written as environments, with & between columns and \\ at the end of each row:

$$
A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}
\qquad
|x| = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}
$$

pmatrix gives round brackets; bmatrix gives square ones. KaTeX's list of supported functions has everything else, from arrows and accents to operators like \operatorname{sgn}.

Multi-line and numbered equations

To show a calculation step by step, use the aligned environment inside $$. Put & before the sign you want to line up on each row, and end each row except the last with \\:

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

Papers and textbooks number their important equations so the text can refer to them. There are two ways to do it:

Newton's second law relates force, mass and acceleration:

\begin{equation}
F = ma
\end{equation}

Kinetic energy and momentum follow from it:

\begin{align}
E_k &= \tfrac{1}{2} m v^2 \\
p   &= m v
\end{align}

Pick one way per document. The automatic numbers don't know about the ones you type, so mixing them can give two equations the same number.

Common mistakes

When a formula has a mistake, md2pdf.me shows its LaTeX in red instead of the formula, so you can see which one needs fixing before you download. These are the usual causes:

Math in a PDF

Formulas are set in KaTeX's own fonts, which are modeled on the Computer Modern type of LaTeX, so they look the way readers expect math to look whichever style you choose. Like the rest of the text, they're drawn as vector shapes and stay sharp however far you zoom in, and they print crisply. An equation on its own line is never split across two pages.

The Academic style suits math-heavy documents best: it sizes formulas to sit well with its Palatino text, numbers the section headings and gives tables the three-line layout used in journals. Classic suits essays and notes with the occasional formula, and Report technical reports with a few key equations.

Long formulas don't wrap the way text does. A formula on its own line that's too wide for the page is shrunk to fit, so it comes out smaller than the text around it. To keep it full size, split it over several lines with aligned, or give a long expression a name and use the name in the main equation.

Other topics

Back to the Markdown guide