mathtools

amsmath has barely changed since the 1990s. Stability is a virtue, but the price was a growing pile of small irritations nobody could fix — the look of :=, absolute-value bars that refuse to follow the height of their contents, a long condition under a summation that stretches the whole formula sideways. mathtools is the LaTeX package created in 2002 as the place to put those fixes, and it does not replace amsmath: it loads amsmath and builds on top of it. This page works through \DeclarePairedDelimiter, \coloneqq, \mathclap, \prescript, matrix*, dcases and showonlyrefs, with measured dimensions for each. Every one of them is a step amsmath alone could not take.

amsmath or mathtools — which one do you load?

Just load mathtools. It pulls amsmath in for you, so a separate \usepackage{amsmath} is unnecessary. Options meant for amsmath pass straight through as well: \usepackage[fleqn,tbtags]{mathtools} is the same as writing \usepackage[fleqn,tbtags]{amsmath} followed by \usepackage{mathtools}. There is no need to name both and worry about the order.

latex
% one line is enough: mathtools loads amsmath itself
\usepackage{mathtools}

% options aimed at amsmath are passed straight through
\usepackage[fleqn,tbtags]{mathtools}

That “build on amsmath” design survived three generations of custodian. The copyright block of mathtools.sty reads: 2002–2011 Morten Høgholm, 2012–2019 Lars Madsen, and from 2020 Lars Madsen together with the LaTeX3 project. In other words mathtools graduated from one person's convenience kit into a quasi-standard looked after by the team that develops LaTeX itself. It shows: in the preamble of a document with mathematics in it, the first line named is now more often mathtools than amsmath. mathtools also depends on a helper package, mhsetup, but distributions such as TeX Live bundle it, so it never needs your attention.

Absolute values and norms that size themselves — \DeclarePairedDelimiter

One preamble line — \DeclarePairedDelimiter\abs{\lvert}{\rvert} — gives you an \abs of your own with three switchable sizes. The homemade \newcommand{\abs}[1]{\lvert#1\rvert} everyone writes first does get the horizontal spacing right, but feed it something tall like \abs{\frac{a}{b}} and the bars refuse to grow, left stranded beside a fraction that overshoots them. \DeclarePairedDelimiter takes over exactly there. Wrapping the command name in braces, \DeclarePairedDelimiter{\abs}{\lvert}{\rvert}, compiles just as well.

latex
% in the preamble; braces around the command name are optional
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\DeclarePairedDelimiter\norm{\lVert}{\rVert}

% three ways to call it
\[
  \abs{x} = \abs{-x}
  \qquad
  \abs*{\frac{a}{b}} = \frac{\abs{a}}{\abs{b}}
  \qquad
  \norm[\big]{v}
\]

The difference is not a matter of impression; it can be measured. Under TeX Live 2024, \sbox0{$\abs{\frac ab}$} comes out 7.5pt tall, while the starred \abs*{\frac ab} comes out 8.50006pt. The star wraps the body in \left … \right, so the bars grew by the height of the fraction. The third form, passing [\big], [\Big], [\bigg] or [\Bigg], picks a step by hand; in this example [\big] also measured 8.50006pt, landing on the same step the automatic version chose. Remember it as star = stretch, brackets = pin to this step and you will not mix them up. For notation that needs a separator inside the fence, like \Set{x}{x>0}, use \DeclarePairedDelimiterX.

FormSizeWhen to use
\abs{x}fixed (measured 7.5pt)when the body is a short variable or number
\abs*{x}automatic (measured 8.50006pt)tall bodies: fractions, radicals, sums
\abs[\big]{x}step chosen by handto keep a row of formulas at one height
\DeclarePairedDelimiterXas above, with several argumentsnotations with an inner separator, like \Set{x}{x>0}

Why := looks wrong, and what \coloneqq actually changes

Two things need fixing: the height of the colon and the gap between the colon and the equals sign. A common explanation says the spacing breaks because : is not a relation — that is simply false. Ask LaTeX for the math code of : and it answers 12346, which is "303A in hexadecimal; the leading 3 marks class 3, a relation. So wrapping it as $a\mathrel{:=}b$ changes nothing at all. Measurement agrees: $a:=b$ and $a\mathrel{:=}b$ both came out at exactly 25.68855pt.

So what does \coloneqq do? Open its definition and you find \vcentcolon\mathrel{\mkern-1.2mu}=. First, in place of the ordinary : it uses \vcentcolon, a colon centred on the math axis (measured: $:$ is 4.30554pt tall, $\vcentcolon$ is 4.65277pt — lifted by 0.34723pt). Second, a negative -1.2mu kern pulls the colon against the equals sign, because when two relations stand side by side TeX inserts relation spacing between them too, and : and = end up reading as two separate symbols. The result: $a\coloneqq b$ measures 25.02191pt, exactly 0.66664pt narrower than a raw := — precisely 1.2mu at a 10pt size.

latex
\[
  f(x) \coloneqq x^2 + 1, \qquad y \eqqcolon g(x), \qquad
  A \Coloneqq B
\]

One trap. mathtools changed what \coloneq means. Today, by default, \coloneq prints the same “:=” as \coloneqq; in the older definition it printed a colon and a dash, “:−”. The same happened to \eqcolon, which now matches \eqqcolon at “=:”. For documents that need the old glyphs, \usepackage[legacycolonsymbols]{mathtools} is provided. Conversely, \coloneqq and \eqqcolon were never deprecated and give the same output under either setting — when in doubt, write the one with two q's and you are safe. The rest of the family: \dblcolon (::), \Coloneqq (::=), \Eqqcolon, \colonapprox, \colonsim, and the newer \approxcolon and \simcolon.

A long condition under a sum widens the formula — \mathclap and \smashoperator

Put the condition in a zero-width box and the problem goes away. \sum_{1 \le i \le j \le n} a_{ij} measures 48.21771pt across; wrap the condition as \sum_{\mathclap{1 \le i \le j \le n}} a_{ij} and it measures 28.43983pt19.78pt narrower. The characters of the condition do not disappear; they still sit visibly under the summation sign. They simply stop counting toward the width of the formula. \mathclap lets the overhang fall on both sides, \mathllap throws it left and \mathrlap right. The text-mode \clap works on the same idea.

latex
% the wide condition stretches the whole formula
\[ \sum_{1 \le i \le j \le n} a_{ij} \]

% zero-width box: same look, width of the operator only
\[ \sum_{\mathclap{1 \le i \le j \le n}} a_{ij} \]

% same result, but this one can flatten a single side
\[ \smashoperator{\sum_{1 \le i \le j \le n}} a_{ij} \]
\[ \smashoperator[r]{\sum_{1 \le i \le j \le n}} a_{ij} \]

mathtools offers a second tool, \smashoperator. Measured, \smashoperator{\sum_{1 \le i \le j \le n}} gives 28.43983pt — precisely the \mathclap figure. The difference is flexibility. \smashoperator takes the operator and its scripts together and lets you choose which side to flatten with [l], [r] or [lr] (the default). When the summation sign sits at the left margin and must not hang into it, [r] is the answer: measured at 38.32878pt, midway between the two-sided smash and the original. Use \mathclap to aim at one spot inside a formula, and \smashoperator when the operator at the start of the line is the problem.

Scripts on the left of a symbol — \prescript and \adjustlimits

Three arguments — \prescript{above}{below}{base} — attach scripts to the upper left and lower left of a symbol. Isotope notation is the clearest case: \prescript{238}{92}{\mathbf{U}} sets uranium-238 with the mass number 238 at the upper left and the atomic number 92 at the lower left. You can also write {}^{238}_{92}\mathbf{U} by hand, but that hangs the scripts on an empty group, so their position and size are not dependable. When no lower-left script is wanted, leave the second argument as an empty {}. The font of the indices can be swapped through \mathtoolsset's prescript-sub-format and prescript-sup-format.

latex
\[
  \prescript{238}{92}{\mathbf{U}}, \qquad
  \prescript{n}{}{C}_{k}
\]

% limits of two nested operators lined up
\[ \adjustlimits \sum_{i} \sum_{j} a_{ij} \]

One more tool belongs here — unglamorous, but it earns its place. \adjustlimits lines up the lower limits of two nested operators. When scripts of different sizes follow one another, as in \sum_i \sum_j, the second limit can appear to float slightly, and \adjustlimits absorbs that step. Measured: \sum_i\sum_j is 30.55557pt and \adjustlimits\sum_i\sum_j is 32.2222pt — a difference of 1.66663pt, an adjustment of exactly 3mu. It helps just as much where operators of different widths follow one another, as in \lim_{n\to\infty}\sup_{x}.

Aligning matrix columns — pmatrix* and [r], [l], [c]

amsmath's pmatrix always centres its columns. That becomes a problem the moment negative numbers appear: put -1 above 2 and the digits look offset by the width of the minus sign. mathtools supplies a starred version of each environment — matrix*, pmatrix*, bmatrix*, Bmatrix*, vmatrix*, Vmatrix*, smallmatrix* — taking the same column specifier array uses, as an optional argument. Write \begin{pmatrix*}[r] and the columns align right, so the minus signs jut cleanly out to the left. The default is c, so adding the star alone changes nothing. The default alignment of the small variants is set by smallmatrix-align in \mathtoolsset.

latex
\[
  \begin{pmatrix*}[r]
    -1 &  3 \\
     2 & -4
  \end{pmatrix*}
  \qquad
  \begin{pmatrix}
    -1 &  3 \\
     2 & -4
  \end{pmatrix}
\]

Case distinctions and one very long line — dcases, rcases, multlined

amsmath's cases sets its contents in text style, so a fraction inside comes out squashed — \frac{1}{2} shrinks. mathtools' dcases keeps the same syntax but sets the body at display size; the d is for displaystyle. When the brace belongs on the right instead — several conditions leading to one conclusion — use rcases and follow \end{rcases} with \Rightarrow or whatever the conclusion needs. There is a combined drcases, and starred dcases* and rcases* that set the final column as text. If you need a case environment of your own design, \newcases builds one.

latex
\[
  f(x) =
  \begin{dcases}
    \frac{1}{2} & x > 0 \\
    0           & x \le 0
  \end{dcases}
  \qquad
  \begin{rcases}
    a \\ b
  \end{rcases} \Rightarrow c
\]

% [t] aligns on the first row; the second optional argument fixes the width
\[
  \begin{multlined}[t][0.6\textwidth]
    a + b + c + d \\ {} + e + f
  \end{multlined}
\]

multlined is amsmath's multline environment shrunk into a single box. multline claims a whole display to itself, so it cannot be used inside align or on the right of an =. multlined, being an environment, embeds anywhere in a formula and lets you break only the long right-hand side. It takes two optional arguments: the first is the vertical alignment against its surroundings (t, b, or centred by default), the second is the width. Unless you state a width, it takes only as much as it needs. To slip an arrow between two aligned rows, there is \ArrowBetweenLines; to loosen the leading across a whole alignment, the spreadlines environment.

Numbering only the equations you actually reference — showonlyrefs

Write \mathtoolsset{showonlyrefs} and only the equations referenced from the text get a number; the rest lose theirs. In a draft, equation numbers appear and vanish constantly, and numbering an equation nobody refers to sends a reviewer hunting for where “(7)” is used. Compiling a two-equation align under TeX Live 2024 and referencing only one of them with \eqref produced (1) on the referenced equation and no number at all on the other. There is one condition: refer with \eqref, not \ref. If you also need the numbers you set by hand with \tag{} to survive, add showmanualtags.

latex
\mathtoolsset{
  showonlyrefs,        % number only what \eqref points at
  showmanualtags,      % but keep \tag{} numbers visible
  centercolon          % a bare : is set at axis height
}

Which of these come from amsmath, and which from mathtools?

Loading only mathtools blurs the boundary, but it matters when you edit somebody else's document or meet submission rules that permit amsmath and nothing else. The .sty files settle it. The extensible arrows \xrightarrow and \xleftarrow are amsmath (defined in amsmath.sty); what mathtools adds is the rest of the family — the double-shafted \xRightarrow, \xLeftarrow and \xLeftrightarrow, the two-way \xleftrightarrow, the hooked \xhookrightarrow and \xhookleftarrow, the map \xmapsto, the longer \xlongrightarrow, and harpoons. All take the same [below]{above} form. \intertext is amsmath, the tightened \shortintertext is mathtools. \overset, \underset and \substack are amsmath; the square-cornered \overbracket and \underbracket are mathtools.

Command / environmentComes fromWhat it does
\xrightarrowamsmatharrow stretching to its labels; [below]{above}
\xleftrightarrowmathtoolsthe rest of the extensible family: \xRightarrow, \xmapsto and more
\overbracketmathtoolsthe square-cornered counterpart of \overbrace / \underbrace; first optional argument is rule thickness, second the height (0.7ex by default)
\intertextamsmathslots prose between rows without losing the alignment
\shortintertextmathtoolsthe same, with the space above and below tightened
\substackamsmathstacks a script over several lines — the counterpart to squeezing it with \mathclap
\MoveEqLeftmathtoolspushes the first line of an align left so later lines line up under it
\splitfracmathtoolsbreaks an over-long numerator or denominator over two lines