With the following MWE, i get the Section title underlined in black :
\documentclass[12pt]{article}
\usepackage[explicit]{titlesec}
\usepackage[dvipsnames]{xcolor}
\titleformat{\section}%
{\large\sffamily\bfseries}%
{Section \arabic{section}}%
{0.5em}%
{#1}%
[{\titlerule[2.5pt]}]
\begin{document}
\section{mon titre}
\end{document}
My first problem : if i remove the { }
enclosing \titlerule
, i get a compilation error
! LaTeX Error: Missing \begin{document}
Which is strange, since the content of the [ ]
is a text to typeset after the section title. For example
[some-after-text]
works. My second problem : if i colorize the rule with
[\textcolor{blue}{\titlerule[2.5pt]}]
I obtain a blue underline, but with an additional vertical space (app 0.5 cm) between the title and the underline. If i use
[{\color{blue}\titlerule[2.5pt]}]
instead, the vertical space disappears... I have to admit, i do not understand that.
\textcolor
starts a new paragraph (mentioned in @egreg's answer), add\makeatletter\show\@textcolor\makeatother
after\titleformat
, then inspect your.log
file and see that it executes\leavevmode
(\@textcolor
is just a subsidiary function called by\textcolor
). Then read Function and usage of\leavevmode
.