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.
Download PDF opens your browser's print dialog. Choose Save as PDF as the destination.
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 type | You get | Often written by |
|---|---|---|
$…$ | Math inside a sentence | GitHub, Jupyter, Obsidian |
\(…\) | Math inside a sentence | LaTeX documents, some AI tools |
$$…$$ | An equation on its own line | GitHub, Jupyter, Obsidian |
\[…\] | An equation on its own line | LaTeX documents, some AI tools |
$`…`$ | Math inside a sentence | GitHub |
A code block marked ```math | An equation on its own line | GitHub |
\begin{equation}…\end{equation} | A numbered equation on its own line | LaTeX 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:
- The opening
$comes at the start of a paragraph or after a space or line break, and is followed straight away by something other than a space. - The closing
$follows something other than a space, and comes at the end of a paragraph or before a space, line break, full stop, comma or semicolon.
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 type | For |
|---|---|
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, \Sigma | Greek letters; a capital first letter gives a capital |
\times, \div, \pm, \cdot | Multiplication, division, plus or minus, a centered dot |
\le, \ge, \ne, \approx | Less 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 > 0 | Ordinary 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:
- Number them yourself. Write the number in brackets after the closing
$$or\], as in$$ E = mc^2 $$ (1), or put\tag{1}inside the formula. The number appears at the right margin. Keep it short and without spaces, such as (1), (2a) or (A). - Let md2pdf.me count.
\begin{equation}numbers a single equation, and\begin{align}numbers every line. The numbers run through the whole document: (1), (2), (3) and so on.
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:
- Unbalanced braces. Every
{needs a}. A missing one in a long fraction is the most common error. - Spaces inside single dollars.
$ x + y $stays as text. Write$x + y$, with no space after the opening dollar or before the closing one. - Starred environments.
\begin{align*}and\begin{equation*}aren't recognized and show as text. For unnumbered equations, put them inside$$, or usealignedinside$$, as above. - Vertical bars in a table. A
|inside a formula in a table starts a new cell. Write absolute values as\vert x \vertor\lvert x \rvert. - Document commands.
\newpage, packages and other commands from a full LaTeX document aren't math, so they don't work. For a new page, see page breaks. A\newcommandonly lasts for the formula it's in, so it can't set up shortcuts for the whole document.
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
- Tables: rows, columns and alignment
- Code blocks: code with syntax highlighting
- Footnotes: numbered notes and references
- Task lists: checklists with tick boxes
- Page breaks: controlling where pages end