7

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
\newcounter{newequation}                                   %Added line
\renewcommand{\theequation}{\arabic{newequation}}          %Added line
\stepcounter{newequation}                                  %Added line
\begin{align}
x=10
\label{eq:c} \\ 
\stepcounter{newequation}                                  %Added line
y=20
\label{eq:d} 
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}

?

3
  • 1
    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, 2012 at 9:41
  • Does that work @MarkS.Everitt?
    – qgp07
    Feb 18, 2012 at 10:09
  • 1
    Thanks. It may seem like a petty thing, but it lowers the mental barrier against taking up a question. :)
    – qubyte
    Feb 18, 2012 at 11:06

1 Answer 1

9

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
\@addtoreset{equation}{myequation}
\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
\@addtoreset{equation}{myequation}
\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}
4
  • 1
    Using the page counter may produce wrong results. See this answer for an example.
    – lockstep
    Feb 18, 2012 at 14:39
  • @lockstep Thanks for pointing that out, I removed the corresponding part from my answer.
    – diabonas
    Feb 18, 2012 at 14:44
  • 1
    This can be automated using \usepackage{etoolbox}\preto\align{\stepcounter}{myequation}}.
    – Werner
    Feb 19, 2012 at 7:47
  • This hyperref option hypertexnames=false does few magics too. source: here
    – Khaaba
    Sep 15, 2018 at 12:12

You must log in to answer this question.

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