Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.