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.

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.

share|improve this question
add comment

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

share|improve this answer
    
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 '11 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 '11 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 '11 at 6:55
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.