2

Instead of my Appendix titles appearing in my Table of Contents formatted like the rest of my Chapters (I'm using the report class) I would prefer for them to appear in the same format as my Sections (see second image below). However, I can't just use \section for each Appendix, as I would like them to be treated as Chapters for all purposes other than the ToC.

My MWE (no additional packages specified):

\documentclass[12pt,a4paper,final]{report}

\begin{document}

    \tableofcontents
    \pagebreak

% Chapters  
    \chapter{First Chapter}
    \chapter{Second Chapter}
    \chapter{Third Chapter}
    \chapter{Fourth Chapter}

%Appendices
    \appendix
    \pagebreak % So that \addcontentsline is given the right page number
    \addcontentsline{toc}{chapter}{Appendices}
    \chapter{First Appendix}
    \chapter{Second Appendix}
    \chapter{Third Appendix}

\end{document}

This gives me a Table of Contents that looks like this, where each Appendix (as well as the additional line `Appendices') are formatted as Chapters:

ToC with Appendices formatted as Chapters

However, I would prefer for them to appear in the ToC formatted like Sections. I can produce this manually by editing the ToC file, changing (for example) the line:

\contentsline {chapter}{\numberline {A}First Appendix}{6} 

to:

\contentsline {section}{\numberline {A}First Appendix}{6}

Editing each Appendix line the same way gives me:

ToC with Appendices formatted at Sections

...which is exactly how I want the finished thing to look.

Obviously manually editing the .toc file works, but isn't ideal (as it gets overwritten when I recompile) and, to be honest, the simplicity of the change makes me feel like there must be a way to do it cleanly and automatically. And I'd always prefer a 'proper' (elegant) solution. If anyone can help me to produce the same result 'properly', I would be very grateful.

N.B. I have found a few solutions to similar questions, but none of them quite cuts it:

  • The obvious solution is to just make the Appendices Sections instead of Chapters, but that changes how they appear in the body (and I would prefer each Appendix to be treated as a Chapter in all instances other than the ToC.

  • I have tried using \chapter*{First Appendix} followed by \addcontentsline{toc}{section}{First Appendix} but that removes the 'numbering' (or rather lettering) in the main body and in the ToC. I would prefer that it still say 'Appendix A' etc.

5
  • 1
    Welcome to TeX.SX! How should sections in the appendix chapters appear then if the chapters are already formatted as sections there?
    – user31729
    May 13, 2015 at 16:08
  • My original intention was to increment everything down one level. So Sections in the appendices would appear as Subsections.
    – owjburnham
    May 15, 2015 at 10:26
  • And subsections? And subsubsections? And...?
    – user31729
    May 15, 2015 at 10:28
  • In an ideal world, the solution would add one to the ToC level. Parts (-1) become Chapters (0), Subsections (2) become Subsubsections (3) etc. Consistently incrementing that numerical value would avoid needing a separate solution at each level. However, practically in my case I don't need to go below Sections, so have been able to get away with a two-level solution.
    – owjburnham
    May 15, 2015 at 12:42
  • In an ideal world you should make your questions clearer ;-)
    – user31729
    May 18, 2015 at 21:03

2 Answers 2

3

Here is a solution with \let\l@chapter\l@section in .toc

\documentclass[12pt,a4paper,final]{report}

\begin{document}

    \tableofcontents
    \pagebreak

% Chapters  
    \chapter{First Chapter}
    \chapter{Second Chapter}
    \chapter{Third Chapter}
    \chapter{Fourth Chapter}

%Appendices
    \appendix
    \pagebreak % So that \addcontentsline is given the right page number
    \addcontentsline{toc}{chapter}{Appendices}
    \addtocontents{toc}{\protect\makeatletter}
    \addtocontents{toc}{\string\let\string\l@chapter\string\l@section}
    \addtocontents{toc}{\protect\makeatother}
    \chapter{First Appendix}
    \chapter{Second Appendix}
    \chapter{Third Appendix}

\end{document}
4
  • Thanks touhami. I had something similar as 'pseudocode', but hadn't been able to translate that into good tex. Your code works perfectly.
    – owjburnham
    May 15, 2015 at 10:28
  • For future readers' refererence: it was very simple to incorporate further levels. In order to cause Sections within each Appendix to be formatted in the ToC as Subsections, I only had to add \addtocontents{toc}{\string\let\string\l@section\string\l@subsection} after the line \addtocontents{toc}{\string\let\string\l@chapter\string\l@section} (to my surprise, adding it before did not work)
    – owjburnham
    May 15, 2015 at 10:40
  • Is there a way to undo the change afterwards? Sep 2, 2018 at 22:54
  • 1
    @Kyle_the_hacker you can type \addtocontents{toc}{\begingroup} for starting a scope and \addtocontents{toc}{\endgroup} for ending it. Another way is by save and restore default values
    – touhami
    Sep 3, 2018 at 6:19
2

This uses the direct write to .aux which produces a \@writefile{toc} in turn. (It's similar to the \addtocontents way touhami made)

The problem is, using this section stuff, that the appendix chapters appear as sections of chapter 4, which is probably not really wanted.

I think, it would be better to use \numberline for the appendix chapters too.

\documentclass[12pt,a4paper,final]{report}


\makeatletter
\let\origl@chapter\l@chapter

\newcommand{\redefinelchapter}{%
  \@writefile{toc}{\let\l@chapter\l@section}
}

\newcommand{\switchtosectionline}{%
  \immediate\write\@auxout{%
    \string\redefinelchapter%
  }%
}


\makeatother


\begin{document}

    \tableofcontents
    \pagebreak

% Chapters  
    \chapter{First Chapter}
    \section{First section of First Chapter}
    \chapter{Second Chapter}
    \chapter{Third Chapter}
    \chapter{Fourth Chapter}

%Appendices
    \appendix
    \pagebreak % So that \addcontentsline is given the right page number
%    \addcontentsline{toc}{chapter}{Appendices}
    \switchtosectionline

    \chapter{First Appendix}
    \chapter{Second Appendix}
    \chapter{Third Appendix}

\end{document}

Edit Some improved version

\documentclass[12pt,a4paper,final]{report}

\usepackage{xpatch}

\makeatletter

\xpretocmd{\appendix}{%
  \clearpage%
  \addcontentsline{toc}{chapter}{\appendixname}% Add the appendix to the Toc
  % Replace the chapter stuff with section like toc - entries
  \xpatchcmd{\@chapter}{%
    \addcontentsline{toc}{chapter}%
  }{%
    \addcontentsline{toc}{section}%
  }{\typeout{Patching of @chapter successful}}{}
}{\typeout{Appendix prepended}}{}

\makeatother


\begin{document}
\tableofcontents
\pagebreak

% Chapters  
\chapter{First Chapter}
\section{First section of First Chapter}
\chapter{Second Chapter}
\chapter{Third Chapter}
\chapter{Fourth Chapter}

\appendix

\chapter{First Appendix}
\chapter{Second Appendix}
\chapter{Third Appendix}

\end{document}
2

You must log in to answer this question.

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