All posts
·6 min read

Writing Math Equations in Markdown with KaTeX

katexmathlatexmarkdown

If you write academic papers, technical documentation, or educational content, you need math rendering in your Markdown. KaTeX is the fastest and most widely supported engine for this. It renders LaTeX-style math expressions directly in the browser, without server-side processing.

Quick Answer: KaTeX renders LaTeX-style math in Markdown using dollar sign delimiters. Use $...$ for inline math and $$...$$ for block-level equations. KaTeX supports over 500 LaTeX functions including fractions, integrals, matrices, and Greek letters, and renders up to 100x faster than MathJax.

What Is the Difference Between Inline and Block Math?

There are two ways to include math in your Markdown:

  • Inline math is embedded within a paragraph, wrapped in single dollar signs: $E = mc^2$
  • Block math is displayed on its own line, wrapped in double dollar signs: $$\sum_{i=1}^n i$$

Inline math flows with the text. Block math is centered on its own line and is typically used for important equations you want to stand out.

How Do You Write Basic Math Expressions in KaTeX?

Arithmetic and Algebra

$a + b = c$
$x^2 + y^2 = z^2$
$\frac{a}{b}$
$\sqrt{x^2 + y^2}$
$a \cdot b = c$
$a \neq b$
$a \leq b$

Greek Letters

$\alpha, \beta, \gamma, \delta, \epsilon$
$\Gamma, \Delta, \Theta, \Lambda, \Omega$
$\pi \approx 3.14159$

Subscripts and Superscripts

$x_1, x_2, \ldots, x_n$
$a^{n+1}$
$x_{i,j}^{2k}$

Use curly braces to group multi-character subscripts and superscripts.

How Do You Write Fractions and Binomials?

$$\frac{n!}{k!(n-k)!} = \binom{n}{k}$$

Use \frac{num}{den} for fractions and \binom{n}{k} for binomial coefficients.

How Do You Write Sums, Products, and Integrals?

$$\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$$

$$\prod_{i=1}^{n} i = n!$$

$$\int_0^{\infty} e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$

$$\oint_C \mathbf{F} \cdot d\mathbf{r}$$

These four constructs cover the vast majority of calculus, statistics, and linear algebra notation used in technical writing.

How Do You Write Matrices in KaTeX?

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

$$\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}$$

Use pmatrix for parentheses, bmatrix for brackets, vmatrix for determinants.

How Do You Write Piecewise Functions?

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

The cases environment is the standard approach for piecewise and conditional function definitions.

What Are the Most Common KaTeX Symbols and Their Syntax?

Syntax Symbol Category
\times x Operator
\div ÷ Operator
\pm ± Operator
\infty Symbol
\partial Symbol
\nabla Symbol
\forall Logic
\exists Logic
\in Set
\subset Set
\cup Set
\cap Set
\rightarrow Arrow
\Rightarrow Arrow
\leftrightarrow Arrow

KaTeX supports over 500 functions from the LaTeX standard. The table above covers the symbols used in more than 80% of academic and technical writing contexts. According to the KaTeX documentation, the library can render a full page of equations in under 10 milliseconds, making it suitable for real-time live-preview editors.

What Are the Best Practices for Writing Clean Math in Markdown?

  • Use block math for any equation that readers might want to reference.
  • Use inline math for variables and short expressions within sentences.
  • Add \text{...} for words inside math mode (e.g., $\text{if } x > 0$).
  • Use \quad and \qquad for spacing within equations.
  • Break long equations across lines using aligned environments.
  • For multi-line derivations, use the align environment: \begin{align} ... \end{align}.

How Does KaTeX Compare to MathJax?

Both KaTeX and MathJax render LaTeX math in the browser, but they differ in key ways:

Feature KaTeX MathJax
Render speed Up to 100x faster Slower, full TeX parsing
LaTeX coverage ~500 functions Near-complete LaTeX
Browser support All modern browsers All modern browsers
Output format HTML+CSS HTML+CSS or SVG
Ideal for Live preview, real-time editors Full academic typesetting

KaTeX is the right choice for Markdown editors where equations need to render as you type. For documents requiring the full LaTeX feature set, MathJax provides broader coverage.

If you also use diagrams in your documents, see the guide on Mermaid diagrams in Markdown for structural and flow visualizations that pair well with KaTeX math notation. For a broader overview of Markdown syntax including code blocks and tables, the Markdown cheat sheet is a useful reference. If you are writing research papers or scientific documentation, the guide on Markdown for technical documentation covers how to structure longer documents that combine formulas, code, and prose.

Try It in edtr.md

edtr.md renders KaTeX in the preview pane as you type. Both inline and block math are supported. Open the editor and paste any of the examples above to see them rendered instantly.

Try it yourself

Open edtr.md and start writing Markdown with live preview, diagrams, math, and PDF export. Free, no sign-up.

Open editor