Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

4 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
2  
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.