기본 클래스와 함께 하이퍼참조 사용

기본 클래스와 함께 하이퍼참조 사용

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

문제에는 특수 클래스가 필요하지 않으며 해당 하이퍼참조만 호출 뒤에 로드됩니다 \AtBeginDocument.

\documentclass{report}

\AtBeginDocument{\tableofcontents}
\usepackage{hyperref}


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

\AtBeginDocument조판을 시작하는 데 사용해서는 안됩니다. 많은 패키지가 여기에 초기화 코드를 추가합니다.

현재 -dev 버전(예: pdflatex-dev)을 사용하여 테스트할 수 있는 새로운 라텍스 2020/10/01을 사용하면 다음과 같이 할 수 있습니다:

\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}

관련 정보