4

Using the report document class, how can I create a table of contents that looks like:

Chapter 1: Title

1.1  Title
1.2  Title

Appendices 
1.A
1.B

Chapter 2: Title

2.1  Title
2.2  Title

Appendices 
2.A
2.B

3 Answers 3

8

Below there's one possible solution; the code includes 1) A redefinition of \@chapter (as defined in report.cls) to include the word "Chapter" in the chapter entries of the ToC. 2) Two newly defined commands: \bappendix and \eappendix; the former starts a group, adds "Appendices" to the ToC and redefines \section (as defined in report.cls) to modify the section counter as requested; the latter simply ends the group:

\documentclass{report}

\newcounter{appendix}[chapter]

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\@chapapp~\thechapter: #1}%
                    \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}

\newcommand\bappendix{%
  \addtocontents{toc}{\protect\addvspace{10pt}Appendices}
  \begingroup
  \renewcommand\section{\stepcounter{appendix}%
    \renewcommand\thesection{\thechapter.\Alph{appendix}}
    \@startsection {section}{1}{\z@}%
      {-3.5ex \@plus -1ex \@minus -.2ex}%
      {2.3ex \@plus.2ex}%
      {\normalfont\Large\bfseries}}}
\makeatother
\newcommand\eappendix{\endgroup}

\begin{document}
\tableofcontents

\chapter{First Chapter}
\section{Section One One}
\section{Section One Two}

\bappendix
\section{Appendix One A}
\section{Appendix One B}
\eappendix

\chapter{Second Chapter}
\section{Section Two One}
\section{Section Two Two}

\bappendix
\section{Appendix Two A}
\section{Appendix Two B}
\eappendix

\end{document}

enter image description here

Here's the same approach, but using the etoolbox package to simplify the code:

\documentclass{report}
\usepackage{etoolbox}

\newcounter{appendix}[chapter]

