4

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}
3
  • In my opinion the end code of subappendices should write \end{multicols} to the ToC, not in \pretocmd{\chapter}{...}{}{}
    – user31729
    Commented Feb 23, 2017 at 6:40
  • I'm not convinced automating this 'automatic' method is a good idea with the \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.
    – jon
    Commented Feb 23, 2017 at 7:04
  • @Jon: Yes, I assumed that there's a subappendix in each chapter. If this is not the case, my comment from above is not applicable
    – user31729
    Commented Feb 23, 2017 at 7:35

1 Answer 1

2

I added a \newif\ifmulticolsused and write a \ifmulticolsused - Test into the ToC such that ToC itself is responsible for the closure of the multicols environment.

\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}


\newif\ifmulticolsused

\pretocmd{\multicols}{\global\multicolsusedtrue}{}{}
\apptocmd{\endmulticols}{\global\multicolsusedfalse}{}{}

% From http://tex.stackexchange.com/questions/199139
\titleformat{\chapter}[display]
  {\Huge\bfseries}
  {\chaptertitlename~\thechapter}
  {1em}{}
  [\ifnum\value{chapter}>0 
    \addtocontents{toc}{\protect\begin{multicols}{2}\protect\multicolsusedtrue}
   \fi
   \stepcounter{countchapters}
  ]% After-Code

\titleformat{name=\chapter,numberless}[display]
  {\Huge\bfseries}
  {\chaptertitlename~\thechapter}
  {1em}{}[]

\pretocmd{\chapter}{%
  % Close the multicols if needed
  \addtocontents{toc}{\protect\ifmulticolsused\protect\end{multicols}\protect\fi}
}{}{}

\AtEndDocument{%
  % Close the multicols if needed
  \addtocontents{toc}{\protect\ifmulticolsused\protect\end{multicols}\protect\fi}
}

% Idea borrowed to the `appendix' package
\newcounter{subappends}
\newenvironment{subappendices}{\par
  \stepcounter{subappends}
  \setcounter{section}{0}
  \renewcommand{\thesection}{\Alph{section}}
}{%
  % Close the multicols if needed
  \addtocontents{toc}{\protect\ifmulticolsused\protect\end{multicols}\protect\fi}%
}

\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}
1
  • It works like a charm in every use case: with and without `subappendix' ending. I had no time to answer to your comments that you send a solution! Many thanks.
    – ejazz
    Commented Feb 23, 2017 at 9:06

You must log in to answer this question.

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