[Tex/LaTex] How to keep Heading together with text

page-breaking

I use a very custom style (not mine) and very frequently I get a heading
on the bottom of a page and the next paragraph on the top of the next page. Sometimes this paragraph may be a list or similar. The same is true for listings and the listing description.

  • Is there a general way how I can adjust the "badness" of these events?
  • Or can I adjust when a section heading would be on the last, say, 3cm of
    a page it should go to the next page?
  • Or can I say, if a *listing description" is on the top 3cm of a page
    pull 5 more listing lines from the last page onto this page?

The style I use does not use standard structure tags and environments. But lets call them section, subsection, listing and I will try to migrate them to my lingo.

Best Answer

You need to incorporate the use of the needspace package. It provides \needspace{<length>} to check whether vertical length <length> is available on the page. If not, it issues a \break to flush the page content. Otherwise it does nothing (\relax).

You could use it in the following way - a very simple example:

\documentclass{article}
\usepackage{needspace}% http://ctan.org/pkg/needspace
...
\begin{document}
...
\needspace{6em}% Require at least 6em on the page
\section{This is a heading of some sort}
Here is the first paragraph after the heading...
\end{document}

You could even incorporate this command in the sectional heading command of the style you are using. This way there's no need to specify \needspace before the problem (or all) sectional heading.

needspace also provides \Needspace{<length>}. However, I've always found it sufficient to use \needspace{<length>}.