[Tex/LaTex] Cross-references linking to wrong equations using `hyperref`

equationshyperrefmathtools

I have two macros I've defined that I use to set the context in which equations are numbered in my document. One that establishes something I call a 'block' in which equations are numbered, by resetting the equation counter to 0 and setting the equation grouping to a string provided as an argument,

\newcommand{\setgroupingblock}[1]{\gdef\equationgrouping{#1:}\phantomsection\setcounter{equation}{0}}

and another that sets the section (using \setcounter{section}) and then calls my first, block-setting macro:

\newcommand{\setgroupingsection}[1]{\setcounter{section}{#1}\setgroupingblock{#1}}

I use these for a variety of reasons, to gain section-independent control over numbering of various elements (not just equations), and they use mathtools to label all of my equations as expected. However hyperref cross-reference links to these equations do not work correctly. Cross-references seem to behave as if they are numbered within sections only, ignoring 'blocks'.

I've stared at my code for a while now, but am stumped and would appreciate help sorting out what I'm doing wrong.


\documentclass{article}

\usepackage{mathtools}
\usepackage{hyperref}

% Manually set the section; set a new 'block'
\newcommand{\setgroupingsection}[1]{\setcounter{section}{#1}\setgroupingblock{#1}}
% Set a 'block' for numbering equations, overriding any section, and resetting the equation counter
\newcommand{\setgroupingblock}[1]{\gdef\equationgrouping{#1:}\phantomsection\setcounter{equation}{0}}

% Equation numbering and referencing
\renewcommand{\theequation}{\equationgrouping\arabic{equation}}
\gdef\equationgrouping{}


\begin{document}

\setgroupingsection{10} Section \textbf{10} begins here. Subsequent equations are numbered within this section, until another section is started, or a block is started.

\begin{equation}
q(x)=f(x)\cdot g(x)\label{eq:X}
\end{equation}
\begin{equation}
a(i,j)=a(i,q)\cdot a(p,j)\label{eq:Y}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

\setgroupingblock{A} Block \textbf{A} begins here. Subsequent equations are numbered within this block, until another block is started, or another section is started.

\begin{equation}
f(x)=x^{2}+3x\label{eq:Example1}
\end{equation}
\begin{equation}
g(x)=\sin(\gamma^{2})\label{eq:Example2}
\end{equation}
\begin{equation}
f(x)=\cos(\delta^{2})\label{eq:Example3}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

\setgroupingsection{20} Section \textbf{20} begins here.

\begin{equation}
a(i,j)=a(i,q)\cdot a(p,j)\label{eq:Z}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

\setgroupingblock{B} Block \textbf{B} begins here.

\begin{equation}
f(x)=x^{2}+3x\label{eq:Example4}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

Numbering of equations works as desired, but hyperlinks in cross-references do not, Cross-references seem to behave as if they are numbered within sections only, ignoring blocks. For example the links to the first and second equations in block \textbf{A},\eqref{eq:Example1} and \eqref{eq:Example2} go to the corresponding equations in the most recent section before block \textbf{A}, \eqref{eq:X} and \eqref{eq:Y}; though the reference to \eqref{eq:Example3} works correctly (there is no corresponding equation in section \textbf{10} to "confuse" it with. Similarly \eqref{eq:Example4} links to \eqref{eq:Z}.

\end{document}

Best Answer

Package hyperref prefers \theH<counter> over \the<counter> to get unique destination names, see my answer for Change nameddest default name.

Since your equations can be numbered within section or grouping blocks, both numbers/strings can be added to \theHequation, e.g.:

\renewcommand{\theHequation}{\theHsection.\equationgrouping\arabic{equation}}
Related Question