4

I am trying to compile a document with

  1. 'Sub-tocs' on the first page of each part and each chapter. Gonzalo Medina's solution to a previous question can be used to achieve this.

  2. The appendices typeset as sections on the main toc of a chapter 'Appendices'. Alan Munn has previously solved this.

However, when these two solutions are combined effects occur that I cannot explain, leave alone correct for.


The{tocdepths} for the \parts and \chapters should be {1} and {2}, respectively. This also holds for the appendix. Medina suggests:

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

\usepackage{titlesec,titletoc}
\usepackage[titles]{tocloft}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\begin{document}
\setcounter{tocdepth}{2}
\tableofcontents
\clearpage

\part*{A Part}
\startcontents[parts]
\printcontents[parts]{}{1}{\setcounter{tocdepth}{1}} 
\clearpage

\chapter{A Chapter}
\startcontents[chapters]
\printcontents[chapters]{}{}{\setcounter{tocdepth}{2}} 
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}
\stopcontents[chapters]

\chapter{B Chapter}
\section{A Section}
\subsection{A Subsection}
\stopcontents[parts]

%%% The Appendix %%%
\clearpage\phantomsection
\part*{Appendices}

%\addcontentsline{toc}{chapter}{Appendices}
%\addtocontents{toc}{\setcounter{tocdepth}{0}}
%\ChapAsSecOnToc
%\ChapAsChapOnPartoc

\appendix
\startcontents[parts]
\printcontents[parts]{}{}{\setcounter{tocdepth}{1}}

\chapter{An Appendix}
\startcontents[chapters]
\printcontents[chapters]{}{}{\setcounter{tocdepth}{2}}
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}
\stopcontents[chapters]

\chapter{B Appendix}
\section{A Section}
\subsection{A Subsection}
\stopcontents[parts]

\end{document}

enter image description here

This does the job as desired and leaves the appendix' representation on the toc to be changed.

Adopting Munn's solution to alter the appended chapter's setting on the toc can be done by:
(1) commenting in the four commands below %%% The Appendix %%% in the MWE above and by
(2) defining the following before the beginning of the document:

% store current settings
\newlength{\truechapindent}
\setlength{\truechapindent}{\cftchapindent}
\newlength{\truebeforechapskip}
\setlength{\truebeforechapskip}{\cftbeforechapskip}
\let\truechapfont\protect\cftchapfont
\newcounter{truechapdotsep}
\setcounter{truechapdotsep}{\cftchapdotsep}

% chapter to look like section on toc
\newcommand{\ChapAsSecOnToc}{
    \addtocontents{toc}{
    \setlength{\cftbeforechapskip}{\cftbeforesecskip}
    \setlength{\cftchapindent}{\cftsecindent}
    \protect\renewcommand{\cftchapfont}{\cftsecfont}
    \protect\renewcommand{\protect\cftchapdotsep}{\cftsecdotsep}
}}

% chapter still to look like chapter on partoc
\newcommand{\ChapAsChapOnPartoc}{
    \addtocontents{ptc}{
    \setlength{\cftbeforechapskip}{\truebeforechapskip}
    \setlength{\cftchapindent}{\truechapindent}
    \protect\renewcommand{\cftchapfont}{\truechapfont}
    \protect\renewcommand{\protect\cftchapdotsep}{\value{truechapdotsep}}
}}

enter image description here

Now I run into two problems:

  1. Resetting the {tocdepth} with \printcontents[XX]{}{}{\setcounter{tocdepth}{YY}} doesn't have an effect any longer, ie the {tocdepth} remains {0}
  2. The chapters are not set in bold in the Appendices' part-toc

How do I address problems (1) and (2)?

I tried to mitigate the first by replacing \printcontent's fourth argument with {\addtocontents{ptc}{\setcounter{tocdepth}{YY}}}. But now the last depth written to the .ptc file overwrites the first. And the chapters are still not set in bold in the Appendices part-toc.

However, it makes 4 of 5 pages look the way I want them to:

enter image description here

3
+100

Here is one way to do this with etoc:

edit: I initially misread the OP's query. See updated answer at bottom

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

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoc}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\usepackage{hyperref}

\begin{document}

\etocsettocdepth{subsection}
% trick to avoid an "Appendices" entry in TOC.
\etocsetlevel{vanishingpart}{6}% i.e. ignore
\makeatletter
\newcommand*\toclevel@vanishingpart{-1}% make hyperref happy
\makeatother


\tableofcontents

