2

I have a document using various packages including hyperref, cleveref and the standard AMS math packages. Within a section, I tagged my first equation to give it a custom label, but not other ones, a la

\begin{equation} \label{special-eq}\tag{ABC}
A + B = C
\end{equation}

\begin{equation} \label{eq1}
f(x) = g(x)
\end{equation}

The issue is that when I refer to eq1, via \eqref{eq1}, \cref{eq1} or \ref{eq1} the correct equation number (say (1)) appears in text, but the hyperlink generated to this equation takes me to equation (ABC).

It appears that hyperref is getting confused by my custom tag. Is there a way to correct the hyperlinks? (without doing them manually of course)

1
  • 1
    show a complete example. Apr 29, 2019 at 19:28

1 Answer 1

3

The hyperref package uses separate counters for environments to create link destinations. These counters are based on the regular counters, for example the regular equation counter is \theequation and hyperref defines \theHequation which creates link destinations containing the equation counter value, such as name{equation.0.1}.

However, when you use tags then the regular equation counter is not updated, therefore two or more equations have the same counter value. This confuses hyperref, because it cannot create two link targets with the same name. The second destination identifier is therefore ignored and the link will point to the first. A warning is issued in the terminal and in the log file to indicate the issue, something like pdfTeX warning (ext4): destination with the same identifier (name{equation.0.1}) has been already used, duplicate ignored.

To prevent the issue you can modify the hyperref counter after the equation with the tag, to create a unique name (see also Manually set destination name for pdflatex). Note that you need to do that after each tagged equation.

MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{lipsum}
\begin{document}
\begin{equation} \label{special-eq}\tag{ABC}
A + B = C
\end{equation}
\renewcommand*{\theHequation}{notag.\theequation}
\lipsum[1]

\begin{equation} \label{eq1}
f(x) = g(x)
\end{equation}

\begin{equation} \label{special-eq2}\tag{DEF}
D + E = F
\end{equation}
\renewcommand*{\theHequation}{notag2.\theequation}

\begin{equation} \label{eq2}
g(x) = h(x)
\end{equation}

see \Cref{special-eq} and \Cref{eq1} and \Cref{eq2}.
\end{document}

To automate it a bit more you can introduce a new counter that is increased for each equation, tagged and regular, and use that counter in your hyperref destination name. Increasing the counter can be done by patching the equation environment with the etoolbox package, in the example below pretocmd is used which means that some code is added in front of the command or environment. In this case you only need to redefine the hyperref counter once, instead of after each tagged equation.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{lipsum}
\usepackage{etoolbox}
\begin{document}
\newcounter{taggedeq}
\setcounter{taggedeq}{0}
\pretocmd{\equation}{\stepcounter{taggedeq}}{}{}
\renewcommand*{\theHequation}{\thetaggedeq.\theequation}
\begin{equation} \label{special-eq}\tag{ABC}
A + B = C
\end{equation}

\lipsum[1]

\begin{equation} \label{eq1}
f(x) = g(x)
\end{equation}

\begin{equation} \label{special-eq2}\tag{DEF}
D + E = F
\end{equation}

\begin{equation} \label{eq2}
g(x) = h(x)
\end{equation}

see \Cref{special-eq} and \Cref{eq1} and \Cref{eq2} and \Cref{special-eq2}.

\lipsum
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .