63

Given LaTeX likeso:

\section{Heading}

Here is some text after the heading.

How could one prevent the Heading from showing up at the very bottom of the page?

In other words, how could one prevent a page-break after Heading and before the following text?

This seems to be happening in a document that I've got. If there's more information that would be helpful, I'd be happy to post more of the entire LaTeX document.

3
  • It's happenning to me, too. I have a small document, I'm not redefining anything. I don't even use \usepackage! I'm using \documentclass{article} and there are \sections in it. It might be because all that are in the sections are tables, so perhaps it's very difficult to layout the tables so that they're not split across pages (which latex won't do?) and keep the section title with the contents? Commented Nov 10, 2011 at 16:55
  • @LimitedAtonement: Maybe give the Memoir class a shot - it solved the problem for me: ctan.org/tex-archive/macros/latex/contrib/memoir Commented Nov 10, 2011 at 19:07
  • In my document, this was just caused by a badly positioned figure. Commented Oct 4, 2016 at 22:55

9 Answers 9

15

This should rarely happen with the standard document classes (and the better-known ones such as memoir and koma). Are you using a homegrown document class or redefining the \section command yourself?

3
  • Using \documentclass[oneside,12pt]{article}. Was thinking of moving to memoir- this might push me more. :) Thanks. Commented Feb 5, 2009 at 4:41
  • 1
    Problem went away with the Memoir class. Commented Sep 1, 2009 at 19:58
  • 5
    @Will Robertson: It still occurs e.g. in book class if you have a footnote below the start of the new section...
    – strpeter
    Commented Jan 28, 2014 at 22:46
24

You should use the option

\usepackage[nobottomtitles*]{titlesec}

This will prevent your heading titles to be displayed at the bottom of the page and it will place your heading in the next page.

2
  • 1
    \usepackage[nobottomtitles*]{titlesec} doesn't work for me. I had to remove the star. \usepackage[nobottomtitles]{titlesec} worked. Commented Jul 2, 2018 at 9:59
  • 1
    \usepackage[nobottomtitles]{titlesec} also worked for me
    – Prevost
    Commented Aug 23, 2018 at 19:18
17

As Will Robertson says, this really should not be happening with standard document classes and packages. You might like to try redefining the sectioning commands with the titlesec package; I usually do this anyway to get the section heading styles I want, and titlesec has features to control the breaking and positioning of section headings.

The other suggestions regarding \widowpenalty and enlarging the page are not helpful here; they're for problems with regular flowing text, and section headings mess up the normal flow with their spacing commands.

Edit: What's probably happening is that you have a chapter with non-text material that's forcing TeX to choose the "least bad" break it can, which in this case happens to be splitting a section heading from its contents; this is Really Bad, but if it's the only option it's the only option. Usually this is caused by floats; if you have floats near the problem heading, you may try playing with them. Or not playing with them; LaTeX floats are dread beasts only the bravest dare do true battle with.

