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.

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}
share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

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

share|improve this answer
    
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 at 13:53
add comment
\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}
share|improve this answer
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.