[Tex/LaTex] How to prevent page break between two section headings

page-breakingsectioning

I have a custom document class, based on book.cls, for which the following code sometimes produces page breaks between two subsequent sections:

  \section{Introduction}
  % page break may occur here
  \subsection{Getting started}

I am completely puzzled, because my definitions for the sectioning commands are almost identical to those in book.cls:

\newcommand \headingfont {\fontfamily{phv}\fontseries{bc}\fontshape{n}\selectfont}
\newcommand\section{\@startsection {section}{1}{\headerindent}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2ex \@plus.2ex}%
                                   {\color{MMIblue}\headingfont\Large}}
\newcommand\subsection{\@startsection{subsection}{2}{\headerindent}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {2ex \@plus .2ex}%
                                     {\color{MMIblue}\headingfont\large}}
\newcommand\subsubsection{\@startsection{subsubsection}{3}{\headerindent}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {2ex \@plus .2ex}%
                                     {\color{MMIblue}\headingfont\normalsize}}
% etc.

Any ideas what might cause LaTeX to allow a page break?

Best Answer

The problem is that \color{MMIblue} inserts a whatsit that disrupts the mechanism for disallowing page breaks after sectional titles.

Use

\newcommand\section{%
  \@startsection{section}{1}
    {\headerindent}%
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {2ex \@plus.2ex}%
    {\headingfont\Large\textcolor{MMIblue}}}

which works because the last instruction in the sixth argument to \@startsection reads the section title (with the number) as a braced group, which can then be interpreted by \textcolor as its second argument.