For a quick fix, you can try the titlesec package as I mentioned earlier. The titlesec manual gives (section 9.2, page 26 of 27) titlesec versions of the standard LaTeX headings. The LaTeX sectioning commands are well known to be ugly, internally and externally (the non-chapter headings aren't so bad, but the rumor that the standard classes were designed to be so ugly that people would be obligated to create their own classes exists for a reason). Paste the following into your preamble (before \begin{document}) and see if it helps after rerunning LaTeX until it stabilizes:

\usepackage{titlesec}
\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titleformat{\section}
{\normalfont\Large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
{\normalfont\large\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}
{\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}
\titleformat{\paragraph}[runin]
{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\titleformat{\subparagraph}[runin]
{\normalfont\normalsize\bfseries}{\thesubparagraph}{1em}{}
\titlespacing*{\chapter} {0pt}{50pt}{40pt}
\titlespacing*{\section} {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\titlespacing*{\subsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\paragraph} {0pt}{3.25ex plus 1ex minus .2ex}{1em}
\titlespacing*{\subparagraph} {\parindent}{3.25ex plus 1ex minus .2ex}{1em}

With those definitions in place, your section headings should look just about identical, but internally be generated by titlesec's cleaner code, and hopefully exhibit saner behavior. Without seeing the document in question it's hard to predict if they'll help or not, but the fix is easy enough that it's worth a shot. Also, this exposes the commands' definitions for tweaking -- if you adjust the spacing values for \section in that block I'm sure you'll be able to find something that works (but it might be even uglier than the broken header!). You can also get ambitious and try defining your own heading styles, but this is 1) a waste of time for documents shorter than book length and 2) likely to go horribly wrong without some experience in typography and/or design.

Hope that helps.

2
  • I had understood that this wouldn't happen, either. Alas, I was wrong. I'd be mighty appreciative for suggestions as to where to start looking to figure out what's going on. Commented Feb 5, 2009 at 4:43
  • I've added a lengthy edit that gives a potential fix using titlesec; I'd have posted it as a comment, but this 300-character limit annoys me to no end.
    – kquinn
    Commented Feb 5, 2009 at 6:14
8

I once had a similar problem and used a tip also found here:

\widowpenalty=1000
\clubpenalty=1000
2
  • Which also prevents single lines from long paragraphs being stranded at the top or bottom of a page. There is some chance of this causing other weird layout decisions, but all it has every done too me was generate big chunks of whitespace... +1 Commented Feb 4, 2009 at 20:05
  • 3
    Unlike the accepted answer, this actually answers the question.
    – Guillochon
    Commented Oct 5, 2017 at 17:43
6

In case you have completed editing the LaTeX file or if you are positive that the position of the heading(right now at the bottom of the page) is not going to change, you can include a pagebreak before the \section{}. I know this is crude, but it's a quick fix.

1
  • I think this is a fine solution and I do it myself. One just has to keep in mind to do this last, after all editing is done and one is finalizing the formatting.
    – j0equ1nn
    Commented Feb 28, 2017 at 23:41
4

Have a look at these pages in the TeX FAQ:

What I normally do is add a \clearpage to force a page break where necessary, or add \enlargethispage{\baselineskip} to increase the height of the current page by one line, or \enlargethispage{-\baselineskip} to reduce the height of the page. But, it's best to leave these kinds of cosmetic changes until the document content is complete, to avoid continually readjusting the layout while you're editing the document.

3

Here is an example to show that this shouldn't happen by default. Perhaps you can try and create something similar based on your document to demonstrate the problem on your end, although I do understand that these can be tricky problems to reproduce and debug.

In the following example, we've got twenty-five lines of text preceding a section title. Uncomment the \oneline to add just one line of text and -- despite the fact that it could fit if the amount of text below the section title was reduced -- the entire section heading is pushed to the next page to avoid the problem you're encountering in your own document.

\documentclass[oneside,12pt]{article}
\def\oneline{%
  text text text text abc def ghi text 
  text text text text abc def ghi text 
  text
}
\def\fiveline{\oneline\oneline\oneline\oneline\oneline\par}
\def\twentyfiveline{\fiveline\fiveline\fiveline\fiveline\fiveline}
\begin{document}
\section{one}
\twentyfiveline
%\oneline
\section{two}
\twentyfiveline
\end{document}
0

A simple and robust way to handle these problems is to use the \needspace{<length>} command, which is available in the needspace package and also provided by the memoir class.

The command will check wheather the provided <length> is available as vertical space on the current page/in the current column - if not, it will flush whatever comes after to a new page/column.

Example:

\documentclass{article}
\usepackage{needspace}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua.

\needspace{2 \baselineskip}
\section{My heading}

Paragraph text which should be on the same page/in the same column as the heading above.

\end{document}

You could provide the \needspace command manually as above, or incorporate it into the \section command by re-defining it or by some other means.

If you use the memoir class, a few high-level commands are available to achieve this, e.g. \setsecheadstyle. In the preamble, use e.g. the following:

\setsecheadstyle{%
\needspace{3.0 \baselineskip}%
\Large% heading formatting
}
7
  • but what are "these problems" ? Can you show an example where latex is breaking the page after a section heading and \needspace prevents that? \needspace is doing nothing in the example code here, so it isn't clear that this answers the question. Commented May 31, 2021 at 18:51
  • to get a page break after the section heading you need something like \section{My heading} \clearpage Paragraph text... and \needspace does not prevent a page break in that case. Commented May 31, 2021 at 18:59
  • @david-carlisle "these problems" (e.i. page or column break after heading) are reported by seven users among this question + its answers alone, or eight including myself. When it happened for me recently I was using the memoir class with a pre-defined pagestyle and I for sure hadn't changed any penalties manually. Pagebreaks after headings sometimes happen. Can some be explained by careless users overriding penalties? Yes. All? For sure no. Could all or most be explained by something else, if digging deep enough? Possibly. Solutions are useful nonetheless.
    – Carl
    Commented May 31, 2021 at 21:52
  • @david-carlisle You're right that my example above doesn't illustrate the problem - that was not my intention with it. The \needspace solution solved it for me and other users have reported success elsewhere. Apparently there are cases where the break happens without a \clearpage inserted after the heading (I agree that wouldn't be helped by \needspace).
    – Carl
    Commented May 31, 2021 at 21:55
  • you are not the first person in this thread to say that this problem occurs, but no one has posted any example. LaTeX sets an infinitely high penalty of breaking after a heading so as far as I can see any case where it does break there would be equivalent to my \clearpage example. unless of course there is a bug somewhere which we could investigate but not if no one ever provides an example. You say "it solved it for me" but that implies the problem occured for you, can you add that example? Commented May 31, 2021 at 22:28
-2

It will never happen if you do not put the blank line (which is equivalent to a paragraph break in TeX/LaTeX) between the section command (i.e., \section{Heading}) and the text that follows it. Instead of what you have typed, including a blank line, viz.,

\section{Heading}

Here is some text after the heading.

type

\section{Heading}
Here is some text after the heading.

Alternatively, if you think that a blank line helps you guide your eyes when you are editing, type

\section{Heading} 
%
Here is some text after the heading.
1
  • 2
    This didn't work in my case :( Commented Jun 12, 2014 at 18:40

You must log in to answer this question.

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