基本クラスでハイパーリファレンスを使用する

基本クラスでハイパーリファレンスを使用する

パッケージをhyperrefカスタム基本クラスと組み合わせて使用​​していますが、エラー メッセージ: および が理解できません Undefined control sequence. <recently read> \Hy@colorlinkUndefined 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

問題には特別なクラスは必要ありません。呼び出しの背後で hyperref がロードされるだけです\AtBeginDocument

\documentclass{report}

\AtBeginDocument{\tableofcontents}
\usepackage{hyperref}


\begin{document}
  \section{One}
  hello
  \section{Two}
  goodbye
\end{document}

\AtBeginDocumentタイプセットを開始するのに使用すべきではありません。多くのパッケージがそこに初期化コードを追加します。

現在 -dev バージョン (例: pdflatex-dev) を使用してテストできる新しい latex 2020/10/01 では、次のように実行できます。

\documentclass{report}

\AddToHook{begindocument/end}{\tableofcontents}
\usepackage{hyperref}


\begin{document}
  \section{One}
  hello
  \section{Two}
  goodbye
\end{document}

古い LaTeX では etoolbox を使用できます:


\documentclass{report}

\usepackage{etoolbox}
\AfterEndPreamble{\tableofcontents}


\usepackage{hyperref}


\begin{document}
  \section{One}
  hello
  \section{Two}
  goodbye
\end{document}

関連情報