5

By default, LaTeX allows a page break after the first two lines of paragraphs following section/subsection headings, but I would like to keep the section heading with even one line below it at the bottom of the current page. How to do it?
Please note that I'd prefer not to use any additional packages such as etoolbox or needspace.
Here is my MWE:

\documentclass{book}
\usepackage[showframe]{geometry}% just for this example
\usepackage{lipsum}
\newcommand{\dummy}{This is a test }
\begin{document}
\lipsum[1-6]
\section{bla}
This is a test  This is a test  This is a test This is a test 
This is a test This is a test This is a test 
This is a test 
\end{document}
5

Without any additional package, add the following lines in your preamble

\makeatletter
\renewcommand\@afterheading{%
  \@nobreaktrue
  \everypar{%
    \if@nobreak
      \@nobreakfalse
      \clubpenalty 1
      \if@afterindent \else
        {\setbox\z@\lastbox}%
      \fi
    \else
      \clubpenalty 1
      \everypar{}%
    \fi}}
\makeatother

MWE:

\documentclass{book}
\usepackage[showframe]{geometry}% just for this example
\usepackage{lipsum}
\newcommand{\dummy}{This is a test }

\makeatletter
\renewcommand\@afterheading{%
  \@nobreaktrue
  \everypar{%
    \if@nobreak
      \@nobreakfalse
      \clubpenalty 1
      \if@afterindent \else
        {\setbox\z@\lastbox}%
      \fi
    \else
      \clubpenalty 1
      \everypar{}%
    \fi}}
\makeatother

\begin{document}
\lipsum[1-6]
\section{bla}
This is a test  This is a test  This is a test This is a test
This is a test This is a test This is a test
This is a test
\end{document} 

Output:

enter image description here

  • unfortunately, this will also result two possibly unwanted outcomes: in the test provided, the next page contains only one short line; that would not be wonderful if it ended a chapter (unless it's done intentionally for effect). and any subsequent page may end with a single line of a longer paragraph, which may not be intended. – barbara beeton Feb 23 '14 at 13:53
2
\documentclass{book}
\usepackage[showframe]{geometry}% just for this example
\usepackage{lipsum}
\newcommand\dummy{This is a test }
\widowpenalty=-1000 %%%%%%%%%%%%%%%%%%%%%%%%%%
\clubpenalty=-1000  %%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\lipsum[1-6]
\section{bla}
This is a test  This is a test  This is a test This is a test 
This is a test This is a test This is a test 
This is a test 
\end{document}

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.