20

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.

1 Answer 1

18

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>}.

7
  • That sounds perfect for headings. I am still thinking how I can apply this in my listing environment, mainly for the listing description.
    – towi
    Oct 20, 2011 at 6:41
  • This may depend on the definition of your listing environment. For example, if the description is separate from the actual listing which itself is contained in a minipage (say), then you won't be able to extract "a couple of lines". However, it listing is typeset as a list (say, enumerate), then you can issue your \needspace between the last couple of \items, to make sure "things stay together". If this is insufficient, provide more detail about the actual definition of your listing environment (or any other environments for that matter).
    – Werner
    Oct 20, 2011 at 6:46
  • @Werneri: I see. I think the specific listing env is very self-made (just a paragraph, not a mini-page nor a enum). I don't like to clutter my listing code with \needspaces. Maybe I will shift them around manually first ("manual float") and will see how far I get. If that gets to tedious, I will come back to you :-) But a quick glance in the class-file just showed me there is a \newcommand{\mylistingheading}{#2} -- well, that sounds promising. Maybe I can tweak that with a \needspace
    – towi
    Oct 20, 2011 at 6:55
  • 2
    @Werner: Your answer looks exactly what I need. How can I avoid the need to type \needspace{6em} every time?
    – Jeroen
    May 15, 2014 at 16:32
  • 1
    @Jeroen: Creating an update for each sectional unit is probably the easiest (without knowing your exact setup). Sectional units from \section downward (in book or report) and \subsection downward (in article) use \@startsection to guide the construction/setup of the layout. One could tie into \@startsection, but that would exclude \part. Lots of things to consider, and therefore my easy solution is to add the \needspace on a per-command basis.
    – Werner
    May 15, 2014 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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