代码

代码

哪个包会影响 hyperref\theHfigure标签值?我该如何扭转这种局面?

我希望改变hyperref counter-specific label使用

\renewcommand*{\theHfigure}{runningfigurecounter.\the\value{runningfigurecounter}}

但令我惊讶的是,该.aux文件并不像预期的那样包含标签。相反,第二个参数的第四个参数(标签)\newlabel通常如下所示

\newlabel{figure:somelabel}{{1}{1}{A title}{hyperref counter-specific label}{random parameter that is empty until the cows home}}

包含如下内容:

figure.caption.5

预期是这样的:

runningfigurecounter.5

代码

\documentclass{article}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{hyperref}

% Running Counter
\newcounter{runningfigurecounter}

% Redefine hyperref \newlabel counters for unique targets with disregard for the printed form
\renewcommand{\theHfigure}{runningfigurecounter.\the\value{runningfigurecounter}}

% Apply \refstepcounter in macros called in the document
\let\oldfigure\figure
\renewcommand{\figure}{\refstepcounter{runningfigurecounter}\oldfigure}


% Counter For TeX Loop (these are not global)
\newcount\STEP
\newcount\STEPTOTAL
\STEPTOTAL=5

\begin{document}

References:
\STEP=0
\loop
\advance\STEP 1
\ref{fig:\the\STEP}
\ifnum\STEP<\STEPTOTAL
\repeat


% TeX Loop to Generate Figures and Labels
\STEP=0
\loop
\advance\STEP 1
\newpage
\begin{figure}
\caption{Another fake picture. \textbackslash theHfigure = \texttt{\theHfigure}}
\label{fig:\the\STEP}
\end{figure}
\lipsum
\ifnum\STEP<\STEPTOTAL
\repeat

\end{document}

PDF 输出剪辑

在此处输入图片描述

.aux 中的相关条目

正如您在此处看到的,条目是figure.caption.1(thru 5) 而不是runningfigurecounter.1(thru 5)。

\newlabel{fig:1}{{1}{2}{Another fake picture. \textbackslash theHfigure = \texttt {\theHfigure }\relax }{figure.caption.1}{}}

\newlabel{fig:2}{{2}{4}{Another fake picture. \textbackslash theHfigure = \texttt {\theHfigure }\relax }{figure.caption.2}{}}

\newlabel{fig:3}{{3}{6}{Another fake picture. \textbackslash theHfigure = \texttt {\theHfigure }\relax }{figure.caption.3}{}}

\newlabel{fig:4}{{4}{8}{Another fake picture. \textbackslash theHfigure = \texttt {\theHfigure }\relax }{figure.caption.4}{}}

\newlabel{fig:5}{{5}{10}{Another fake picture. \textbackslash theHfigure = \texttt {\theHfigure }\relax }{figure.caption.5}{}}

答案1

caption包定义

   \renewcommand*\caption@makestart[1]{%
          \begingroup
          \Hy@hypertexnamesfalse
          \hyper@makecurrent{#1.caption}%
          \endgroup
          \caption@Debug{hypcap start=\@currentHref}}%
    }

它提供了锚点名称的前缀#1.caption,其中#1被当前的 替换\@captype,即figure此处。

这应该被替换\hyper@makecurrent{\csname theH#1\endcsname},然后应用“正确”的\theH...宏。

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{caption}

\usepackage{hyperref}

% Running Counters
\newcounter{runningfigurecounter}
\newcounter{runningsectioncounter}


% Redefine hyperref \newlabel counters for unique targets with disregard for the printed form
\renewcommand{\theHfigure}{runningfigurecounter.\therunningfigurecounter}

% Apply \refstepcounter in macros called in the document
\let\oldfigure\figure
\let\oldsection\section
\AtBeginEnvironment{figure}{%
  \refstepcounter{runningfigurecounter}%
}

% Counter For TeX Loop (these are not global)
\newcount\STEP
\newcount\STEPTOTAL
\STEPTOTAL=10

\makeatletter

\AtBeginDocument{%
    \renewcommand*\caption@makestart[1]{%
      \begingroup
      \Hy@hypertexnamesfalse
      \hyper@makecurrent{\csname theH#1\endcsname}%
      \endgroup
      \caption@Debug{hypcap start=\@currentHref}}%
}
\makeatother


\begin{document}


References:
\STEP=0
\loop
\advance\STEP 1
\ref{fig:\the\STEP}
\ifnum\STEP<\STEPTOTAL
\repeat


% TeX Loop to Generate Figures and Labels
\STEP=0
\loop
\advance\STEP 1
\newpage
\begin{figure}
  \caption{Another fake picture. \textbackslash theHfigure = \texttt{\theHfigure}}
  \label{fig:\the\STEP}
\end{figure}
\lipsum
\ifnum\STEP<\STEPTOTAL
\repeat

\end{document}

相关内容