我正在使用大学提供的自定义类来撰写论文。最近,我包含了 hyperref 包,并立即收到大量与 \addtocontents 命令相关的错误。我使用了以下提供的解决方案此链接。这样就消除了所有错误,但现在当我编译时,latex 给出了错误“未定义的控制序列 \chapter。如果我删除 hyperref 包,它就可以正常工作。
我花了一整天的时间尝试让它工作。我将 \chapter 的定义粘贴到下面的 .cls 文件中。我所做的更改标有注释“%changes 由 Waqas 添加”
\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\addcontentsline{toc}{chapter}%
{\protect {CHAPTER }\numberline{\thechapter}\MakeUppercase{#1}}%
\else
\addcontentsline{toc}{chapter}{\MakeUppercase{#1}}%
\fi
\chaptermark{#1}%changes added by waqas (added {}{}
\addtocontents{lof}{\protect\addvspace{10\p@}{}{}}%
\addtocontents{lot}{\protect\addvspace{10\p@}{}{}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
\def\@makechapterhead#1{%
\vspace*{50\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\centering \Large \bfseries \MakeUppercase{\@chapapp}\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\interlinepenalty\@M
\Huge \bfseries \MakeUppercase{#1}\par\nobreak
\vskip 40\p@
}}
\def\@schapter#1{\if@twocolumn
\@topnewpage[\@makeschapterhead{#1}]%
\else
\@makeschapterhead{#1}%
\@afterheading
\fi}
\def\@makeschapterhead#1{%
\vspace*{50\p@}%
{\parindent \z@ \raggedright
\normalfont
\interlinepenalty\@M
\Huge \bfseries #1\par\nobreak
\vskip 40\p@
}}
任何帮助都将非常感激。
答案1
处理以下问题hyperref
:
只有
\protect{CHAPTER }
左花括号{
受到保护,因为\protect
只作用于下一个令牌(不是一组标记)。无论如何,丑陋的前缀CHAPTER
浪费了书签中宝贵的空间,因此我将其更改为\texorpdfstring{CHAPTER }{}
。\MakeUppercase
在书签中不起作用,请将\texorpdfstring\MakeUppercase{}
其禁用在书签中代替。\chapter
如果我根据类将代码片段扩展为完整文档,book
则没有看到有关的错误。
完整示例:
\documentclass{book}
\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\def\@chapter[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\addcontentsline{toc}{chapter}{%
\texorpdfstring {CHAPTER }{}%
\numberline{\thechapter}%
\texorpdfstring\MakeUppercase{}{#1}}%
\else
\addcontentsline{toc}{chapter}{\MakeUppercase{#1}}%
\fi
\chaptermark{#1}%changes added by waqas (added {}{}
\addtocontents{lof}{\protect\addvspace{10\p@}{}{}}%
\addtocontents{lot}{\protect\addvspace{10\p@}{}{}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi
}
\def\@makechapterhead#1{%
\vspace*{50\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\centering \Large \bfseries \MakeUppercase{\@chapapp}\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\interlinepenalty\@M
\Huge \bfseries \MakeUppercase{#1}\par\nobreak
\vskip 40\p@
}}
\def\@schapter#1{\if@twocolumn
\@topnewpage[\@makeschapterhead{#1}]%
\else
\@makeschapterhead{#1}%
\@afterheading
\fi}
\def\@makeschapterhead#1{%
\vspace*{50\p@}%
{\parindent \z@ \raggedright
\normalfont
\interlinepenalty\@M
\Huge \bfseries #1\par\nobreak
\vskip 40\p@
}}
\makeatother
\usepackage{hyperref}
\usepackage{bookmark}
\bookmarksetup{numbered}
\begin{document}
\tableofcontents
\chapter{Foobar}
\end{document}