# \Cref (package cleveref) problem with subequations

I found a bug with these three settings used together. Basically, I need to use subequations for (only) one equation. In this example, it is on page 1.

I also need to use \numberwithin{equation}{section} to number my equations.

Finally I use the package cleverref to get things like Theorem 1 without type Theorem myself.

Compile the code below, go to page 4 and click link there. It goes to page 1. But it is expected to go to page 3.

If I comment out \numberwithin{equation}{section}, it is fine, but this style of numbering equations is not what I want.

If I don't use the package cleveref and just use \ref instead of \Cref, it goes to page 3 which is correct. But I lose the advantage of using this package.

I need a solution to this problem but still to have the desired result (style of numbering, the advantage of cleveref...etc).

\documentclass{article}

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

\numberwithin{equation}{section}

\begin{document}

\section{Section 1}

This is a list of equations in page 1.
\begin{subequations}
\begin{align}
a &= b \label{aaaa}  \\
a &= b \label{bbbb}
\end{align}
\end{subequations}

\newpage

This is an equation in page 2.
$$\label{cccc} abcd$$

\newpage

\section{Section 2}

This is section 2 in page 3.
$$\label{dddd} abc$$

\newpage

\Cref{dddd}. Click the link here will go to page 1, which is not
expected.

\end{document}


This is no bug, but just the consequence of the way hyperref and cleveref changes the labels.

\numberwithin{equation}{section} must be done before hyperref and cleveref, i.e. both packages are informed about this counting style.

The link to the second equation abcd works, because it's in the same first section, but after the section is updated, the information isn't. It's very convenient to compile on the console to see the warnings about hyperref's name anchors.

General rule: \numberwithin and \counterwithin (from chngcntr package) should be placed before hyperref and cleveref are included.

\documentclass{article}

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

\begin{document}

\section{Section 1}

This is a list of equations in page 1.
\begin{subequations}
\begin{align}
a &= b \label{aaaa}  \\
a &= b \label{bbbb}
\end{align}
\end{subequations}

\newpage

This is an equation in page 2.
$$\label{cccc} abcd$$

\newpage

\section{Section 2}

This is section 2 in page 3.
$$\label{dddd} abc$$

\newpage

\Cref{cccc}
\Cref{dddd}. Click the link here will go to page 1, which is not
expected.

\end{document}

• So the problem is just the position of \numberwithin{equation}{section}? – user565739 Sep 9 '15 at 9:44
• @user565739: Yes, as I've explained at the top (right now) – user31729 Sep 9 '15 at 9:45
• Maybe not a bug, but surely a misfeature that, in my opinion should be fixed anyway: just as new counters should defined after hyperref has been loaded, changing their representation should too. – egreg Sep 9 '15 at 9:51
• @egreg: Isn't that the \theHcounter stuff meant for? (Not desirable as well, I know) But the basic 'criminal' here is cleveref, in my point of view – user31729 Sep 9 '15 at 9:54
• 'criminal' is a bit harsh, no? I might accept culprit'...though I reckon hyperref and amsmath are accessories to the crime :-) See my answer for details. – Toby Cubitt Jun 15 '17 at 15:53

TLDR: should be fixed in the latest pre-release version (0.21) available from my web-site http://www.dr-qubit.org/latex.html

This one confounded me for a while, because cleveref steers well clear of the hyperref counter-mangling machinery and doesn't touch \numberwithin or the lower-level \@addtoreset at all. A bit of sleuthing in .aux files revealed the bug was worse than reported: using subequations anywhere in the document broke the hyperlink destinations of all subsequent equation environments, even ones that don't use subequations at all!

Turns out the problem was caused by cleveref doing some temporary \@addtoreset's within subequations to get the clever referencing machinery to work there, naively assuming no one would mess with this low-level LaTeX kernel macro to make it do something more than adding a counter to a reset list. Unfortunately, hyperref messes with this low-level LaTeX kernel macro to make it do something more than adding a counter to a reset list. amsmath isn't blameless here, either. None of this would be necessary if its subequations implementation wasn't such a convoluted affair.

I've modified the cleveref code to use its own version of \@addtoreset which is always let to the vanilla kernel macro, even when hyperref` is loaded.

Roll on LaTeX3... (naively hoping the LaTeX3 kernel and document classes will be designed with add-on packages in mind, so one doesn't have to create this kind of unholy mess of macro patch on top of macro patch!)