# How do I force \section's to start on a new page if they don't fit on current page?

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 \sections to be on a new page if all of it doesn't fit in the current page?

I am using memoir document class, btw.

-
Did you try using \pagebreak? –  Thorsten Apr 30 '11 at 8:30
@Thorsten, but that would force a page break in cases where the section would fit on the current page, wouldn't it? I am ok with 2 sections fitting on the same page. I am only not ok with a situation when a section would overflow at the title or code example level. –  Peteris Krumins Apr 30 '11 at 8:34
TeX has the \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. –  Christian Lindig Apr 30 '11 at 8:48
@Christian: Indeed \section tries to avoid creating orphans, but apparently inserting lstlisting without any preceding text breaks this. –  Andrey Vihrov Apr 30 '11 at 10:21
The empty line between \section and lstlisting is a paragraph break. IIRC, this breaks the \section orphan behaviour. –  You Aug 8 '11 at 18:30

My suggestion is to focus on getting the content and structure of your document in place before attending to details such as this. The reason is that the layout of your document will shift constantly while content is still being added. Because of this, the most efficient use of time is to focus on writing and ignore these details until the rough draft is done.

Once the rough draft is ready, then you can clean these bad section breaks up by inserting \goodbreak, \newpage, etc. as part of the editing process.

-
I honestly don't agree. I get motivated by seeing my text in near final form during the writing process. If it looks ugly I tend to get less motivated. –  jonalv Apr 7 at 8:23

Starting a lstlisting environment immediately after a section title triggers a bug of listings. It can be solved by

\usepackage{etoolbox}
\makeatletter
\preto\lstlisting{\@nobreakfalse}
\makeatother


In this case, in order to avoid adding undesired vertical space, call lstlisting as

\begin{lstlisting}[aboveskip=0pt]


Probably this won't solve your problem completely, as a page break may happen after the lstlisting and I don't know how to avoid it. :(

-

Have a look at the \Needspace command. Put it before the \section command and the section will not start unless there is enough space left on the page.

for example:

\Needspace{5\baselineskip}
\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}

....


To start the section on the next page unless there are att least 5 '\baselineskip' of space available. Be sure to include \usepackage{needspace} if you are not using memoir before the \begin{document} line.

-

\raggedbottomsectiontrue often helps (but not always for some reason)

-
Hi, I just got to solving this problem again. How do I exactly use \raggedbottomsectiontrue and what does it do? I can't find docs on it anywhere... –  Peteris Krumins Jun 3 '11 at 19:32