# How do I number equations only if they are referred to in the text?

I have a paper where all equations are numbered, but I want to number equations only if they are actually referred to in the .tex file using \ref{}. How can I make that happen? Alternatively, how can I make all equations that have no \label next to them (because then I know I don't refer to them) not have a number? Do I have to use \nonumber separately for each such equation?

• See also this question – Lev Bishop Oct 31 '10 at 2:10
• I hope you don't mind that I tried to enhance your question a bit. – Hendrik Vogt Oct 31 '10 at 7:11
• It looks like wanting to number the equations with an internal reference only is not a good habit. It would be somehow similar to numbering specific pages only for personal reasons. Readers of your document may need to refer to an unnumbered equation which is very annoying. – pluton Oct 31 '10 at 21:21
• @pluton: no more annoying, surely, than to refer to random lines of prose, or snippets of math inlined in the text? These arguments for “number all equations” always seem to me like they’re really arguments for “include line numbers throughout”. (And conversely, the main arguments against line numbering — clutter and distraction — seem equally to be arguments against numbering all equations; if anything more so, since the consistency of line numbers makes them comparatively unobtrusive.) – Peter LeFanu Lumsdaine Dec 20 '10 at 21:04

The mathtools package provides a switch showonlyrefs. You could use that to achieve that only those equations would be numbered which are referred to in the text.

Usage:

\mathtoolsset{showonlyrefs}


or

\mathtoolsset{showonlyrefs=true}


or set it to false this way if desired.

mathtools with showonlyrefs will not work with the cleveref package, so if you rely on that package for your cross-references, it would be better to use autonum. If one wants to use hyperref as well, the loading order must be hyperref - cleveref - autonum, as described in the manual.

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{autonum}
\begin{document}

$$a = b \label{eq:a}$$

$$c = d \label{eq:b}$$

$$e = f \label{eq:c}$$

See \cref{eq:b}.
\end{document}


• According to @egreg, this absolutely does not work anymore currently with biblatex, since autonum loads etextools, which seems to be forbidden by biblatex by now. – Alex Povel Jul 1 '19 at 10:37