5

I am trying to set the font color of a section title that is formatted with leftmargin. When I don't use the leftmargin format, the font color changes correctly, but once I add the leftmargin format the color remains at default black.

\documentclass{article}

\usepackage{titlesec,xcolor}

\titleformat{\section}[leftmargin]   % When leftmargin is removed it works correctly
{\scshape\color{red}\fontsize{15pt}{11}\selectfont}
{}
{0.5em}                         
{}

\begin{document}
\section{Section Heading}
\end{document}
2
  • When I compile your example it looks terrible (the heading overlaps with the text). Are you sure that you want to have it like this? Moreover, you can't use \fontsize{15}{11}. Computer Modern does not know about 15pt. It switches to \fontsize{14}{11} anyways.
    – Ruben
    Mar 31, 2014 at 11:22
  • Yeah, it is horrible. My actual document is quite a lot different - I was just using that as a minimum working example. Thanks for the info on the font size - I didn't realise that.
    – abeverley
    Mar 31, 2014 at 12:39

1 Answer 1

5

I am scared of the result. But anyway, you may use the explicit option of the titlesec package and define

\titleformat{\section}[leftmargin]   % When leftmargin is removed it works correctly
{\scshape\fontsize{15pt}{18pt}\selectfont}
{}
{0.5em}
{\textcolor{red}{#1}}

Code:

\documentclass{article}
%%\usepackage{lmodern}    %% you may need this
\usepackage[explicit]{titlesec}
\usepackage{xcolor}

\titleformat{\section}[leftmargin]   % When leftmargin is removed it works correctly
{\scshape\fontsize{15pt}{18pt}\selectfont}
{}
{0.5em}
{\textcolor{red}{#1}}

\begin{document}
\section{Section Heading}
\end{document}

enter image description here

Also note that you got font size specifications wrong and I have corrected it.

Without explicit option it works this way.

\titleformat{\section}[leftmargin]   % When leftmargin is removed it works correctly
{\fontsize{15}{18}\scshape}
{}
{0.5em}
{\color{red}}

Also as noted by Gonzalo, \scshape also selects \selectfont

Another option is to use \textcolor withour explicit option as noted by egreg this way:

\titleformat{\section}[leftmargin]   % When leftmargin is removed it works correctly
{\fontsize{15}{18}\scshape}
{}
{0.5em}
{\textcolor{red}}

Also to obtain any font size load fixltx2e package.

8
  • Just \textcolor{red} is sufficient, without the explicit option. For "any size", load fixltx2e.
    – egreg
    Mar 31, 2014 at 11:56
  • Thanks @Harish, that works great (and thanks for the info on the font specs). @egreg I found that I did need to specify explicit for it to work (and I had to specify #1 to the "before" code of my later subsection definition for it to display properly). Thanks for the info on fixltx2e.
    – abeverley
    Mar 31, 2014 at 12:44
  • There's really no need for the explicit option. This is enough: \titleformat{\section}[leftmargin] {\fontsize{15}{18}\scshape} {} {0.5em} {\color{red} (notice also that \scshape makes the \selectfont automatically). {\color{red}}` Mar 31, 2014 at 16:50
  • @egreg I have updated the answer. Thanks :)
    – user11232
    Mar 31, 2014 at 22:58
  • @GonzaloMedina I have updated the answer. Thanks :)
    – user11232
    Mar 31, 2014 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .