[Tex/LaTex] Hyperref jumps to the wrong equation if the equation has a \tag and cleveref is used

amsmathcleverefhyperreflinkspositioning

The problem

I'm writing a document where all of my equations are labeled, not by numbers, but by names, which I provide with amsmath's \tag command. I also use cleveref for ease of cross-referencing. And I just today decided I'd add hyperref. While this didn't break anything old (at least, not after I'd figured out where to put it), it didn't completely work: links to equations jump incorrectly. Why is this? And how can I fix it?

Edit: The extended problem. This isn't just with equations, it's with all mathematical environments. I make use of equation, align, align*, alignat, align*, and gather, and these all need to support appropriate links. And unfortunately, I just realized this, and the due date for this document is bearing down on me, which is my own fault; but the solution is sufficiently important to me that I'm adding a bounty. (The remainder of this question just refers to the shown MWE.tex; however, the problem is more general.)

An example

In the following document, all three \eqref links jump to the first equation:

\documentclass{book}

\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

\begin{equation}\label{eq:first}\tag{first}
  \text{First equation}
\end{equation}

\begin{equation}\label{eq:second}\tag{second}
  \text{Second equation}
\end{equation}

\begin{equation}\label{eq:third}\tag{third}
  \text{Third equation}
\end{equation}

\begin{itemize}
  \item A reference to~\eqref{eq:first}.
  \item A reference to~\eqref{eq:second}.
  \item A reference to~\eqref{eq:third}.
\end{itemize}

\newpage

For scrolling purposes.

\end{document}

If I either remove \usepackage{cleveref} or remove the \tags, everything works fine. But with both of them together, everything jumps to the first equation.

Compilation messages

With \usepackage{cleveref} and \tags

When I compile this example (in MWE.tex), I get the following warning messages in the log:

[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}pdfTeX warning (ext4): des
tination with the same identifier (name{equation.0.0.1}) has been already used,
 duplicate ignored

\AtBegShi@Output ...ipout \box \AtBeginShipoutBox 
                                                  \fi \fi 
l.27 \newpage
             pdfTeX warning (ext4): destination with the same identifier (name{
equation.0.0.1}) has been already used, duplicate ignored

\AtBegShi@Output ...ipout \box \AtBeginShipoutBox 
                                                  \fi \fi 
l.27 \newpage
             ] [2] (./MWE.aux) )

And sure enough, if I look at MWE.aux, it contains the following lines:

\newlabel{eq:first}{{{first}}{1}{\relax }{equation.0.0.1}{}}
\newlabel{eq:first@cref}{{[equation][2147483647][]{first}}{1}}
\newlabel{eq:second}{{{second}}{1}{\relax }{equation.0.0.1}{}}
\newlabel{eq:second@cref}{{[equation][2147483647][]{second}}{1}}
\newlabel{eq:third}{{{third}}{1}{\relax }{equation.0.0.1}{}}
\newlabel{eq:third@cref}{{[equation][2147483647][]{third}}{1}}

With just \tags (no \usepackage{cleveref})

If I compile without cleveref, then I get the same warning:

[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}pdfTeX warning (ext4): des
tination with the same identifier (name{equation.0.0.1}) has been already used,
 duplicate ignored

\AtBegShi@Output ...ipout \box \AtBeginShipoutBox 
                                                  \fi \fi 
l.27 \newpage
             pdfTeX warning (ext4): destination with the same identifier (name{
equation.0.0.1}) has been already used, duplicate ignored

\AtBegShi@Output ...ipout \box \AtBeginShipoutBox 
                                                  \fi \fi 
l.27 \newpage
             ] [2] (./MWE.aux) )

However, the links work correctly, and the aux file contains

\newlabel{eq:first}{{{first}}{1}{\relax }{AMS.1}{}}
\newlabel{eq:second}{{{second}}{1}{\relax }{AMS.2}{}}
\newlabel{eq:third}{{{third}}{1}{\relax }{AMS.3}{}}

With just \usepackage{cleveref}s (no \tags)

If I compile without using \tags, then I get no warning, the links work, and my aux file contains

\newlabel{eq:first}{{1}{1}{\relax }{equation.0.0.1}{}}
\newlabel{eq:first@cref}{{[equation][1][0]1}{1}}
\newlabel{eq:second}{{2}{1}{\relax }{equation.0.0.2}{}}
\newlabel{eq:second@cref}{{[equation][2][0]2}{1}}
\newlabel{eq:third}{{3}{1}{\relax }{equation.0.0.3}{}}
\newlabel{eq:third@cref}{{[equation][3][0]3}{1}}

With nothing (neither \usepackage{cleveref} nor \tags)

If I compile without using either \usepackage{cleveref} or \tags, then I get no warning, the links work, and my aux file contains

\newlabel{eq:first}{{1}{1}{\relax }{equation.0.0.1}{}}
\newlabel{eq:second}{{2}{1}{\relax }{equation.0.0.2}{}}
\newlabel{eq:third}{{3}{1}{\relax }{equation.0.0.3}{}}

