32

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.

4
  • Did you try using \pagebreak?
    – Thorsten
    Apr 30, 2011 at 8:30
  • 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. Apr 30, 2011 at 8:48
  • @Christian: Indeed \section tries to avoid creating orphans, but apparently inserting lstlisting without any preceding text breaks this. Apr 30, 2011 at 10:21
  • The empty line between \section and lstlisting is a paragraph break. IIRC, this breaks the \section orphan behaviour.
    – You
    Aug 8, 2011 at 18:30

4 Answers 4

29

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.

2
  • 34
    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, 2015 at 8:23
  • 3
    It's worth debunking the classic myth that LaTeX is "more efficient": doi.org/10.1371/journal.pone.0115069. I'm still happy to settle for "more beautiful" Feb 28, 2020 at 4:29
17

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.

7

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. :(

5

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

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .