# Trouble with hyperref when resetting the counter for equations (specifically with the align environment)

I am trying to reset a counter to zero; however hyperref tries to reference previous equations. I am using:

\setcounter{equation}{0}


And if I have labels in say align environments:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\begin{align}
x=1
\label{eq:a} \\
y=2
\label{eq:b}
\end{align}
Eq.~\eqref{eq:a}.
Reset counter
\newpage
\setcounter{equation}{0}
\begin{align}
x=10
\label{eq:c} \\
y=20
\label{eq:d}
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}


If I now \ref{eq:c}, the hyperref package will point me back to eq:a (in text, they both have the same number).

Is there a way to fix this without manually stepping a new counter you make? I.e. can you fix it without doing something like this:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\begin{align}
x=1
\label{eq:a} \\
y=2
\label{eq:b}
\end{align}
Eq.~\eqref{eq:a}.
Reset counter
\newpage
%% \setcounter{equation}{0}                                %Commented out line
\begin{align}
x=10
\label{eq:c} \\
y=20
\label{eq:d}
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}


?

-
Could you provide a minimal working example please? It makes things easier if we have some complete code to copy and play with. –  qubyte Feb 18 '12 at 9:41
Does that work @MarkS.Everitt? –  thequark Feb 18 '12 at 10:09
Thanks. It may seem like a petty thing, but it lowers the mental barrier against taking up a question. :) –  qubyte Feb 18 '12 at 11:06

To produce correct references, you have to make the counter values unique to hyperref. To do so, you can introduce a parent counter myequation: Each time this counter is increased, the counter equation is reset. Like this, hyperref can take into account both the value of myequation and equation to produce the correct hyperlink.

In order to do this, add the following code to your document after having loaded hyperref:

\newcounter{myequation}
\makeatletter
\makeatother


Resetting the equation counter is done by stepping myequation, so use \stepcounter{myequation} instead of \setcounter{equation}{0}.

### Full example code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\newcounter{myequation}
\makeatletter
\makeatother
\begin{document}
\begin{align}
x=1
\label{eq:a} \\
y=2
\label{eq:b}
\end{align}
Eq.~\eqref{eq:a}.
Reset counter
\newpage
\stepcounter{myequation}
\begin{align}
x=10
\label{eq:c} \\
y=20
\label{eq:d}
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}

-
Using the page counter may produce wrong results. See this answer for an example. –  lockstep Feb 18 '12 at 14:39
@lockstep Thanks for pointing that out, I removed the corresponding part from my answer. –  diabonas Feb 18 '12 at 14:44
This can be automated using \usepackage{etoolbox}\preto\align{\stepcounter}{myequation}}. –  Werner Feb 19 '12 at 7:47