以下代码由 Martin Scharrer 在 5 月 10 日的一篇帖子回复中提供。
\documentclass{book}
\usepackage[british]{babel}
\usepackage[titles]{tocloft}
\usepackage{tocloft}
\newlength\mylenprt
\newlength\mylenchp
\newlength\mylenapp
\renewcommand\cftpartpresnum{\partname~}
\renewcommand\cftchappresnum{\chaptername~}
\renewcommand\cftchapaftersnum{.}
\settowidth\mylenprt{\cftpartfont\cftpartpresnum\cftpartaftersnum}
\settowidth\mylenchp{\cftchapfont\cftchappresnum\cftchapaftersnum}
\settowidth\mylenapp{\cftchapfont\appendixname~\cftchapaftersnum}
\addtolength\mylenprt{\cftpartnumwidth}
\addtolength\mylenchp{\cftchapnumwidth}
\addtolength\mylenapp{\cftchapnumwidth}
\setlength\cftpartnumwidth{\mylenprt}
\setlength\cftchapnumwidth{\mylenchp}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\begin{document}
\tableofcontents
\mainmatter
\part{First Part}
\chapter{First Chapter}
\appendix
\addtocontents{toc}{% NB!!! must be inside the first \include
\protect\renewcommand\protect\cftchappresnum{\appendixname~}%
\protect\setlength{\cftchapnumwidth}{\mylenapp}}%
\chapter{First Appendix}
\end{document}
为了试验超链接,我在上面添加了以下内容。
\usepackage{hyperref}
\hypersetup{colorlinks=true}
运行 LaTeX 后,会创建一个目录,其Part I. First Part
标题为上文的部分。但是,单词Part
与其他单词不同,它没有超链接。我想知道如何扩展超链接以包含单词Part
。
答案1
在这种情况下,我不会使用tocloft
来更改部分条目(这些条目和tocloft
不能很好地协作),而是宁愿重新定义命令\@part
(如在 中实现的book.cls
):
\documentclass{book}
\usepackage[british]{babel}
\usepackage[titles]{tocloft}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\makeatletter
\def\@part[#1]#2{%
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\partname~\thepart\hspace{1em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\bfseries \partname\nobreakspace\thepart
\par
\vskip 20\p@
\fi
\Huge \bfseries #2\par}%
\@endpart}
\makeatother
\newlength\mylenchp
\newlength\mylenapp
\renewcommand\cftchappresnum{\chaptername~}
\renewcommand\cftchapaftersnum{.}
\settowidth\mylenchp{\cftchapfont\cftchappresnum\cftchapaftersnum}
\settowidth\mylenapp{\cftchapfont\appendixname~\cftchapaftersnum}
\addtolength\mylenchp{\cftchapnumwidth}
\addtolength\mylenapp{\cftchapnumwidth}
\setlength\cftchapnumwidth{\mylenchp}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\part{First Part}
\chapter{First Chapter}
\appendix
\addtocontents{toc}{% NB!!! must be inside the first \include
\protect\renewcommand\protect\cftchappresnum{\appendixname~}%
\protect\setlength{\cftchapnumwidth}{\mylenapp}}%
\chapter{First Appendix}
\end{document}