% from now on, TOCs do not open a new page,
% not do they print a prominent "Contents"
\etocsettocstyle{}{}
% for local TOCs, the "vanishingpart" must
% act as real "part" (really important only
% for the main \localtableofcontents of Appendix document part
\etocsetlevel{vanishingpart}{-1}% i.e. part
% ignore from now on the on-the-fly limitation of the tocdepth
% to chapter level applying in the main TOC
% (see \etocsettocdepth.toc{chapter} below)
\etocignoretoctocdepth

\clearpage

\phantomsection
\part*{A Part}
\addcontentsline{toc}{part}{A Part}

\etocsetnexttocdepth{section}
\localtableofcontents

\clearpage

\chapter{A Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}

%%% The Appendix %%%
\clearpage

\phantomsection
\part*{Appendices}
\addcontentsline{toc}{vanishingpart}{Appendices}
\etocsettocdepth.toc{chapter}

\etocsetnexttocdepth{section}
\localtableofcontents

\appendix

\chapter{An Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}

\end{document}

The above has some complication only to avoid the main TOC having an Appendices entry because that was requested in OP's images. This looks strange because there is then no way to click in main TOC to get to Appendices, which as a local TOC, one must at best click on Appendix A, missing that local TOC of all appendices in document. But one can use the PDF bookmarks as a replacement.

Here is the simpler way which gives exactly same result as above except for the main TOC which would look like this:

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

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoc}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\usepackage{hyperref}

\begin{document}

\etocsettocdepth{subsection}

\tableofcontents

% from now on, TOCs do not open a new page,
% not do they print a prominent "Contents"
\etocsettocstyle{}{}
% ignore from now on the on-the-fly limitation of the tocdepth
% to chapter level applying in the main TOC
% (see \etocsettocdepth.toc{chapter} below)
\etocignoretoctocdepth

\clearpage

\phantomsection
\part*{A Part}
\addcontentsline{toc}{part}{A Part}

\etocsetnexttocdepth{section}
\localtableofcontents

\clearpage

\chapter{A Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}

%%% The Appendix %%%
\clearpage

\phantomsection
\part*{Appendices}
\addcontentsline{toc}{part}{Appendices}
\etocsettocdepth.toc{chapter}

\etocsetnexttocdepth{section}
\localtableofcontents

\appendix

\chapter{An Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}

\end{document}

Although the above had its merits, it wasn't what was asked.

Here we go:

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

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoc}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\usepackage{hyperref}

\makeatletter
\newcommand*\MimickAppendicesAsSections {%
   % This is executed as part of the main \tableofcontents
   % inside a scope limiting group
   \etocsetlevel{part}{0}%
   \etocsetlevel{chapter}{1}%
   \etocsetlevel{section}{2}%
   % etoc is in "compatibility mode", so the actual
   % \l@part will get executed, similarly for chapter
   \let\l@part\l@chapter
   \let\l@chapter\l@section
   % Only used once, for main TOC
   \global\let\MimickAppendicesAsSections\empty
}%
\makeatother

\begin{document}

\etocsettocdepth{subsection}

\etocsetlevel{part}{6}% a priori, ignore "Parts" in the main TOC, as requested

\tableofcontents

% reset to normal level (needed for limiting local TOCs)
\etocsetlevel{part}{-1}
% from now on, TOCs do not open a new page,
% not do they print a prominent "Contents"
\etocsettocstyle{}{}
% ignore from now on the on-the-fly limitation of the tocdepth
% to appendix chapter sin the main TOC (which are rendred as section)
% (see \etocsettocdepth.toc{chapter} below)
\etocignoretoctocdepth

\clearpage

\phantomsection % for PDF bookmarks
\part*{A Part}
\addcontentsline{toc}{part}{A part}

\etocsetnexttocdepth{section}
\localtableofcontents

\clearpage

\chapter{A Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}

%%% The Appendix %%%
\clearpage

\phantomsection
\part*{Appendices}
\addtocontents{toc}{\protect\MimickAppendicesAsSections}
\etocsettocdepth.toc{chapter}
\addcontentsline{toc}{part}{Appendices}

\etocsetnexttocdepth{section}
\localtableofcontents

\appendix

\chapter{An Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}

\end{document}

Posting here only the main TOC:

enter image description here

I think all other TOCs are also as requested. Please ping me if I again misread something.

  • This works nicely for the appendix-page and the following subtocs. But the main toc should show a 'chapter' called Appendices and a 'section' for each chapter in the appendix. What I mean is shown on the second and third image uploaded. Can your code be altered to achieve that? – D.Roepo Oct 9 '18 at 10:09
  • @D.Roepo fixed (I hope). I don't know how, but my eyes had seen your query but my brain decided it wanted to do something else. – user4686 Oct 9 '18 at 10:15
  • @jbfu Thank you very much, this works as desired! I went through your code and changed the order of things so to make understanding a bit easier for a noob like me. Mind if I, in the end, edit your post to have it as a direct answer to my question? And add an image like those in my post? Anyway, one last question: How would I add a bibliography as an 'Appendix C'? Right now, any bibliography added is unfortunately simply ignored... – D.Roepo Oct 11 '18 at 10:29
  • I prefer you add your own answer if you needed to make modifications to the code I proposed. There you can insert the image if you like as in your question. As per the bibliography, it seems this is a new question, unrelated to the current one, perhaps first search through site, but something like \addcontentsline{toc}{chapter}{Appendix C} should work if used after \begin{thebibliography} if you use that environment. – user4686 Oct 11 '18 at 14:50
  • (in my previous comment the \addcontentsline will create unnumbered, perhaps use ...{\numberline{C}Bibliography} rather, but anyway I can't answer that in comments because it depends on document class and on how bibliography is produced. – user4686 Oct 11 '18 at 14:52

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.