[Tex/LaTex] Changing “Chapter A” to “Appendix A” in TOC – Existing solutions don’t work

appendices

I'm writing my thesis using \documentclass{harvard-thesis}

My desired result for TOC would be:

Chapter 1. 
Chapter 2.
Appendix A.

I use this to include the word "Chapter" in front of chapter numbers in TOC:

\renewcommand{\cftchappresnum}{Chapter }

My appendix was shown in TOC as "Chapter A" instead of the desired "Appendix A":

>\appendix
>\renewcommand\chaptername{Appendix }
>\include{appendices/appendixA}

I have applied solutions from How to force Latex to change from "A Appendix's name" to "Appendix A", Changing “Chapter A” to “Appendix A” in TOC as well as this patch:

\makeatletter
\patchcmd{\@chapter}{\protect {Chapter }}{\ifappendix{Appendix }\else{Chapter 
}\fi}{}{}
\makeatother
\AtBeginEnvironment{appendices}{\appendixtrue}

None of them works.
My current result for TOC is:

Chapter 1. 
Chapter 2.
Chapter A.

If I comment out the renewcommand that includes "Chapter" then Appendix A will show correctly:

1. 
2.
Appendix A.

Can someone help?

Best Answer

Appendix chapters are considered chapters as well, specifically because that's what is written to the .toc file. To illustrate this, consider the following example:

enter image description here

\documentclass{book}

\usepackage{tocloft}

\renewcommand{\cftchappresnum}{Chapter }
\setlength{\cftchapnumwidth}{6em}% Just for this example

\begin{document}

\tableofcontents

\chapter{A chapter}

\appendix

\chapter{An appendix}

\end{document}

Here's what the .toc looks like:

\contentsline {chapter}{\numberline {1}A chapter}{3}
\contentsline {chapter}{\numberline {A}An appendix}{5}

Note that each \contentsline entry corresponds to an entry in the ToC and they're all considered chapter.

In order to fix your problem, you can still update \cftchappresnum in your preamble, but write an update to this macro as soon as you execute \appendix. Here's how you can update \appendix to do this automatically:

enter image description here

\documentclass{book}

\usepackage{tocloft}

\renewcommand{\cftchappresnum}{Chapter }
\setlength{\cftchapnumwidth}{7em}% Just for this example

\let\oldappendix\appendix
\renewcommand{\appendix}{%
  \oldappendix
  \addtocontents{toc} % Update \cftchappresnum within the .toc
    {\protect\renewcommand{\protect\cftchappresnum}{Appendix }}
}

\begin{document}

\tableofcontents

\chapter{A chapter}

\appendix

\chapter{An appendix}

\end{document}

Now your .toc resembles:

\contentsline {chapter}{\numberline {1}A chapter}{3}
\renewcommand {\cftchappresnum }{Appendix }
\contentsline {chapter}{\numberline {A}An appendix}{5}

Each \contentsline entry is still a chapter, but \cftchappresnum was updated at the appropriate time within the .toc to print an Appendix prefix for your appendix \chapters.

Related Question