使用 `hyperref` 交叉引用链接到错误的方程式

使用 `hyperref` 交叉引用链接到错误的方程式

我定义了两个宏,用于设置文档中方程编号的上下文。一个宏通过将方程计数器重置为 0 并将方程分组设置为作为参数提供的字符串,建立我称之为“块”的东西,在其中对方程进行编号,

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

另一个设置部分(使用\setcounter{section}),然后调用我的第一个块设置宏:

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

我出于各种原因使用这些函数,以获得对各个元素(不仅仅是方程式)编号的独立于章节的控制,并且它们用于mathtools按预期标记我的所有方程式。但是hyperref这些方程式的交叉引用链接无法正常工作。交叉引用似乎表现得好像它们仅在章节内编号,而忽略了“块”。

我已经盯着我的代码一段时间了,但仍然很困惑,希望能够帮助我找出我做错的地方。


\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}

答案1

包裹更hyperref喜欢获得唯一的目的地名称,请参阅我的答案\theH<counter>\the<counter>更改 namedst 默认名称

由于您的方程式可以在节或分组块内编号,因此可以将数字/字符串添加到\theHequation,例如:

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

答案2

这详细阐述了 Heiko 的回答。

hyperref\Hy@AnchorName需要一个唯一的锚点(由within引用hyperref.sty)。查看您的示例没有

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

.aux在您的文件中显示以下内容:

%...
\newlabel{eq:X}{{10:1}{1}{\relax }{equation.10.1}{}}
\newlabel{eq:Y}{{10:2}{1}{\relax }{equation.10.2}{}}
\newlabel{eq:Example1}{{A:1}{2}{\relax }{equation.10.1}{}}
\newlabel{eq:Example2}{{A:2}{2}{\relax }{equation.10.2}{}}
\newlabel{eq:Example3}{{A:3}{2}{\relax }{equation.10.3}{}}
\newlabel{eq:Z}{{20:1}{3}{\relax }{equation.20.1}{}}
\newlabel{eq:Example4}{{B:1}{4}{\relax }{equation.20.1}{}}

#4注意 的第二个参数中的第四个参数\newlabel{..}{{#1}{#2}{#3}{#4}{#5}}。这个锚点或目的地必须是唯一的,而你的显然不是,这会导致错误的超跳。

添加信息两个都每个标签的部分和组都具有唯一的锚点/目标:

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

这是更新的.aux

%...
\newlabel{eq:X}{{10:1}{1}{\relax }{equation.10.10:1}{}}
\newlabel{eq:Y}{{10:2}{1}{\relax }{equation.10.10:2}{}}
\newlabel{eq:Example1}{{A:1}{2}{\relax }{equation.10.A:1}{}}
\newlabel{eq:Example2}{{A:2}{2}{\relax }{equation.10.A:2}{}}
\newlabel{eq:Example3}{{A:3}{2}{\relax }{equation.10.A:3}{}}
\newlabel{eq:Z}{{20:1}{3}{\relax }{equation.20.20:1}{}}
\newlabel{eq:Example4}{{B:1}{4}{\relax }{equation.20.B:1}{}}

显示出明显独特的锚点/目的地。

相关内容