# Is it possible to define a multicols{2} preamble switch that works for \chapter?

With standard LaTeX, issuing \twocolumn in the preambe causes the document to be typeset in two columns -- except that \chapter will produce separate one-column chapter headings.

\documentclass{book}

\twocolumn% Note: this doesn't change page margins

\usepackage{lipsum}

\begin{document}

\chapter{foo}

\lipsum[1-2]

\end{document}


If one wants balanced columns at the end of last (chapter) pages, one may use the multicol package and its multicols environment. However, simply enclosing the whole document body in \begin{multicols}{2} ... \end{multicols} won't do because a) \chapter headings become part of the first column and b) the last pages of any but the last chapter won't feature balanced columns. Instead, one has to use separate multicols environments for every chapter and to issue \chapter within the optional argument of multicols.

\documentclass{book}

\usepackage{multicol}

\usepackage{lipsum}

\begin{document}

\begin{multicols}{2}

\chapter{First}

\lipsum[1-2]

\chapter{Second}

\lipsum[1-2]

\end{multicols}

\begin{multicols}{2}[\chapter{Third}]
\lipsum[1-2]
\end{multicols}

\end{document}


Is it possible to define a multicols{2} switch similar to \twocolumn that may be issued in the document preamble and that produces separate one-column \chapter headings?

-
Would it be acceptable to issue a \clearpage command at the end of each chapter (while using the multicols environment)? – Mico Dec 31 '11 at 12:37
A partial solution that does away with multiple multicols environments will gain an upvote (though not necessarily be accepted). – lockstep Dec 31 '11 at 12:40

A simple solution would look somewhat like this:

\documentclass{book}

\usepackage{multicol}

\makeatletter
\def\closeopenmulticols{%
% test if current env is "multicols" if so close it
\def\@tempa{multicols}%
\ifx\@tempa\@currenvir
\end{multicols}%
\fi
}
\makeatother

\newcommand\Mychapter[1]{%
\closeopenmulticols
% start new multicols with chapter
\begin{multicols}{2}[\chapter{#1}]}

% close last open multicols
\AtEndDocument{ \closeopenmulticols}

\usepackage{lipsum}

\begin{document}

\Mychapter{First}
\lipsum[1-2]

\Mychapter{Second}
\lipsum[1-2]
\end{document}


Obvious improvements would be to redefine \chapter to work like \Mychapter but with the optional arguments of the standard heading commands - exercise for the reader ;-) ... happy new year (soon).

## Update

Here is a slightly more elaborate solution that actually implements the switch that was asked for (or rather two switches \startchaptercols[<colno>] and \stopchaptercols) that change the behavior on the next \chapter command).

\documentclass{book}

\usepackage{multicol}

\makeatletter
\def\closeopenmulticols{%
% test if current env is "multicols" if so close it
\def\@tempa{multicols}%
\ifx\@tempa\@currenvir
\end{multicols}%
\fi }
\makeatother

\let\origchapter\chapter

%parse optional args if present and save them
\def\mychapter#1#{\gdef\buildmychapter{\origchapter#1}\mychapteri}
% parse mandatory arg and do the work
\def\mychapteri#1{\closeopenmulticols
% start new multicols with chapter
\begin{multicols}{\chaptercols}[\buildmychapter{#1}]}

\newcommand\startchaptercols[1][2]{\gdef\chaptercols{#1}\global\let\chapter\mychapter}
\def\stopchaptercols{\gdef\chapter{\closeopenmulticols\origchapter}}

% close last open multicols
\AtEndDocument{ \closeopenmulticols}

\usepackage{lipsum}

\begin{document}

\startchaptercols    % start next chapter with two columns
\tableofcontents
\startchaptercols[3]    % start next chapter with three columns

\chapter{First}
\section{foo} \lipsum[1-2]
\startchaptercols    % start next chapter with two columns (no immediate change)
\section{foo} \lipsum[1-2]

\chapter[Running second]{Second}
\lipsum[1-2] \section{foo} \lipsum[1-2]
\stopchaptercols    % stop at next chapter (if any)
\section{foo} \lipsum[1-2]
\chapter*{Third}
\lipsum[1-2]
\end{document}

-

Here is a short solution:

\documentclass{book}
\usepackage{lipsum}
\usepackage{multicol,etoolbox}
\newenvironment{Chapter}[2][1]{\chapter[#1]{#2}}{}
%\usepackage{setlistings} private code edited out
\makeatletter
\def\multicols@string{multicols}
\BeforeBeginEnvironment{Chapter}{%
\ifx\@currenvir\multicols@string
\xdef\resume@multicols{\noexpand\begin{multicols}{\number\col@number}}%
\end{multicols}%
\else
\global\let\resume@multicols\@empty
\fi}
\AfterEndEnvironment{Chapter}{\resume@multicols}
\makeatother
\begin{document}
\begin{multicols}{2}
\begin{Chapter}{Multivariate Algebra I}
\end{Chapter}
\lipsum[1-2]
\begin{Chapter}{Multivariate Algebra II}
\end{Chapter}
\lipsum[3-4]
\end{multicols}
\end{document}


If you notice the \Chapter command has been defined as an environment. This is preferable, as you can then be able to adjust the opening layout for a chapter better. Here for example I use something similar to add the picture layout.

-
Your example seems to work without \usepackage{setlistings} (which is not part of CTAN). Is this code line redundant? – lockstep Dec 31 '11 at 13:07
@lockstep Yes is not needed. I extracted this from some longer code of mine. I use the same strategy to have the listings full width. I will edit it out. All these between a couple of glasses of wine. Happy New Year and don't edit any tags today:) – Yiannis Lazarides Dec 31 '11 at 13:12
Happy New Year, too. I'll try to restrict myself. ;-) – lockstep Dec 31 '11 at 13:18