The aim is to have a main table of contents with two columns for the (sub)sections of a book. This can be achieved as given in the answer to the post : Patching \chapter command such that all lower sections are set in twocolumn in ToC.
In order to be able to add appendices inside a chapter, I have defined a subappendices
environment, borrowing the idea to the appendix
package.
At this stage, that works fine. To keep the traditional structure of a LaTeX document (ToC, mainmatter, appendices...) and taking into account some possible appendices as chapter, I have simply defined a new helper counter (see below the MWE).
Furthermore, the layout of pages for the subappendices has to be modified. With the geometry
and etoolbox
packages, I have wrapped the subappendices
environment with \BeforeBeginEnvironment{subappendices}{\clearpage\newpagelayout}
and \AfterEndEnvironment{subappendices}{\restoregeometry}
.
Here an issue occurs when the document is ending by a subappendices
environment. The \AtEndDocument
macro doesn't end the muticols
environment anymore in the ToC.
After some trial and error, I have remarked that adding some text at the end of the document can overpass the problem, but obviously adds also a new useless page (despite the fact that it could be empty using a \phantom macro).
I'm interested by any advice and explanation. What have I missed?
MWE:
\documentclass{book}
\usepackage{multicol,etoolbox}
\usepackage{titletoc,titlesec}
\usepackage{geometry}
%\usepackage{hyperref}
\geometry{margin=4cm,a4paper,showframe}
%-- Command for changing the page layout mid-document
\newcommand{\newpagelayout}{\newgeometry{margin=2cm}}
% With \appendix macro \value{chapter} is resetted to zero
\newcounter{countchapters}
% From https://tex.stackexchange.com/questions/199139
\titleformat{\chapter}[display]
{\Huge\bfseries}
{\chaptertitlename~\thechapter}
{1em}{}
[\ifnum\value{chapter}>0
\addtocontents{toc}{\protect\begin{multicols}{2}}
\fi
\stepcounter{countchapters}
]% After-Code
\titleformat{name=\chapter,numberless}[display]
{\Huge\bfseries}
{\chaptertitlename~\thechapter}
{1em}{}[]
\pretocmd{\chapter}{%
\ifnum\value{countchapters}>0
\addtocontents{toc}{\protect\end{multicols}}%
\fi}{}{}
\AtEndDocument{%
\ifnum\value{countchapters}>0
%\phantom{}% Works but adds a useless page
\addtocontents{toc}{\protect\end{multicols}}%
\fi}
% Idea borrowed to the `appendix' package
\newcounter{subappends}
\newenvironment{subappendices}{\par
\stepcounter{subappends}
\setcounter{section}{0}
\renewcommand{\thesection}{\Alph{section}}
}{}
\BeforeBeginEnvironment{subappendices}{\clearpage\newpagelayout}
\AfterEndEnvironment{subappendices}{\restoregeometry}
\begin{document}
\tableofcontents
\mainmatter
\chapter{FOO}
\section{bar1}
\section{bar2}
\section{bar3}
\section{bar4}
\begin{subappendices}
\section{Appendix A}
\section{Appendix B}
\end{subappendices}
\chapter{BAR}
\section{foo1}
\section{foo2}
\section{foo3}
\section{foo4}
\begin{subappendices}% It fails for the last subappendix
\section{Appendix A}
\section{Appendix B}
\end{subappendices}
\appendix
% Test: OK whithout and with `subappendices'
%\chapter{FOO appendix}
%\section{bar1}
%\section{bar2}
%\section{bar3}
%\section{bar4}
\end{document}
subappendices
should write\end{multicols}
to theToC
, not in\pretocmd{\chapter}{...}{}{}
\newgeometry
/\restoregeometry
. Why not simply define commands to do\addtocontents{toc}{\protect\begin{multicols}{2}}
(and then to\end{multicols}
) and mark off what you want typeset in multicolumns? However, if every chapter will conclude with a 'subappendix', then Christian's suggestion might be fine.subappendix
in each chapter. If this is not the case, my comment from above is not applicable