‘hyperref’ 和​​ TOC 的问题

‘hyperref’ 和​​ TOC 的问题

我在目录中添加了“chapter”一词,但是当我使用package时hyperref,该词从目录中删除。这是我的代码:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{hyperref} % if commented all fine

\begin{document}

\makeatletter
\patchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{\addcontentsline{toc}{chapter}{\protect{\numberline{\chaptername :  #1}}}}{}{}
\makeatother

\tableofcontents
\chapter{my name}
my name is ...
\section{structure}
\chapter{your name}
 your name is .....
\end{document}

答案1

解决方案是\@chapter在加载之前修补命令hyperref,但你需要一些额外的空间,\l@chapter你可以这样做

\documentclass{book}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{\addcontentsline{toc}{chapter}{\protect\numberline{\chaptername}#1}}{}{}
\patchcmd{\l@chapter}{\setlength\@tempdima{1.5em}}{\setlength\@tempdima{4.5em}}{}{}
\makeatother
\usepackage{hyperref}

\begin{document}
\makeatletter
\let\stdl@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{\stdl@chapter{{#1}}{}}
\makeatother

\tableofcontents
\chapter{my name}
my name is ...
\section{structure}
\chapter{your name}
 your name is .....
\end{document}

相关内容