[Tex/LaTex] Hyperlinking problems when using subequations, hyperref and cleveref

cleverefhyperrefsubequations

The problem

Hi. I'm currently writing my thesis and have my equations numbered within sections. I also use subequations from amsmath. When I use amsmath, hyperref and cleveref together, however, I get 2 warnings about destinations with the same identifier. The result is that \cref{eq:2a} gets the correct number in the pdf but the hyperlink points to eq:1a.

A MWE

\documentclass{book}

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

\numberwithin{equation}{section}

\begin{document}
This is a test.

\section{Section 1}

\begin{subequations}\label{eq:1}
\begin{align}
x=y\label{eq:1a}
\end{align}
\end{subequations}

\section{An error}

\begin{subequations}\label{eq:2}
\begin{align}
1=2\label{eq:2a}
\end{align}
\end{subequations}

\end{document}

The aux

Sure enough,

\newlabel{eq:1}{{0.1.1}{1}{Section 1\relax }{equation.0.0.1.1}{}}
\newlabel{eq:1@cref}{{[equation][1][0,1]0.1.1}{1}}
\newlabel{eq:1a}{{0.1.1a}{1}{Section 1\relax }{equation.0.0.1a}{}}
\newlabel{eq:1a@cref}{{[equation][1][0,1]0.1.1a}{1}}
\@writefile{toc}{\contentsline {section}{\numberline {0.2}An error}{1}{section.0.2}}
\newlabel{eq:2}{{0.2.1}{1}{An error\relax }{equation.0.0.1.1}{}}
\newlabel{eq:2@cref}{{[equation][1][0,2]0.2.1}{1}}
\newlabel{eq:2a}{{0.2.1a}{1}{An error\relax }{equation.0.0.1a}{}}
\newlabel{eq:2a@cref}{{[equation][1][0,1]0.2.1a}{1}}

Warnings

pdfTeX warning (ext4): destination with the same identifier (name{equation.0.0.1a}) has been already used, duplicate ignored
pdfTeX warning (ext4): destination with the same identifier (name{equation.0.0.1.1}) has been already used, duplicate ignored

What to do?

All packages have been updated too. So I've tried a few things that can get this to work without any warnings but none is an ideal solution from my point of view.

  1. Remove all subequations
  2. Remove line \numberwithin{equation}{section}
  3. Remove package cleveref
  4. Remove package hyperref

This in no doubt related to Hyperref jumps to the wrong equation if the equation has a \tag and cleveref is used and also Cross-references linking to wrong equations using `hyperref` but I can't manage to use those Q&As to help me here.

I'd really appreciate it if anybody could shed some light on this issue or suggest a workaround.

Thanks in advance!

Best Answer

So I found the following solution by playing around with when packages and lines are called and it turns out that if the \numberwithin{}{} line is called before \cleveref, there are no warnings and hyperlinks work as expected, i.e.

\documentclass{book}

\usepackage{amsmath}
\usepackage{hyperref}
\numberwithin{equation}{section}
\usepackage{cleveref}

\begin{document}
...
\end{document}

I have no idea what's going on beneath the hood of these packages so I'd be curious to know why this works. It seems (to me at least) that this is quite strange behaviour between hyperref and cleveref but perhaps this is to be expected.

In the mean time, thanks to all those who had a look at this problem for me!

Related Question