[Tex/LaTex] How to prevent page break between two section headings with color formatting using titlesec

page-breakingsectioningtitlesec

Description

This question is based on almost the the following question,
How to prevent page break between two section headings. The answer given there explains that the problem is with \color, which adds a whatsit while in vertical mode (vertically breakable).

The solution is to use \textcolor, which takes two parameters (color and text), and does not insert a whatsit. This is described as follows:

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.

The answer uses the standard section definitions. I would like to have a solution that uses atitlesec definition (with the explicit parameter enabled).

Details not really part of problem but possibly important

I use logic for coloring certain tags within the titles from How can I write a conditional coloring command that distinguishes variables within sectioning commands vs those within the body text?:

\titleformat{\section}[hang]{\colorspecialstagstrue\color{black}\Huge\bfseries}{}{0pt}{\thesection\quad#1}

\newif\ifcolorspecials
\DeclareRobustCommand{\specialtag}[1]{%
    \textbf{\textit{{\ifcolorspecials\color{orange}\fi #1}}}%
}%

Example

Note that the title number (label) \thesection should normally be in parameter 4, because parameter 5 allows you to easily input the horizontal separation between the label and the title text. I have advanced code in the real document (using TikZ) that makes it necessary to include it in parameter 6.

\documentclass{article}
\usepackage{fontspec}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{lipsum}

% Title/Heading Formatting
% SECTION
\titleformat{\section}[hang]{\color{black}\Huge\bfseries}{}{0pt}{\thesection\quad#1} % \thesection "incorrectly" in 6th param due to tikz formatting in actual document.
\titlespacing{\section}{0pt}{40pt}{5ex plus .2ex}
% SUBSECTION
\titleformat{\subsection}[hang]{\color{red}\Large\bfseries}{\thesubsection\quad}{0pt}{#1} % Color in 3rd param adds whatsit messing up nobreak mechanism between titles/headings
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
% SUBSUBSECTION
\titleformat{\subsubsection}[hang]{\color{red}\normalsize\bfseries}{\thesubsubsection\quad}{0pt}{#1}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}


\begin{document}
\section{Family}
\lipsum[1-4]
More junk to take up space and hopefully force a page break between Genus and Species such that this example illustrates the problem.
\subsection{Genus}
\subsubsection{Species}
\end{document}

Output

enter image description here

Best Answer

Just as with your specification for \section, you can move all the printing commands into the final argument. This then allows you to color them with \textcolor. In your example you then get

Sample output

on page two, rather a page break between the section commands.

\documentclass{article}
\usepackage{fontspec}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{lipsum}

% Title/Heading Formatting
% SECTION
\titleformat{\section}[hang]{\Huge\bfseries}{}{0pt}{\textcolor{black}{\thesection\quad#1}}
\titlespacing{\section}{0pt}{40pt}{5ex plus .2ex}
% SUBSECTION
\titleformat{\subsection}[hang]{\Large\bfseries}{}{0pt}{\textcolor{red}{\thesubsection\quad#1}} 
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
% SUBSUBSECTION
\titleformat{\subsubsection}[hang]{\normalsize\bfseries}{}{0pt}{\textcolor{red}{\thesubsubsection\quad #1}}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}


\begin{document}
\section{Family}
\lipsum[1-4]
More junk to take up space and hopefully force a page break between Genus and Species such that this example illustrates the problem.
\subsection{Genus}
\subsubsection{Species}
\end{document}

Probably you will need to do something similar in your tagging command:

\DeclareRobustCommand{\specialtag}[1]{%
    \textbf{\textit{{\ifcolorspecials\textcolor{orange}{#1}\else #1\fi}}}%
}%