# 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}

% 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
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
% SUBSUBSECTION
\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

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

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

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

% SECTION
\titlespacing{\section}{0pt}{40pt}{5ex plus .2ex}
% SUBSECTION
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
% SUBSUBSECTION
\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}}}%
}%

• My problem with the tagging command is that my entire title text variable is passed into a TikZ node (I keep getting errors). I know that I did not provide a working example for this, because I wanted to keep my example to a minimum. I will post some notes later in my question about this. You did answer the question, however. – Jonathan Komar Jul 7 '15 at 12:08

The best way to cope with formatting of section titles is to use a macro with one argument in the last argument to \titleformat. Color specifications should only go in this auxiliary macro.

Here is an example.

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{lipsum}

% SECTION
\titleformat{\section}[hang]
{\Huge\bfseries\global\let\thisnumber\relax}
{0pt}
{\dosection}
\titlespacing{\section}{0pt}{40pt}{5ex plus .2ex}
% SUBSECTION
\titleformat{\subsection}[hang]
{\Large\bfseries\global\let\thisnumber\relax}
{0pt}
{\dosubsection}
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
% SUBSUBSECTION
\titleformat{\subsubsection}[hang]
{\normalsize\bfseries\global\let\thisnumber\relax}
{0pt}
{\dosubsubsection}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\newcommand{\dosection}[1]{%
\textcolor{black}{\thisnumber#1}%
}
\newcommand{\dosubsection}[1]{%
\textcolor{red}{\thisnumber#1}%
}
\newcommand{\dosubsubsection}[1]{%
\textcolor{red}{\thisnumber#1}%
}

\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}

\section*{Test}
\subsection*{Test}
\subsubsection*{Test}
\end{document}


I also show that unnumbered sections work as expected. In the definition of \dosection and the similar macros you can test whether \thisnumber is \relax or not, instead of just using \thisnumber with

\ifx\thisnumber\relax
<code for a starred section>
\else
<code for a numbered section using \thisnumber>
\fi