etoolbox 与 hyperref 不兼容

etoolbox 与 hyperref 不兼容

我尝试整理 Werner 的第二个回答自动编号不等式使用hyperref包。但是,第一次运行会出现以下错误:

Runaway argument?
{\@firstoffive }\fi ), you will notice it is different from (\ref {re\ETC.
! Paragraph ended before \Hy@setref@link was complete.
<to be read again> 
                   \par 

尽管第二次运行没有给我任何错误,但不等式引用不起作用,即我得到了??而不是i和ii。

如果我不包含该hyperref包,那么代码就可以编译得很好。

为了完整起见,这里是 MWE

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{hyperref}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

\newcounter{numrel}% Counter for numering relations
\renewcommand{\thenumrel}{\roman{numrel}}% Counter numrel uses lowercase roman numerals
\let\textlabel\label% Use text \label rather than math \label
\newcommand{\numrel}[2]{% Relation numbering
  \begingroup%
  \refstepcounter{numrel}% Increment numrel counter and create correct reference hook
  \textlabel{#2}\endgroup% Label numrel counter
  \ensuremath{\stackrel{(\thenumrel)}{#1}}% Print counter + relation
}
\AfterEndEnvironment{align*}{\setcounter{numrel}{0}}% Resets numrel at the end of align*

\begin{document}
\section{First section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\stackrel{(i)}{\leq} cx^3+dx^2+ex+f \\
       &\stackrel{(ii)}{<} gx^4+hx^3+ix^2+jx+k  
\end{align*}
\section{Second section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel1} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel2} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel1}), you will notice it is different from (\ref{rel2}).
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel3} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel4} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel3}), you will notice it is different from (\ref{rel4}).

\end{document}

答案1

该问题与 无关etoolbox,而是与 标签的管理align以及其他对齐环境有关amsmath

您想要的是避免使用由内部\label重新定义的,它已经可用作;由于您还重置了计数器,因此有必要使用技巧才能获得正确的链接:定义和步进计数器是所需要的。amsmathalign\ltx@labelhyperrefHnumrel

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{hyperref}

\newcounter{numrel}% Counter for numering relations
\newcounter{Hnumrel}% Keep hyperref happy and don't duplicate anchors
\renewcommand{\thenumrel}{\roman{numrel}}% Counter numrel uses lowercase roman numerals

\makeatletter
\newcommand{\numrel}[2]{% Relation numbering
  \refstepcounter{numrel}% Increment numrel counter and create correct reference hook
  \stepcounter{Hnumrel}%
  \ifmeasuring@\else\ltx@label{#2}\fi % Label numrel counter (issue only once)
  \overset{\text{(\thenumrel)}}{#1}% Print counter + relation
}
\makeatother
\AfterEndEnvironment{align*}{\setcounter{numrel}{0}}% Resets numrel at the end of align*

\begin{document}

\section{A section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel1} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel2} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel1}), you will notice it is different from (\ref{rel2}).
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel3} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel4} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel3}), you will notice it is different from (\ref{rel4}).

\end{document}

在此处输入图片描述

答案2

很多hyperref内容都是在文档开头加载的。如果我对代码进行这样的更改,我们的代码就可以在我的系统上编译

\AtBeginDocument{\let\textlabel\label}%

然后它似乎就存储了正确的文本\label

相关内容