8

I'm using this code to get word Chapter in TOC before each chapter.

\documentclass[a4paper]{book}
\usepackage{tocloft,calc}

\renewcommand{\cftchappresnum}{Chapter }
\AtBeginDocument{\addtolength\cftchapnumwidth{\widthof{\bfseries Chapter }}}

\begin{document}
\tableofcontents

\include{Intro}


\cleardoublepage
\begin{appendices}
\renewcommand{\thesection}{\arabic{section}}
\include{Appendix}
\end{appendices}

\end{document}

This code also adds Chapter word before appendix. I wonder how to get word Appendix before the appendix in TOC.

2
  • Related: tex.stackexchange.com/questions/56839/…
    – lockstep
    Jun 10, 2012 at 20:45
  • 1
    Notice that you use files Appendix.tex and Intro.tex and you do not post them. You should have made your code simpler if you just made examples of a \chapter before and inside the appendices environment, without the need of extra files.
    – yo'
    Jun 10, 2012 at 20:45

3 Answers 3

5

You have to do the changes while reading the .toc file: this is an example of how to do it. The double \unexpanded is because the toc entry is first written in the .aux file and then in the .toc, so we have to protect it twice. One could add several \protect, but this is surely easier to write.

\documentclass[a4paper]{book}
\usepackage{tocloft,calc}

\newcommand{\setupname}[1]{%
  \addtocontents{toc}{%
    \unexpanded{\unexpanded{%
      \renewcommand{\cftchappresnum}{#1 }%
      \setlength\cftchapnumwidth{\widthof{\bfseries #1 }}%
      \addtolength\cftchapnumwidth{\fixedchapnumwidth}%
    }}%
  }%
}
\AtBeginDocument{\edef\fixedchapnumwidth{\the\cftchapnumwidth}}

\begin{document}
\frontmatter
\tableofcontents

\mainmatter

\setupname{Chapter}
\chapter{Title}

text

\chapter{Another}

text

\appendix
\setupname{Appendix}
\chapter{Title}

text

\end{document}

The \setupname command might be integrated in \mainmatter and in \appendix, but this is left as an exercise. :)

2
  • It must be added that \setupname must be added to the included file if \include is used, otherwise it will only be inserted after the included file. It is therefor doubtfull if you can make a foolproof integration with \mainmatter and \appendix because a shipout is needed before the \addtocontents is written to the AUX file.
    – Danie Els
    Jun 12, 2012 at 1:09
  • @DanieEls I never use \include, but you're right. ;-)
    – egreg
    Jun 12, 2012 at 7:51
5

Add this line after \begin{appendices}:

\addtocontents{toc}{\def\protect\cftchappresnum{\appendixname{} }}

It adds a line to the TOC file that changes the title of the chapters (you ought to run twice). You might as well want to change the label width, so put Appendix instead of Chapter in the \AtBeginDocument... line.

1

The answers using tocloft works fine if you are not using the \include command. The problem is the delayed write mechanism used inside the \addtocontents to put stuff in the toc usualy ends up after the include contents. Solutions that works under all conditions are:

(1) It is the default behaviour of the amsbook class

(2) With memoir it can be obtain by putting the following in your preamble:

\renewcommand*{\cftpartname}{PART~}
\renewcommand*{\cftchaptername}{\chaptername~}
\renewcommand*{\cftappendixname}{\appendixname~}
\renewcommand*{\cftchapteraftersnum}{.}% dot after the number
\setlength{\cftchapternumwidth}{2em}

(3) With the standard book class you need a lot of changes. The following is from c.t.t. and was the basis of the code that ended up in memoir

\documentclass{book}
\makeatletter
%% Add a conditional to \appendix. Could also use \@chapp.
\newif\if@inappendix\@inappendixfalse
\let\oldappendix=\appendix
\renewcommand*{\appendix}{\oldappendix\@inappendixtrue}

%% User defined number width in TOC (\tocchpnumwdth)
%% Add inserts for Chapter and Appendix. Define to
%% \relax if standard output is needed.
\newlength{\tocchpnumwdth}
\setlength{\tocchpnumwdth}{1.5em}
\newcommand*{\tocchapinsert}{\chaptername\space}
\newcommand*{\tocappinsert}{\appendixname\space}

%% Small change in \@chapter. Write write different contents
%% line for Chapter and Appendix. Add inerts to \numberline.
\def\@chapter[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
       \if@mainmatter
          \refstepcounter{chapter}%
          \typeout{\@chapapp\space\thechapter.}%
          \if@inappendix
             \addcontentsline{toc}{appendix}%<-- THIS IS NEW
               {\protect\numberline{\tocappinsert\thechapter}#1}%<-
          \else
             \addcontentsline{toc}{chapter}%
               {\protect\numberline{\tocchapinsert\thechapter}#1}%<-
          \fi
       \else
          \addcontentsline{toc}{chapter}{#1}%
       \fi
    \else
       \addcontentsline{toc}{chapter}{#1}%
    \fi
    \chaptermark{#1}%
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \if@twocolumn
       \@topnewpage[\@makechapterhead{#2}]%
    \else
       \@makechapterhead{#2}%
       \@afterheading
    \fi}

%% Creates a \l@appendix identical to \l@chapter. Add the
%% width of the inserts to the width of the \numberline
%% box (\@tempdima).
\renewcommand*\l@chapter{\l@@chapter{\tocchapinsert}}
\newcommand*\l@appendix{\l@@chapter{\tocappinsert}}%<-- NEW

\newcommand*\l@@chapter[3]{%
   \ifnum \c@tocdepth >\m@ne
     \addpenalty{-\@highpenalty}%
     \vskip 1.0em \@plus\p@
     \begingroup
       \parindent \z@ \rightskip \@pnumwidth
       \parfillskip -\@pnumwidth
       \leavevmode \bfseries
       \settowidth{\@tempdima}{#1}%           <- change
       \addtolength\@tempdima{\tocchpnumwdth}%<- change
       \advance\leftskip\@tempdima
       \hskip -\leftskip
       #2\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #3}\par
       \penalty\@highpenalty
     \endgroup
   \fi}

%% Keep hyperref happy
\def\toclevel@appendix{0}

\makeatother
\usepackage[breaklinks=true, % for long names
           ]{hyperref}
\begin{document}
\frontmatter
    \chapter{Preface}
    \tableofcontents
    \chapter{Nomenclature}
\mainmatter
    \chapter{First Chapter}
    \section{First Section}
\appendix
    \chapter{First Appendix}
    \section{First Appendix Section}
\backmatter
    \chapter{Data Tables}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .