5

I have a thesis (book-class document) with several chapters and appendices. I am using titletoc and titlesec to format the chapter headings and table of contents. When I create an appendix the title formatting of the appendix has the word "Appendix" as expected, but the same item in the table of contents has the word "Chapter" instead. I cannot figure out why my code is returning the word "Chapter" and not "Appendix" in the table of contents. Does anyone know how to get the word "Appendix" instead of "Chapter" in the table of contents?

\documentclass[letter,12pt]{book}
\usepackage{titlesec}
\usepackage{titletoc}

% Chapter title formatting
\titleformat{\chapter} % command
    [display] % shape
    {\filcenter} % format
    {\MakeUppercase{\chaptertitlename} \thechapter \vspace{1em}} % label
    {0pt} % separation
    {\MakeUppercase} % before code
    
% Chapter table of contents formatting
\titlecontents{chapter} % section name to be formatted
    [1in] % distance from left margin
    {} % code for formatting before the entry
    {\contentslabel[\MakeUppercase{\chaptertitlename}~\thecontentslabel]{7em}\uppercase} % format when the chapter has a number
    {} % format when the chapter has no number
    {\titlerule*[1ex]{.}\contentspage} % format for the filler (leader line) and page number

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{An Example Chapter}
To be or not to be, that is the question...

\chapter{Another Example Chapter}
Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune...

\appendix
\chapter{The First Appendix}
Note that the chaptername command returns \chaptername~while the appendixname command returns \appendixname~and the chaptertitlename command returns \chaptertitlename~but this behavior does not seem to be preserved in the table of contents.


\end{document}

2 Answers 2

3

\appendix redefines \chaptertitlename from \chaptername to \appendixname. But it does not put any information in the toc file where the appendix starts:

\contentsline {chapter}{\numberline {1}An Example Chapter}{1}{}%
\contentsline {chapter}{\numberline {2}Another Example Chapter}{3}{}%
\contentsline {chapter}{\numberline {A}The First Appendix}{5}{}%
\contentsfinish 

You could redefine \appendix to add a macro to the toc file that redefines \chaptertitlename locally. One possibility is

\newcommand\originalappendix{}
\let\originalappendix\appendix
\renewcommand\appendix{\originalappendix\addtocontents{toc}{\protect\appendixintoc}}
\newcommand\appendixintoc{\def\chaptertitlename{\appendixname}}

Example:

\documentclass[letter,12pt]{book}
\usepackage{titlesec}
\usepackage{titletoc}

% Chapter title formatting
\titleformat{\chapter} % command
    [display] % shape
    {\filcenter} % format
    {\MakeUppercase{\chaptertitlename} \thechapter \vspace{1em}} % label
    {0pt} % separation
    {\MakeUppercase} % before code
    
% Chapter table of contents formatting
\titlecontents{chapter} % section name to be formatted
    [1in] % distance from left margin
    {} % code for formatting before the entry
    {\contentslabel[\MakeUppercase{\chaptertitlename}~\thecontentslabel]{7em}\uppercase} % format when the chapter has a number
    {} % format when the chapter has no number
    {\titlerule*[1ex]{.}\contentspage} % format for the filler (leader line) and page number

\newcommand\originalappendix{}
\let\originalappendix\appendix
\renewcommand\appendix{\originalappendix\addtocontents{toc}{\protect\appendixintoc}}
\newcommand\appendixintoc{\def\chaptertitlename{\appendixname}}

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{An Example Chapter}
To be or not to be, that is the question...
\chapter{Another Example Chapter}
Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune...

\appendix
\chapter{The First Appendix}
Note that the chaptername command returns \chaptername~while the appendixname command returns \appendixname~and the chaptertitlename command returns \chaptertitlename~but this behavior does not seem to be preserved in the table of contents.
\end{document}

enter image description here

1
  • Thank you, @esdd, that appears to work in my larger thesis file as well as the MWE.
    – Gauen
    Mar 10, 2021 at 16:44
2

A variant of esdd's answer. Since \appendix does \gdef\@chapapp{\appendixname}, we need to use global definitions as well.

So I append to the code of \appendix the instruction to write in the .toc file the change in definition:

\addtocontents{toc}{\gdef\protect\@chapapp{\protect\appendixname}}

and the instruction to undo it in the \contentsfinish hook provided by titletoc.

\documentclass[12pt]{book}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{etoolbox}

\makeatletter
\appto\appendix{\addtocontents{toc}{\gdef\protect\@chapapp{\protect\appendixname}}}
\appto\contentsfinish{\gdef\@chapapp{\chaptername}}
\makeatother

% Chapter title formatting
\titleformat{\chapter} % command
    [display] % shape
    {\filcenter} % format
    {\MakeUppercase{\chaptertitlename} \thechapter \vspace{1em}} % label
    {0pt} % separation
    {\MakeUppercase} % before code
    
% Chapter table of contents formatting
\titlecontents{chapter} % section name to be formatted
    [1in] % distance from left margin
    {} % code for formatting before the entry
    {\contentslabel[\MakeUppercase{\chaptertitlename}~\thecontentslabel]{7em}\MakeUppercase} % format when the chapter has a number
    {} % format when the chapter has no number
    {\titlerule*[1ex]{.}\contentspage} % format for the filler (leader line) and page number

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{An Example Chapter}
To be or not to be, that is the question...

\chapter{Another Example Chapter}
Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune...

\appendix
\chapter{The First Appendix}
Note that the chaptername command returns \chaptername~while the appendixname command 
returns \appendixname~and the chaptertitlename command returns \chaptertitlename~but 
this behavior does not seem to be preserved in the table of contents.

\end{document}

I changed the \uppercase command into \MakeUppercase and also removed the letter option that doesn't exist. It should be, if you so want, letterpaper (but it can be omitted because it's the default).

1
  • Thank you, @egreg. This appears to work in my overall thesis file as well as the MWE. I lack the LaTeX expertise to make a judgement as to the efficiency of this solution versus esdd's solution, as they seem to work equivalently. Thank you for your help. Side note: I think 'letter' is an option in Kile which I am using as a front end. Apologies for the confusion.
    – Gauen
    Mar 10, 2021 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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