[Tex/LaTex] Table of Contents with “Chapter” and per-chapter “Appendix”

appendicestable of contents

How do I create a table of contents that looks like:

Chater 1 Name

   1.1 section
   1.2 section 
   Appendix A
   Appendix B

Chapter 2 Name

   2.1 section
   2.2 section 
   Appendix A
   Appendix B

Specifically, I am wondering how to create appendix entries for each chapter, instead of the whole document. I am also wondering how to add the terms "Appendix" and "Chapter" as shown above.

Best Answer

I believe the following code does most of what you're asking for. Be sure to use the macro \appsecnums in each chapter at the start of the appendix area.

The major (but hopefully not disqualifying) limitation of this code is that you can't have subsections (or subsubsections) inside appendix-type sections because the numbering would be incorrect. (Specifically, you'd have an unwanted "Appendix" string prefixed to the sub(sub)section's number.) When cross-referencing an appendix section labelled, say, as \label{sec:newapp}, be sure to write \ref{sec:newapp} rather than Appendix~\ref{sec:newapp}.

\documentclass{report}
\usepackage{tocloft} % the tocloft package lets you redefine the Table of Contents (ToC)
  \renewcommand\cftchappresnum{Chapter } % prefix "Chapter " to chapter number in ToC
  \cftsetindents{chapter}{0em}{8em}      % set amount of indenting
  \cftsetindents{section}{2em}{6em}
% Macros to redefine numbering of appendix sections (and to
%   reset numbering when not in per-chapter area appendix)
\newcommand\normalsecnums{%
    \renewcommand\thesection{\thechapter.\arabic{section}}}
\let\origchapter\chapter
\renewcommand{\chapter}[1]{\normalsecnums
    \origchapter{#1}}
\newcommand\appsecnums{%   % execute this command before entering appendix area
    \setcounter{section}{0}
    \renewcommand\thesection{\appendixname~\Alph{section}}}

\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{Section Title}
\section{Section Title}
\appsecnums  % switch to "appendix-style" numbering of sections
\section{Some additional stuff}
\section{Still more additional stuff}

\chapter{Another Chapter}
\section{Section Title}
\section{Section Title}
\appsecnums  % switch to "appendix-style" numbering of sections
\section{Some additional stuff}
\section{Still more additional stuff}
\end{document}

enter image description here

Addendum. In the MWE above, to change the numbering style of the appendices from A, B, ... to <chapnum>.A, <chapnum>.B, ..., you'd need to modify the instruction

\renewcommand\thesection{\appendixname~\Alph{section}}}

as follows:

\renewcommand\thesection{\appendixname~\thechapter.\Alph{section}}}

You'll probably also need to increase the indent amount, in the third argument of cftsetindents commands, by a bit, say by 0.75em:

\cftsetindents{chapter}{0em}{8.75em}  
\cftsetindents{section}{2em}{6.75em}