# Multiple chapters per page, only if they “fit”

I am typesetting a book which has some very short chapters. If two consecutive chapters are short enough, I would like to fit them both on the same page. Otherwise, the next chapter should always begin on the next page.

As a bonus, it would be great if chapters longer than one page could allow a new chapter before a page break if and only if that subsequent chapter would fit entirely on that last page of the current chapter without another page break.

Is this possible?

• This looks like an application for the \filbreak command of plain TeX, provided that you prohibit page breaks in the first page or so of every chapter. – GuM May 19 '15 at 16:14
• @GustavoMezzetti this would have to be done manually for each chapter? – Lucina May 19 '15 at 16:30
• No, it can be made to happen automatically. Besides, there is actually no need to prohibit page breaks in the beginning of the chapter (I was wrong in saying this), because if two potential page breaks have the same cost, the page builder will choose the last one on the galley. I haven't time, now, to post a detailed answer, I'll just give you the general idea: prepend to each chapter something similar to \vfil\penalty-500\vfilneg. 500 units of "bonus" should be enough to overcome other bonuses that might occur in the first few lines of the new chapter. – GuM May 19 '15 at 16:44
• Why is this question tagged {pdftex}? It doesn’t address a pdftex-specific issue. – GuM May 19 '15 at 18:11

The following is just an example that gives the general idea. It contains a patch of the \chapter command that is suitable for the standard book class; for other document classes, the patch could (and in general will) be different. Of course, etoolbox's \patchcmd could be used instead.

\documentclass[a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

\renewcommand\chapter{%
%   \if@openright\cleardoublepage\else\clearpage\fi
\vfil \penalty-9000 \vfilneg
% Something from the following two lines could be retained:
%   \thispagestyle{plain}%
%   \global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter
}

\makeatother

% \tracingpages = 1

\begin{document}

\chapter{First}
\lipsum[1-16]

\chapter{Second}
A very short chapter.

\chapter{Third}
Another very short chapter.

\chapter{Fourth}
This is true minimalism.

\chapter{Fifth}
\lipsum[17-20]

\chapter{Sixth}
\lipsum[21]

\chapter{Seventh}
\lipsum[22]

\chapter{Eighth}
\lipsum[23-24]

\chapter{Ninth}
\lipsum[25-32]

\chapter{Tenth}
And so on\ldots

\end{document}


Edit

As another example, for the memoir class (as explicitly requested), the patch would be:

\documentclass[a4paper]{memoir}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

\renewcommand\chapter{%
% Do not remove support for "article" option:
\ifartopt\par\@nameuse{chapterblock}\else
%     \clearforchapter
\vfil \penalty-9000 \vfilneg
% Something from the following two lines could be retained:
%     \thispagestyle{chapter}
%     \global\@topnum\z@
\fi
\m@mindentafterchapter
\@ifstar{\@m@mschapter}{\@m@mchapter}%
}

\makeatother

% \tracingpages = 1

\begin{document}

\chapter{First}
\lipsum[1-16]

\chapter{Second}
A very short chapter.

\chapter{Third}
Another very short chapter.

\chapter{Fourth}
This is true minimalism.

\chapter{Fifth}
\lipsum[17-20]

\chapter{Sixth}
\lipsum[21]

\chapter{Seventh}
\lipsum[22]

\chapter{Eighth}
\lipsum[23-24]

\chapter{Ninth}
\lipsum[25-32]

\chapter{Tenth}
And so on\ldots

\end{document}


And so on…

• How would I adapt this to the memoir class? I don't know how I would even figure this out. – Lucina May 19 '15 at 18:57
• @Lucina: See edit. – GuM May 19 '15 at 19:16