# Colored section title fails with [leftmargin] option of titlesec

I want my section title colored. It's all right when I use \titleformat from the package titlesec to set it.

But when I add the option argument [leftmargin], the title text fails to change color. Is something wrong with my code?

MWE:

\documentclass{article}
\usepackage{titlesec,xcolor}

\begin{document}
\titleformat{\section}
{\normalfont\bfseries\color{blue}}
{\thesection.}
{1em}
{}
\section{my colored section}

\titleformat{\section}[leftmargin]% only the optional argument "leftmargin" added
{\normalfont\bfseries\color{blue}}
{\thesection.}
{1em}
{}
\section{The Color Unchanged}

\end{document}


A work-around: put the colour command in the 2nd and 4th arguments.

\documentclass{article}
\usepackage{,xcolor, titlesec}

\begin{document}
\titleformat{\section}
{\normalfont\bfseries\color{blue}}
{\thesection.}
{1em}
{}
\section{my colored section}

\titleformat{\section}[leftmargin]% only the optional argument "leftmargin" added
{\normalfont\bfseries}
{\color{blue}\thesection.}
{1em}
{\color{blue}}
\section{The Color Unchanged}

\end{document}


• Yes, this can solve my problem. But I think this is only a temparory solution. Is this a problem of titlesec? Is there a final solution? – lyl Feb 18 '19 at 1:28
• One should take a look at the code for the leftmargin option. – Bernard Feb 18 '19 at 9:43
• Oh, I think this is too hard for me. – lyl Feb 18 '19 at 11:47