I use \usepackage[explicit]{titlesec}
to customize the design of my sections, subsections…
In particular, i have :
\titleformat{\section}%
{\large\sffamily\bfseries}%
{My Section \arabic{section} }%
{0.5em}%
{#1}% \\ \hspace*{-1.5cm}
[{\color{blue}\titlerule[2.5pt]}]
where the {\color{blue}\titlerule[2.5pt]}
produces an underline. The problem is, that a page breaking can happen just after the section title, and the line would appear on the next page. Is there a way to prevent that ?
EDIT : Minimal Example:
\documentclass[12pt]{article}
\usepackage[explicit]{titlesec}
\usepackage[dvipsnames]{xcolor}
\titleformat{\section}%
{\large\sffamily\bfseries}%
{Section \arabic{section}}%
{0.5em}%
{#1}% \\ \hspace*{-1.5cm}
[{\color{blue}\titlerule[2.5pt]}]
\begin{document}
\section{titre1}
blabla
\vspace{16cm}\\
blabla
\section{titre2}
blabla blabla
\end{document}
Best Answer
Replace
{\color{blue}<rule definition>}
with\textcolor{blue}{<rule definition>}
.EDIT: Note that this solution produces unwanted additional vertical space between title and underline. See Strange behaviour of titlesec, with colored titlerule and particularly egreg's answer for a solution to this follow-up problem.