# 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.

-
Instead of using the switch the package can be loaded with the according option: \usepackage[showonlyrefs]{mathtools} –  Thorsten Donig Oct 31 '10 at 9:40
@Thorsten: in which version does that syntax work? I tested it with v1.06 and it doesn't work that way. –  Stefan Kottwitz Oct 31 '10 at 17:56
I once read (not in the manual) about "an option" for this setting without testing it. My bad. The information was wrong. There is neither a switch nor a package option. The setting has to be done as you wrote in your corrected answer. –  Thorsten Donig Oct 31 '10 at 21:55
@Thorsten: The option you suggested is meaningful and I guess that the mathtools package might support this in the future. –  Stefan Kottwitz Nov 1 '10 at 0:07
It is at least suggestive. At the moment the package seems only to accept the same options as »amsmath« to pass them. –  Thorsten Donig Nov 1 '10 at 8:34

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}


-