\使用 hyperref 时带标签的感谢

\使用 hyperref 时带标签的感谢

我想\label \ref在标准类的标题材料中使用配对。因此,我\thanks使用可选提供的标签名称(例如)扩展了宏\thanks[label]{Thanks}。然后我可以\ref在标题材料中使用,例如\author{Author \ref{label}}。当我加载时,这会失败。但是,如果我在之后hyperref使用宏,它仍然有效\thanks\begin{document}

\documentclass{article}

\makeatletter
\renewcommand\thefootnote{\textsuperscript{\@fnsymbol\c@footnote}} % just for the look
\DeclareRobustCommand\thanks[2][]{%
  \if\relax#1\relax% check if no optional argument
    \footnotemark% use normal thanks
  \else%
    \protect\refstepcounter{footnote}\protect\label{#1} % add footnote label
  \fi%
  \protected@xdef\@thanks{% copy paste from original code
    \@thanks\protect\footnotetext[\the\c@footnote]{#2}%
  }%
}
\makeatother

\usepackage{hyperref} % everything works without hyperref

\title{Title}
\author{Author\ref{a}} % should add a footnotemark

\thanks[a]{Thanks} % Footnote text works only outside the header if hyperref is loaded
\begin{document}
\maketitle
\end{document}

在使用时如何使修改后的\thanks工作在标题中起作用hyperref

答案1

根据 Ulrike Fischer 的评论,我提出了以下解决方案。

\documentclass{article}

\makeatletter
\renewcommand\thefootnote{\textsuperscript{\@fnsymbol\c@footnote}}
\let\old@thanks\thanks % save the normal thanks
\DeclareRobustCommand\thanks[2][]{% redefine thanks
  \AddToHook{begindocument/end}{% but postpone the change until it is needed
    \if\relax#1\relax%
      \footnotemark%
    \else%
      \protect\refstepcounter{footnote}\protect\label{#1}%
    \fi%
    \protected@xdef\@thanks{%
      \@thanks\protect\footnotetext[\the\c@footnote]{#2}%
    }%
  }%
}
\AddToHook{begindocument/begin}{\let\thanks\old@thanks} % present hyperref with the usual thanks
\let\old@maketitle\maketitle
\def\maketitle{\old@maketitle\def\thefootnote{\@arabic\c@footnote}}
\makeatother

\usepackage{hyperref}

\title{Title \ref{t}}
\thanks[t]{Title thanks}
\author{Author\ref{a}}
\thanks[a]{Author thanks}

\begin{document}

\maketitle

\footnote{Footnote}

\end{document}

相关内容