Without \usepackage{hyperref}, but with everything else

If I instead remove just `\usepackage{hyperref}, then I get no warning, there are no links, and my aux file contains

\newlabel{eq:first}{{{first}}{1}}
\newlabel{eq:first@cref}{{[equation][2147483647][]{first}}{1}}
\newlabel{eq:second}{{{second}}{1}}
\newlabel{eq:second@cref}{{[equation][2147483647][]{second}}{1}}
\newlabel{eq:third}{{{third}}{1}}
\newlabel{eq:third@cref}{{[equation][2147483647][]{third}}{1}}

Replacing equation with gather

The hyperref README reports that the equation environment "[isn't] supported too well," and suggests replacing it with the gather environment. Doing that successfully gets rid of the error message, but doesn't fix the behavior of the links; they still jump to the wrong place. The aux file contains

\newlabel{eq:first}{{{first}}{1}{\relax }{Doc-Start}{}}
\newlabel{eq:first@cref}{{[equation][2147483647][]{first}}{1}}
\newlabel{eq:second}{{{second}}{1}{\relax }{Doc-Start}{}}
\newlabel{eq:second@cref}{{[equation][2147483647][]{second}}{1}}
\newlabel{eq:third}{{{third}}{1}{\relax }{Doc-Start}{}}
\newlabel{eq:third@cref}{{[equation][2147483647][]{third}}{1}}

What's happening?

Admittedly, that's part of my question, but it looks to me like hyperref and \tag don't play well together: if I have both, I get the error message about duplicates. But it doesn't seem to cause any problems in the document unless cleveref is present as well.

I should also add a couple of caveats:

  • This is an 85-page document. I really don't want to deal with changing out any of my packages at this point, but if it works….

  • I'm compiling this document on a school machine, and so I don't have control over the main TeX install. I have installed some updated versions to my local texmf tree, however. Here are all the package versions in my log file:

    • amstext 2000/06/29 v2.01
    • amsbsy 1999/11/29 v1.2d
    • amsopn 1999/12/14 v2.01
    • hyperref 2011/04/09 v6.82f
    • ltxcmds 2011/11/09 v1.22
    • ifpdf 2011/01/30 v2.3
    • pdftexcmds 2011/11/29 v0.20
    • infwarerr 2010/04/08 v1.3
    • ifluatex 2010/03/01 v1.3
    • keyval 1999/03/16 v1.13
    • kvsetkeys 2012/04/25 v1.16
    • etexcmds 2011/02/16 v1.5
    • pdfescape 2011/11/25 v1.13
    • ifvtex 2010/03/01 v1.5
    • ifxetex 2009/01/23 v0.5
    • hycolor 2011/01/30 v1.7
    • xcolor-patch 2011/01/30
    • hopatch 2011/06/24 v1.1
    • letltxmacro 2010/09/02 v1.4
    • kvoptions 2011/06/30 v3.11
    • intcalc 2007/09/27 v1.1
    • url 2006/04/12 ver 3.3
    • bitset 2011/01/30 v1.1
    • bigintcalc 2012/04/08 v1.3
    • atbegshi 2011/10/05 v1.16
    • atveryend 2011/06/30 v1.8
    • rerunfilecheck 2011/04/15
    • uniquecounter 2011/01/30 v1.2
    • cleveref 2012/03/07{} v0.18.5{}
    • nameref 2010/04/30 v2.40
    • refcount 2011/10/16 v3.4

Best Answer

I have a solution. However this might not be the most elegant way.

You have to define your own numbering scheme for hyperref so that each number is unique:

\listfiles
\documentclass{book}

\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

%%% define a new counter and use it for \theHequation which hyperref uses for the links
\newcounter{Hequation}
\renewcommand{\theHequation}{\thechapter.\arabic{Hequation}}
\makeatletter
\g@addto@macro\equation{\stepcounter{Hequation}}
\makeatother

\begin{document}
\begin{equation}\tag{first}\label{eq:first}
   \text{First equation}
\end{equation}

\begin{equation}\tag{second}\label{eq:second}
   \text{Second equation}
\end{equation}

\begin{equation}\label{eq:firstreal}
   \text{First equation without tag}
\end{equation}

\begin{equation}\tag{third}\label{eq:third}
   \text{Third equation}
\end{equation}

\begin{equation}\label{eq:secondreal}
   \text{Second equation without tag}
\end{equation}

\begin{itemize}
   \item A reference to~\eqref{eq:first}.
   \item A reference to~\eqref{eq:second}.
   \item A reference to~\eqref{eq:third}.
   \item A reference to~\eqref{eq:firstreal}.
   \item A reference to~\eqref{eq:secondreal}.
\end{itemize}

\newpage

For scrolling purposes.

\end{document}

As you can see \equation has to be redefined as well. Then equations with and without \tag can be mixed-up without any problems.

Best regards
Martin