[Tex/LaTex] \part and tocloft

hyperrefpartstable of contentstocloft

The following code was provided by Martin Scharrer in reply to a post in May 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}

To experiment with hyperlinks, I have added the following to the above.

\usepackage{hyperref} 

\hypersetup{colorlinks=true}

After running LaTeX, a table of contents is created with Part I. First Part as the title of the part above. However, the word Part, unlike the rest, does not have hyperlink. I wonder how the hyperlink can be extended to include the word Part.

Best Answer

In this case, instead of using tocloft to change the part entries (these entries and tocloft don't cooperate well), I'd rather redefine the \@part command (as implemented in 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}

enter image description here