Why is showonlyrefs in mathtools not working this way

mathtools

I want to only show equations that I label/ref later in text. However I am doing something wrong, as there is not equation number shown, although I referenced it:

\documentclass[a4paper]{article}

\usepackage{mathtools}
\mathtoolsset{showonlyrefs}

\begin{document}
Here have an equation:
\begin{align}
A=B\label{eq:AB}\\
C=D
\end{align}
Only number equation \ref{eq:AB}.
\end{document}

which results in

showonlyrefs fails to display equation

Without \mathtoolsset{showonlyrefs} it looks fine but also equation C=D is displayed as (2):

how it looks without showonlyrefs

What should I do to solve this issue? I want that it looks exactly like in the second picture but without the (2).

Best Answer

You need \eqref not \ref to trigger the showonlyrefs processing.

Related Question