[Tex/LaTex] How to change the colour of the document’s section numbering

colornumberingsectioning

I know how to change the colour of a section name using for example

\section{\color{Red} Foobar}

but that still leaves the section number in black. Is there any way to change the colour of the numbers of the sections, subsections, etc?

Best Answer

I'm sure there is some package that does this, but the simplest way is to redefine the \thesection command:

\renewcommand\thesection{\color{red}\arabic{section}}

or if you also want the chapter number:

\renewcommand\thesection{\color{red}\thechapter.\arabic{section}}

Similar commands work for \thechapter, \thesubsection, etc.

EDIT: A maybe unintended side effect of this is that it also color every reference to the section. To just change it in the section header you can use

\makeatletter
\renewcommand\@seccntformat[1]{\color{red} {\csname the#1\endcsname}\hspace{0.5em}}
\makeatother

In any case you might want to have a look at section 2.2 of the LaTeX Companion and the titlesec package.