4

I'm trying to create a 3 column book layout. So far the only way I've figured out to get 3 columns is to use the venerable multicol package, which works nicely. Using the answer from this question Automatically add \begin{multicols}{N} to chapters I'm attempting to use etoolbox to automatically insert the \begin \end multicols whenever I type \chapter; however, this breaks when styling chapters using titlesec.

\documentclass{book}
\usepackage{etoolbox}
\usepackage{multicol}
\usepackage{titlesec}          % Customize chapters and sections
\usepackage{kantlipsum}

% Automatically wrap \chapter with \begin and \end multicols
\newif\iffirst
\makeatletter
\appto{\mainmatter}
{\firsttrue
\preto{\chapter}{\iffirst\firstfalse\else\end{multicols*}\fi}
\preto{\enddocument}{\end{multicols*}}
\apptocmd{\@makechapterhead}{\begin{multicols*}{3}}{}{}
\apptocmd{\@makeschapterhead}{\begin{multicols*}{3}}{}{}
}
\makeatother

% Format chapter heading
\titleformat
    {\chapter} % command
    [display]  % shape
    {\LARGE} % format
    {} % label 
    {-1.75cm} % horizontal separation between label and title body
    {Chapter \thechapter. \enspace \vspace{0cm}} % before-code
    [] % after-code

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{A chapter}
\kant
\chapter{A chapter}
\kant
\chapter{A chapter}
\kant
\end{document}

Latex complains about a ! Argument of \begin has an extra }. What's the best way to make this work?

Alan Munn
  • 218,180
Zendel
  • 203

1 Answers1

5

I'd do it differently:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{multicol}
\usepackage{titlesec}          % Customize chapters and sections
\usepackage{xparse}

\usepackage{kantlipsum}

% Automatically wrap \chapter with \begin and \end multicols

\let\latexchapter\chapter
\newif\iffirst
\appto\mainmatter{\sloppy\global\firsttrue\let\chapter\zendelchapter}
\raggedbottom

\NewDocumentCommand{\zendelchapter}{sO{#3}m}{%
  \iffirst\global\firstfalse\else\end{multicols*}\fi
  \begin{multicols*}{3}[%
    \IfBooleanTF{#1}{\latexchapter*{#3}}{\latexchapter[#2]{#3}}%
  ]
}
\AtEndDocument{\end{multicols*}}

% Format chapter heading
\titleformat
    {\chapter} % command
    [display]  % shape
    {\LARGE} % format
    {} % label 
    {-1.75cm} % horizontal separation between label and title body
    {Chapter \thechapter.\quad} % before-code
    [] % after-code
\titlespacing{\chapter}{0pt}{50pt}{51.7pt}

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{A chapter}
\kant
\chapter{A chapter}
\kant
\chapter{A chapter}
\kant
\end{document}

The strange 51.7pt has been computed by trial and error, in order to ensure an integral number of lines in the chapter pages.

For older TeX distribution, an error

! Illegal parameter number in definition of \zendelchapter

can be solved by replacing the given definition of \zendelchapter with

\NewDocumentCommand{\zendelchapter}{som}{%
  \iffirst\global\firstfalse\else\end{multicols*}\fi
  \begin{multicols*}{3}[%
    \IfBooleanTF{#1}
     {%
      \latexchapter*{#3}%
     }
     {%
      \IfNoValueTF{#2}
       {%
        \latexchapter{#3}%
       }%
       {%
        \latexchapter[#2]{#3}%
       }%
     }%
  ]
}
egreg
  • 1,121,712
  • Both latex and lualatex report errors when trying to compile this:

    ~ > lualatex example.tex ... ! You can't use macro parameter character # in horizontal mode. <argument> \numberline {1}## ######3

    Ubuntu 16.04

    – Zendel Sep 10 '18 at 14:20
  • 1
    @Zendel No error for me. Unfortunately the version of TeX Live provided by Ubuntu lags behind the current one, so you have a very outdated TeX system. I added a workaround. – egreg Sep 10 '18 at 17:00
  • Thanks, this works in my version of latex. I'm slowly getting more confident with it -- it's just been a low priority. This is very helpful. – Zendel Sep 10 '18 at 19:00