Mathematical Expressions in LaTeX
LaTeX is the gold standard for typesetting mathematical expressions. Whether you're writing a simple equation or a multi-page derivation, LaTeX gives you precise control over how your math looks. This guide covers everything from basic inline formulas to aligned multi-line equations, equation numbering, and the essential amsmath package.
Inline vs Display Math
LaTeX provides two primary ways to include mathematics in your document:inline math and display math. Choosing the right mode is important because it affects both the appearance of the formula and the flow of your text.
Inline Math
Inline math is placed directly within a line of text using dollar signs ($...$) or \(...\). It keeps the formula compact so it doesn't break the paragraph flow.
The Pythagorean theorem states that $a^2 + b^2 = c^2$.
% Equivalent using \(...\)
The Pythagorean theorem states that \(a^2 + b^2 = c^2\).Inline mode automatically reduces the size of large operators, fractions, and limits so the formula fits within the line height. For instance, a fraction like$\frac{1}{2}$ will appear smaller than in display mode.
Display Math
Display math places the expression on its own line, centred and with generous vertical spacing. Use \[...\] (or the equation environment when you need numbering).
\[
E = mc^2
\]
% Or equivalently (plain TeX syntax, less recommended):
$$
E = mc^2
$$Display mode renders fractions, sums, and integrals at full size, making them easier to read. Use it for any expression you want the reader to focus on.
When to Use Each
- Inline math – short expressions referenced in running text, e.g. "where
$n$is a positive integer." - Display math – important equations, long formulas, or anything you want the reader to study carefully.
Fractions
Fractions are one of the most common constructs in mathematical writing. LaTeX provides several commands depending on how you want them displayed.
\frac{}{} – Standard Fraction
The basic \frac command adapts its size to the current mode: full-size in display math, compact in inline math.
% Display mode – full size
\[
\frac{a}{b} + \frac{c}{d} = \frac{ad + bc}{bd}
\]
% Inline mode – compact
The probability is $\frac{1}{6}$ for each face.\dfrac{}{} – Display-Style Fraction
Forces a full-size (display-style) fraction even in inline mode. Requires theamsmath package.
% Force display-size fraction inside inline text
The answer is $\dfrac{n!}{k!(n-k)!}$.\tfrac{}{} – Text-Style Fraction
Forces a compact (text-style) fraction even in display mode. Useful inside exponents or subscripts.
\[
2^{\tfrac{n}{2}} \quad \text{instead of} \quad 2^{\frac{n}{2}}
\]\cfrac – Continued Fractions
Designed specifically for continued fractions, keeping each level properly sized.
\[
\cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{\ddots}}}}
\]Square Roots and Nth Roots
The \sqrt command produces a square root symbol that automatically stretches to fit its argument.
Basic Square Root
\[
\sqrt{a^2 + b^2}
\]Nth Root
Pass an optional argument in square brackets for an arbitrary root index.
\[
\sqrt[3]{27} = 3
\qquad
\sqrt[n]{x}
\]Nested Roots
LaTeX handles nested roots gracefully. For deeply nested expressions you may want to add a small space with \, for readability.
\[
\sqrt{1 + \sqrt{1 + \sqrt{1 + \sqrt{1 + x}}}}
\]Parentheses and Brackets
Delimiters (parentheses, brackets, braces) are essential for grouping sub-expressions. LaTeX can automatically scale them to fit or you can control sizing manually.
Basic Delimiters
\[
(a + b) \quad [a + b] \quad \{a + b\}
\quad \langle a + b \rangle
\quad |a + b| \quad \|a + b\|
\]Auto-Sizing with \left and \right
Wrap your delimiters with \left and \right to scale them automatically to the height of the enclosed content.
\[
\left( \frac{a}{b} \right)
\qquad
\left[ \sum_{i=1}^{n} x_i \right]
\qquad
\left\{ \frac{1}{2}, \frac{1}{3}, \frac{1}{4} \right\}
\]If you need only one side, use \left. or \right. for an invisible delimiter:
\[
\left. \frac{d f}{d x} \right|_{x=0}
\]Manual Sizing
Sometimes \left/\right don't produce the size you want. Use the manual sizing commands for precise control:
\[
\big( \Big( \bigg( \Bigg(
\quad
\big] \Big] \bigg] \Bigg]
\quad
\big\{ \Big\{ \bigg\{ \Bigg\{
\]This is especially helpful inside multi-line equations where auto-sizing cannot span across line breaks.
Aligned Equations
When you have multiple lines of equations that should line up at a particular point (usually the equals sign), use alignment environments from theamsmath package.
The align Environment
The workhorse for multi-line equations. Use & to mark the alignment point and \\ to break lines.
\begin{align}
f(x) &= x^2 + 2x + 1 \\
&= (x + 1)^2
\end{align}Each line is numbered by default. Use align* to suppress all numbers, or \nonumber on individual lines.
\begin{align}
a &= b + c \nonumber \\
d &= e + f \label{eq:important}
\end{align}The split Environment
Use split inside an equation environment when you want a single equation number for multiple lines.
\begin{equation}
\begin{split}
(a + b)^3 &= (a + b)(a + b)^2 \\
&= (a + b)(a^2 + 2ab + b^2) \\
&= a^3 + 3a^2b + 3ab^2 + b^3
\end{split}
\end{equation}The multline Environment
For a single long equation that must be broken across lines. The first line is left-aligned, the last is right-aligned, and middle lines are centred.
\begin{multline}
p(x) = x^8 + x^7 + x^6 + x^5 \\
+ x^4 + x^3 + x^2 + x + 1
\end{multline}The gather Environment
Centres every line independently with no alignment point. Useful for listing several separate equations together.
\begin{gather}
x + y = 4 \\
x - y = 2
\end{gather}Equation Numbering
Numbered equations are essential for academic papers so you can reference results later. LaTeX makes this effortless.
The equation Environment
Produces a single numbered display equation.
\begin{equation}
E = mc^2
\label{eq:einstein}
\end{equation}
As shown in Equation~\ref{eq:einstein}, energy and mass are equivalent.\label and \ref
Assign a label to any numbered equation with \label{eq:name}and reference it anywhere in your document with \ref{eq:name}or \eqref{eq:name} (the latter adds parentheses automatically and requires amsmath).
From \eqref{eq:einstein} we can derive...Custom Tags with \tag{}
Override the automatic number with a custom label.
\begin{equation}
a^2 + b^2 = c^2 \tag{Pythagoras}
\end{equation}Unnumbered Equations
Add a * to suppress numbering: equation*,align*, gather*, etc.
\begin{equation*}
\sum_{k=0}^{\infty} x^k = \frac{1}{1-x}, \quad |x| < 1
\end{equation*}Cases and Piecewise Functions
The cases environment (from amsmath) is the standard way to write piecewise-defined functions.
\[
f(x) =
\begin{cases}
x^2 & \text{if } x \geq 0 \\
-x^2 & \text{if } x < 0
\end{cases}
\]You can have as many cases as you need:
\[
|x| =
\begin{cases}
x & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}
\]
% Sign function
\[
\operatorname{sgn}(x) =
\begin{cases}
1 & \text{if } x > 0 \\
0 & \text{if } x = 0 \\
-1 & \text{if } x < 0
\end{cases}
\]For a right-side brace, use the rcases environment (also fromamsmath):
\[
\begin{rcases}
x > 0 \\
y > 0
\end{rcases}
\Rightarrow x + y > 0
\]Matrices
LaTeX provides several matrix environments through the amsmath package. Here is a brief overview; for a deep dive see our dedicated Matrices in LaTeX guide.
Parenthesised Matrix (pmatrix)
\[
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\]Bracketed Matrix (bmatrix)
\[
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
\]Other variants include Bmatrix (curly braces),vmatrix (vertical bars for determinants), andVmatrix (double vertical bars for norms).
Spacing in Math Mode
LaTeX handles most mathematical spacing automatically, but sometimes you need manual adjustments. Here are the spacing commands available in math mode, from thinnest to widest:
| Command | Width | Description |
|---|---|---|
\! | -3/18 em | Negative thin space (pulls elements closer) |
\, | 3/18 em | Thin space |
\: | 4/18 em | Medium space |
\; | 5/18 em | Thick space |
\quad | 1 em | Quad space |
\qquad | 2 em | Double quad space |
\[
\int_0^1 f(x) \, dx % thin space before dx
\qquad
\sqrt{2} \, x % thin space after root
\qquad
\iint\limits_D f(x,y) \, dA % double integral
\]The negative thin space \! is handy for tightening things like double integrals:
\[
\int \!\!\! \int_D f \, dA
\]Text Inside Math
Sometimes you need to include words or phrases within a mathematical expression. LaTeX provides several commands for this purpose.
\text{} (recommended)
From the amsmath package, \text renders its argument in the surrounding text font and respects the current size. This is the preferred approach.
\[
f(x) = x^2 \quad \text{for all } x \in \mathbb{R}
\]\mathrm{}
Produces upright (Roman) text but stays in math mode. Useful for multi-letter operator names or unit labels.
\[
\mathrm{pH} = -\log[\mathrm{H}^+]
\]\mbox{}
An older TeX command that puts text in an unbreakable horizontal box.\text is generally preferred in modern documents, but\mbox works without loading amsmath.
\[
x = y \mbox{ if and only if } y = x
\]Common Math Constructs
Here is a quick-reference table of frequently used LaTeX math commands:
| Command | Description |
|---|---|
\frac{a}{b} | Fraction a/b |
\sqrt{x} | Square root of x |
\sqrt[n]{x} | Nth root of x |
x^{n} | Superscript (exponent) |
x_{i} | Subscript |
\sum_{i=1}^{n} | Summation |
\int_{a}^{b} | Definite integral |
\lim_{x \to 0} | Limit |
\infty | Infinity symbol |
\partial | Partial derivative symbol |
\nabla | Nabla (gradient) operator |
\cdot | Centred dot (multiplication) |
\times | Cross product |
\leq, \geq | Less/greater than or equal |
\neq | Not equal |
\approx | Approximately equal |
\equiv | Identical / congruent |
\in, \notin | Element of / not element of |
\subset, \supset | Subset / superset |
\cup, \cap | Union / intersection |
\mathbb{R} | Blackboard bold (e.g. real numbers) |
\overline{x} | Overline (mean, conjugate) |
\hat{x} | Hat accent (estimator) |
\vec{v} | Vector arrow |
\dot{x}, \ddot{x} | Time derivatives (Newton notation) |
The amsmath Package
The amsmath package, developed by the American Mathematical Society, is the single most important package for mathematical typesetting in LaTeX. If you write any non-trivial mathematics, you should include it in every document.
\usepackage{amsmath}Here is what amsmath provides:
- Alignment environments –
align,gather,multline,split,flalign,alignat - Matrix environments –
pmatrix,bmatrix,vmatrix,Bmatrix,Vmatrix - The
casesenvironment – for piecewise-defined functions - Display-style fractions –
\dfrac,\tfrac,\cfrac \text{}command – for inserting text inside math mode\eqref{}– for referencing equations with automatic parentheses\operatorname{}– for defining custom operator names (e.g.\operatorname{argmax})\tag{}– for custom equation labels- Improved spacing – better default spacing around operators and relations
You can also load the companion packages amssymb (extra symbols like\mathbb, \mathfrak) and amsthm (theorem environments). Together they form the AMS-LaTeX bundle:
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}Next Steps
Now that you've mastered the foundations of mathematical expressions, explore these related topics:
- Integrals, Sums, and Limits in LaTeX – advanced integral notation, multi-index sums, and limit constructs
- Matrices in LaTeX – all matrix environments, augmented matrices, and large matrices
- Greek Letters and Math Symbols – complete reference for Greek letters, arrows, set notation, and more
- Subscripts and Superscripts in LaTeX – nested scripts, multi-character scripts, and combining sub/superscripts
Free LaTeX Tools
- LaTeX Preview – Test your LaTeX code with live PDF preview
- Math to LaTeX – Convert handwritten equations to LaTeX
- Excel to LaTeX – Convert spreadsheets to LaTeX tables
- Citation Generator – Generate BibTeX citations from DOIs
- AI LaTeX Generator – Generate LaTeX from text descriptions
- LaTeX Symbols Reference – Browse math operators and Greek letters
Ready to start writing?
Try Octree – the AI-powered LaTeX editor that makes writing faster and easier.
Try Octree Free →