This lesson covers the structured environments that give mathematical documents their formal backbone: theorem blocks, piecewise functions, custom operators, equation management, and fine-tuning tools.
The LaTeX theorem environment system gives mathematical writing its formal backbone: numbered theorems, lemmas, and definitions that cross-reference themselves no matter how much you reorder the document. This lesson covers amsthm setup, piecewise cases, and taking control of equation numbering.
Theorem Environments
Mathematical documents need structured environments for theorems, lemmas, definitions, and proofs. The amsthm package provides this.
Defining Theorem Styles
Theorem environments are declared in the preamble. The amsthm package offers three built-in styles:
\usepackage{amsthm}
\theoremstyle{plain} % Italic body
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}
\theoremstyle{definition} % Upright body
\newtheorem{definition}{Definition}[chapter]
\newtheorem{example}{Example}[chapter]
\theoremstyle{remark} % Italic header, upright body
\newtheorem{remark}{Remark}[chapter]- plain — for theorems, lemmas, corollaries. The body text is italicized.
- definition — for definitions and examples. The body text is upright (roman).
- remark — for remarks and notes. Lighter visual weight.
Using Theorem Environments
\begin{theorem}[Pythagorean]
\label{thm:pythag}
For a right triangle with legs $a$, $b$ and hypotenuse $c$:
<!--M21-->
\end{theorem}
\begin{proof}
Consider a square of side $a+b$...
\end{proof}Rendered result:
Theorem 5.1 (Pythagorean). For a right triangle with legs \( a \), \( b \) and hypotenuse \( c \):
$$ a^2 + b^2 = c^2 $$
Proof. Consider a square of side \( a+b \)…
The [theorem] option in \newtheorem{lemma}[theorem]{Lemma} means lemmas share the theorem counter. This produces Theorem 1, Lemma 2, Corollary 3 instead of Theorem 1, Lemma 1, Corollary 1. Shared counters make it easier to find results in a document.
Custom Theorem Styles with tcolorbox
For visually distinct theorems, combine amsthm with tcolorbox:
\newtcbtheorem[number within=chapter]{mythm}{Theorem}{
colback=ramanujanblue!5,
colframe=ramanujanblue,
fonttitle=\bfseries,
arc=3pt,
boxrule=0.8pt
}{thm}
\begin{mythm}{Fundamental Theorem of Calculus}{ftc}
If $f$ is continuous on $[a,b]$ and $F'(x) = f(x)$, then
<!--M22-->
\end{mythm}This creates a colored box around the theorem with a styled title bar. The {thm} at the end is the label prefix, so this theorem can be referenced as thm:ftc.
Cases and Piecewise Functions
The cases environment handles piecewise definitions:
<!--M23-->Rendered result:
$$ |x| = \begin{cases} x & \text{if } x \geq 0 \\ -x & \text{if } x < 0 \end{cases} $$
Right-Brace Cases
For a system of equations with a right brace (the reverse of cases), use aligned with manual delimiters:
<!--M24-->Rendered result:
$$ \left.\begin{aligned} x + y &= 5 \\ 2x – y &= 1 \end{aligned}\right\} \quad \Rightarrow \quad x = 2, \; y = 3 $$
The \left. produces an invisible left delimiter that pairs with \right\} to create the right brace.
Custom Operators
When your field uses operators that aren’t predefined, create them with \DeclareMathOperator:
% In the preamble:
\DeclareMathOperator{\tr}{tr} % trace
\DeclareMathOperator{\diag}{diag} % diagonal
\DeclareMathOperator{\rank}{rank} % rank
\DeclareMathOperator{\sgn}{sgn} % sign function
\DeclareMathOperator{\Span}{span} % span (note capital S)
\DeclareMathOperator*{\argmax}{arg\,max} % with limits
% In the document:
$\tr(A) = \sum_i a_{ii}$, $\rank(A) = 3$
<!--M25-->Rendered result:
Inline: \( \operatorname{tr}(A) = \sum_i a_{ii} \), \( \operatorname{rank}(A) = 3 \)
Display:
$$ \hat{\theta} = \operatorname*{arg\,max}_{\theta} \mathcal{L}(\theta) $$
The starred form \DeclareMathOperator* places subscripts below the operator in display mode (like \lim). Without the star, subscripts appear to the side.
Why not just type \text{tr}? Because \DeclareMathOperator applies proper operator spacing, uses the correct font, and works consistently in all math contexts.
Equation Numbering and Tags
Controlling Numbering
% Numbered (default)
\begin{equation}
E = mc^2
\end{equation}
% Unnumbered
\begin{equation*}
E = mc^2
\end{equation*}
% Custom tag
\begin{equation}
E = mc^2 \tag{Einstein}
\end{equation}
% Numbered by section: (5.1), (5.2), ...
% Add to preamble:
\numberwithin{equation}{section}The \tag command replaces the automatic number with any text you choose. The \numberwithin command resets equation numbers at each section, producing numbers like (5.1), (5.2) instead of (1), (2), (3) throughout the entire document.
Subequations
Group related equations under one number:
\begin{subequations}\label{eq:maxwell}
\begin{align}
\nabla \cdot \vec{E} &= \frac{\rho}{\varepsilon_0}
\label{eq:gauss-e} \\
\nabla \cdot \vec{B} &= 0
\label{eq:gauss-m} \\
\nabla \times \vec{E} &= -\frac{\partial \vec{B}}{\partial t}
\label{eq:faraday} \\
\nabla \times \vec{B} &= \mu_0\vec{J}
+ \mu_0\varepsilon_0\frac{\partial \vec{E}}{\partial t}
\label{eq:ampere}
\end{align}
\end{subequations}
Maxwell's equations~\eqref{eq:maxwell}.
Gauss's law is~\eqref{eq:gauss-e}.This produces equations numbered (5.1a), (5.1b), (5.1c), (5.1d), and you can reference the group as (5.1) or individual equations like (5.1a).
Commutative Diagrams
For algebra and category theory, the tikz-cd package draws commutative diagrams:
\usepackage{tikz-cd} % in preamble
<!--M26-->This produces a square diagram with objects A, B, C, D at the corners and labeled arrows connecting them. The ' after a label places it on the opposite side of the arrow (below or left instead of above or right).
Arrow direction shortcuts: r = right, d = down, l = left, u = up. You can combine them: rd draws a diagonal arrow.
Boxed Equations
Highlight important results with \boxed:
<!--M27-->Rendered result:
$$ \boxed{E = mc^2} $$
This draws a thin rectangle around the equation, making key results stand out on the page.
Phantom and Smash
Fine-tuning tools for alignment:
\phantom{x}— invisible space the size of \( x \). Useful for alignment.\hphantom{x}— horizontal space only.\vphantom{x}— vertical space only. Forces consistent delimiter height.\smash{x}— renders \( x \) with zero height. Prevents stretching.
Using vphantom for Consistent Delimiters
% Without vphantom (uneven)
$\left(\frac{a}{b}\right) + \left(c\right)$
% With vphantom (even)
$\left(\frac{a}{b}\right)
+ \left(\vphantom{\frac{a}{b}}c\right)$Without \vphantom, the second pair of parentheses is much shorter than the first because \( c \) is smaller than \( \frac{a}{b} \). Adding \vphantom{\frac{a}{b}} inside the second pair forces the delimiters to match the height of the fraction, even though the phantom itself is invisible.
Numbering and Referencing Best Practices
Golden rules for equation management:
- Number equations you’ll reference. Leave the rest unnumbered.
- Use
\eqref(not\ref) for equation references. It adds parentheses automatically: Equation (5.1) not Equation 5.1.- Use descriptive labels:
eq:cauchy-schwarznoteq:1.- Group related equations with
subequations.- Use
\numberwithin{equation}{section}for long documents.
Exercises
Typeset Maxwell’s equations using the
alignenvironment with proper numbering and labels.Create a \( 4 \times 4 \) identity matrix, a general \( 3 \times 4 \) matrix with dot notation, and a \( 3 \times 4 \) augmented matrix.
Define and use a theorem environment. State and prove the AM-GM inequality: \( \frac{a+b}{2} \geq \sqrt{ab} \) for \( a, b \geq 0 \).
Create a custom operator
\Homand use it in a sentence: “The Hom-set \( \operatorname{Hom}(A,B) \) is…”Typeset the Schrodinger equation in both time-dependent and time-independent forms using
subequations.
These structures complete the mathematical core of the course. From here, figures and tables handles the visual side of documents, or jump to KaTeX and MathJax to put your math on the web.
Quick answers to common questions:
How do I set up theorem environments?
Load amsthm, then declare each type once in the preamble with \newtheorem{theorem}{Theorem}[section]. The optional argument numbers theorems within sections (Theorem 2.1, 2.2), which is the convention readers expect in anything longer than a problem set.
How do I reference equations and theorems by number?
Label them (\label{eq:euler}) and cite with \eqref for equations or \ref for theorems. Numbers update automatically when you reorder content, which is the entire point: never hardcode a number a refactor can break.
How do I write a piecewise function?
The cases environment from amsmath: each line is value & condition separated by \\. It produces the brace-and-columns layout textbooks use for absolute value and step functions.