So I am writing a programming book, and I use \section
for each topic. For example, I'd write,
\section{Another way to double-space a file}
\begin{lstlisting}
awk 'BEGIN { ORS="\n\n" }; 1'
\end{lstlisting}
BEGIN is a special kind of pattern which ...
\section{Triple-space a file}
....
Sometimes I get a situation where the title of the section is at the very bottom of the page, and the example is at the beginning of a new page, etc.
Does anyone know how can I force \section
s to be on a new page if all of it doesn't fit in the current page?
I am using memoir
document class, btw.
\pagebreak
?\penality
primitive to control where page breaks are encouraged. The\section
definition should use this to discourage page breaks right after it. You can try a\goodbreak
before\section
or\raggedbottom
in the preamble.\section
tries to avoid creating orphans, but apparently insertinglstlisting
without any preceding text breaks this.\section
andlstlisting
is a paragraph break. IIRC, this breaks the\section
orphan behaviour.