# Absolutely prevent pagebreak after section title

Okay I've searched but didn't find any existing solution. My question is how to absolutely forbid pagebreak immediately after section title. I tried:

\makeatletter
\def\neverbreak{\nopagebreak\@nobreaktrue}
\makeatother
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}\neverbreak}

but it doesn't work. Below is an example of the problem. I suspect titlesec is partly responsible, as the problem goes away when I remove it. But I want titlesec and my custom penalties, so how can this be solved?

\documentclass{article}
\usepackage{titlesec}
\usepackage{etoolbox}
\newtheorem{thm}{Theorem}
\AtBeginEnvironment{thm}{\thmbegin}
\interlinepenalty=1000
\widowpenalty=1000
\clubpenalty=1000
\parindent=0pt
\def\muchtext{text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text }
\makeatletter
\def\neverbreak{\nopagebreak\@nobreaktrue}
\makeatother
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}\neverbreak}
\begin{document}
\muchtext\muchtext

\muchtext\muchtext

\muchtext

\muchtext\muchtext\muchtext

\muchtext

\muchtext\muchtext\muchtext\muchtext

\section{title}
\begin{thm} \muchtext\muchtext
\end{thm}
\end{document}

• can you explan why do you use penalties? – touhami Jul 23 '15 at 18:55
• latex already does make it infinitely bad to break after the heading but you over-ride that and add a penalty of -1000 which is saying that that is a really good place to break. – David Carlisle Jul 23 '15 at 19:01
• @DavidCarlisle: By the way, why does my \neverbreak fail? Is it because it doesn't affect the start of the theorem environment? – user21820 Jul 24 '15 at 3:44
• It doesn't do anything. As I say the standard heading already makes it infinitely bad to break there so adding another penalty at that point does not change anything, the problem is not the heading it is the theorem code where you not only allow a page break, you strongly encourage it. – David Carlisle Jul 24 '15 at 7:44
• @DavidCarlisle: Thanks for the explanation! I also couldn't figure out how to encourage page-break before section titles using penalties. Do you know a way (I just don't like the idea of using needspace or manual adjustments)? – user21820 Jul 24 '15 at 8:12

Despite the penalties @davidcarlisle mentions, I find I often get troublesome lone headings and dangly bits in complex documents, particularly where subheadings follow higher level headings without intervening text. One way is to use needspace, defining how much space is required on the page before a heading of a particular level can be set.

Perhaps along the lines of the below depending on how much un-dangliness is wanted.

\usepackage{needspace}

\usepackage[explicit,pagestyles]{titlesec}

\titleformat{\subsubsection}{\needspace{4\baselineskip}\normalsize\sffamily\bfseries}{\thesubsubsection}{1em}{#1}[]

• Anyway, the above is what I do -- would welcome any comments on why it's a very bad approach... Obviously flushbottom is not very good with this, and I know there may perhaps be some issues with measuring and setting space before the heading if needspace is used without titlesec. – Aubrey Blumsohn Jul 23 '15 at 19:23
• there are some issues with the measuring (just the way it is in tex) but needspace does a pretty good job getting the right values, so it can make the wrong choice but probably won't and it has the advantage of giving more control, basically latex tries to keep the heading with 2 lines of following text, but by adjusting the values here gives you more control to keep a whole para or whatever. especially if the following material isn't plain text so "line" isn't a useful unit. so anyway either answer is useful depending (+1:-) – David Carlisle Jul 23 '15 at 20:38
• I gave up using flushbottom after finding it usually more ugly than beautiful haha.. @DavidCarlisle: How would one adjust the values here to always keep a whole paragraph? Or do you mean manually adjust for each case? – user21820 Jul 24 '15 at 3:42
• @user21820 I meant the latter, because you are specifying by length and specifying it before the heading and the following text is seen you can't base it on what is there automatically (in one run) but you can manually account for special things like images or display math that may mean that tex's internal concept of "paragraph line" might not match human expectation. – David Carlisle Jul 24 '15 at 7:48

If you really need to do this, you want \addpenalty rather than \penalty just as you used \addvspace rather than \vspace.

\documentclass{article}
\usepackage{titlesec}
\usepackage{etoolbox}
\newtheorem{thm}{Theorem}
\AtBeginEnvironment{thm}{\thmbegin}
\interlinepenalty=1000
% does nothing \widowpenalty=1000
% does nothing \clubpenalty=1000
\parindent=0pt
\def\muchtext{text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text }

%\makeatletter
%\def\neverbreak{\nopagebreak\@nobreaktrue}
%\makeatother
%\let\oldsection\section
%\renewcommand{\section}[1]{\oldsection{#1}\neverbreak}

\begin{document}
\muchtext\muchtext

\muchtext\muchtext

\muchtext

\muchtext\muchtext\muchtext

\muchtext

\muchtext\muchtext\muchtext\muchtext

\section{title}
\begin{thm} \muchtext\muchtext
\end{thm}
\end{document}

• Honestly I really did not know that! Everywhere I searched said something like "\penalty inserts a penalty ..." and I mistakenly thought it adds. Also, do you know how to show all the penalties in a document? Thanks a lot! – user21820 Jul 24 '15 at 3:39