
hyperref
我將該套件與自訂基類結合 使用,但在理解錯誤訊息:Undefined control sequence. <recently read> \Hy@colorlink
和 時遇到問題Undefined control sequence. \close@pdflink ->\Hy@endcolorlink
。它們僅在文件中聲明包時出現。當在基類中聲明時,它按預期工作。
考慮以下最小設定:
\ProvidesClass{base}
\LoadClass{report}
% declare option 1: works
% \RequirePackage{hyperref}
\AtBeginDocument{
\tableofcontents
}
\documentclass{base}
% declare option 2: does not work
% \usepackage{hyperref}
\begin{document}
\section{One}
hello
\section{Two}
goodbye
\end{document}
背景
我必須為大學編寫一系列具有相同結構的報告,並且希望將所有非內容頁面外包到文件類別中。
\AtBeginDocument
處理諸如標題頁、摘要、目錄等內容。
答案1
您的問題不需要特殊的類,只需在您的\AtBeginDocument
呼叫後面載入 hyperref 即可:
\documentclass{report}
\AtBeginDocument{\tableofcontents}
\usepackage{hyperref}
\begin{document}
\section{One}
hello
\section{Two}
goodbye
\end{document}
\AtBeginDocument
不應該用於開始排版。許多套件都在其中添加了初始化程式碼。
使用新的 Latex 2020/10/01,目前可以使用 -dev 版本(例如 pdflatex-dev)進行測試,您可以這樣做:
\documentclass{report}
\AddToHook{begindocument/end}{\tableofcontents}
\usepackage{hyperref}
\begin{document}
\section{One}
hello
\section{Two}
goodbye
\end{document}
對於較舊的乳膠,您可以使用 etoolbox:
\documentclass{report}
\usepackage{etoolbox}
\AfterEndPreamble{\tableofcontents}
\usepackage{hyperref}
\begin{document}
\section{One}
hello
\section{Two}
goodbye
\end{document}