Is it possible to define a `multicols{2}` preamble switch that works for \chapter?
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.