\makeatletter
\patchcmd{\@chapter}{\protect\numberline{\thechapter}#1}
{\@chapapp~\thechapter: #1}{}{}
\newcommand\bappendix{%
  \addtocontents{toc}{\protect\addvspace{10pt}Appendices}
  \begingroup
  \pretocmd{\section}{\stepcounter{appendix}%
    \renewcommand\thesection{\thechapter.\Alph{appendix}}}{}{}}
\makeatother
\newcommand\eappendix{\endgroup}

\begin{document}
\tableofcontents

\chapter{First Chapter}
\section{Section One One}
\section{Section One Two}

\bappendix
\section{Appendix One A}
\section{Appendix One B}
\eappendix

\chapter{Second Chapter}
\section{Section Two One}
\section{Section Two Two}

\bappendix
\section{Appendix Two A}
\section{Appendix Two B}
\eappendix

\end{document}

Here's now a much more simple solution using the subappendices environment from the appendix package; the subappendices environment was slightly modified (to add some vertical space before "Appendices" in the ToC) using the etoolbox package; this package was also used to add "Chapter" before the chapter entries in the ToC:

\documentclass{report}
\usepackage{appendix}
\usepackage{etoolbox}

\AtBeginEnvironment{subappendices}{%
  \addtocontents{toc}{\protect\addvspace{10pt}Appendices}
}
\makeatletter
\patchcmd{\@chapter}{\protect\numberline{\thechapter}#1}
{\@chapapp~\thechapter: #1}{}{}
\makeatother


\begin{document}
\tableofcontents

\chapter{First Chapter}
\section{Section One One}
\section{Section One Two}

\begin{subappendices}
\section{Appendix One A}
\section{Appendix One B}
\end{subappendices}

\chapter{Second Chapter}
\section{Section Two One}
\section{Section Two Two}

\begin{subappendices}
\section{Appendix Two A}
\section{Appendix Two B}
\end{subappendices}

\end{document}
13
  • Awesome, thanks. Is it possible to obtain the same formatting with the tocloft package?
    – richtera
    Mar 6, 2012 at 17:37
  • @AlexRichter: yes; Mico's answer, for example, uses tocloft. Do you want me to produce a separate example using tocloft? Mar 6, 2012 at 17:57
  • I wonder if it is possible to combine the appendix package (using begin and end subapendices) and either the etoolbox or tocloft packages to simplify the code.
    – richtera
    Mar 6, 2012 at 18:03
  • @AlexRichter: yes, it's possible. See the third example in my updated answer. Mar 6, 2012 at 18:35
  • When I inserted the code to the last example, it didn't add the "Chapter " text in front of each chapter entry in the TOC. Also, it didn't indent the appendix tag.
    – richtera
    Mar 6, 2012 at 21:37
4

A solution to your question is provided in the following MWE. The MWE also lets you specify when there's only one appendix in a chapter (having the subtitle read "Appendices" when there's only one appendix may look funny).

\documentclass{report}
\usepackage{tocloft,ifthen}
    \renewcommand\cftchappresnum{Chapter } 
    \cftsetindents{chapter}{0em}{6em}
    \cftsetindents{section}{0em}{2.25em}
    \cftsetindents{subsection}{2.25em}{3em}

\makeatletter
%% For source of "@seccntformat" command, see book "The LaTeX Companion"
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\ \ }%  default
   {\csname #1@cntformat\endcsname}}%    enable individual control
%% Individual control: '\section@cntformat'
\newcommand{\section@cntformat}{\thechapter.\thesection\ \ }

% Macros to redefine numbering of appendix sections
\newcommand{\appname}{} % dummy definition 
\newcommand{\appsecnumbering}[1][0]{%
    \ifthenelse{#1=1}{\renewcommand\appname{Appendix}}
                     {\renewcommand\appname{Appendices}}
    \setcounter{section}{0}
    \renewcommand\thesection{\thechapter.\Alph{section}}
    \renewcommand{\section@cntformat}{\appendixname~\thesection\ \ }
    \addtocontents{toc}{\medskip\protect{\mdseries\appname\par}}}
\newcommand\normalsecnumbering{%
    \renewcommand{\thesection}{\thechapter.\arabic{section}}
    \renewcommand{\section@cntformat}{\thesection\ \ }}
\let\origchapter\chapter
\renewcommand\chapter{\normalsecnumbering % first, reset numbering style
    \origchapter} % second, execute the original \chapter command
\makeatother

\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{First Section Title} \label{sec:11}
\section{Second Section Title} \label{sec:12}
\appsecnumbering  
\section{Some additional stuff}       \label{sec:1a}
\section{Still more additional stuff} \label{sec:1b}
A couple of cross-references: 
  Section~\ref{sec:11} and Appendix~\ref{sec:1b}.

\chapter{Another Chapter}
\section{First Section Title}
\section{Second Section Title}
\appsecnumbering[1]  % specify "1" to request singular form of noun "Appendix"
\section{Some singular additional stuff}

\chapter{Yet Another Chapter}
\section{First Section Title}
\section{Second Section Title}
\appsecnumbering[7] % any number other than "1" generates plural form of noun
\section{Some additional stuff}
\section{Still more additional stuff}
\end{document}

enter image description here

Note the appearance of the first full page and, in particular, the cross-references to regular and appendix-area sections.

enter image description here

Finally, if you want the start of the section headers to line up vertically with the headers of the chapters, you should change the line

\cftsetindents{section}{0em}{2.25em}

to:

\cftsetindents{section}{0em}{6em}
6
  • I wonder if it would be simplier to use the appendix package to number the subappendices?
    – richtera
    Mar 6, 2012 at 17:43
  • With this solution, the optional argument of \chapter will produce undesired results. Mar 6, 2012 at 17:55
  • @GonzaloMedina -- thanks for pointing out this mistake; I've fixed it in the newly-saved version. (I've also generalized the approach to let the user indicate when there's only one appendix in a chapter.)
    – Mico
    Mar 6, 2012 at 18:16
  • @Mico Hello, could you also tell me how can I add the figures and tables in the appendix to the TOC?
    – WZhao
    Sep 11, 2019 at 9:06
  • @WZhao - Without knowledge of your document setup, I'm not in a position to offer actionable advice. Please post a new query, in which you state (a) the typesetting issue, or issues, you wish to solve and (b) enough detail about your document to give people the required pieces of information. For sure, do reveal which document class you employ and how you inform LaTeX that a part of the document is an appendix. Thanks.
    – Mico
    Sep 11, 2019 at 10:20
1

Something like this is not too difficult to do using either the memoir class, or by calling the appendix package manually (usepackage{appendix}) for other document classes.

Note that while \addcontentsline{toc} might work for any LaTeX document,

the subappendices environment (\begin{subappendices} ... \end{subappendices}) comes from either the memoir class or the appendix package.

\newenvironment{coolsubappendices}{
\addcontentsline{toc}{section}{Appendices}
\begin{subappendices}
}{
\end{subappendices}
}

Here's the specifics of what I actually did, with extra features that aren't directly relevant to answering your question.

\newenvironment{coolsubappendices}{
\clearpage
\chapter*{Chapter \thechapter. ~ Appendices}
\addcontentsline{toc}{section}{Appendices}
\namedsubappendices % from memoir class I think
\begin{subappendices}
}{
\end{subappendices}
}

E.g. the page break before the chapter's appendices, giving the appendices a within-chapter title labelled by the chapter, using namedsubappendices to automatically have the in-text section names be e.g. 'Appendix 1.A' instead of just '1.A' (it still only says '1.A' in the table of contents by default though).

In hindsight this is actually just a dumbed-down version of one of the accepted answer's suggestions, so perhaps I should not have posted this. Anyway here is a minimal example (copied and modified from that answer) using the report class

\documentclass{report}
\usepackage{appendix}

\newenvironment{coolsubappendices}{
\addcontentsline{toc}{section}{Appendices}
\begin{subappendices}
}{
\end{subappendices}
}

\begin{document}
\tableofcontents

\chapter{First Chapter}
\section{One One}
\section{One Two}

\begin{coolsubappendices}
\section{One A}
\section{One B}
\end{coolsubappendices}

\chapter{Second Chapter}
\section{Two One}
\section{Two Two}

\begin{coolsubappendices}
\section{Two A}
\section{Two B}
\end{coolsubappendices}

\end{document}

You must log in to answer this question.

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