14

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?

21

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.

  • 1
    +1 Put the command in the preamble to change it globally. If you want to change just one section, you can try to redefine the command before and after the \section command to switch on and off the \color. But this probably won't affect the color in the TOC. – Willie Wong Jul 28 '10 at 10:50
  • This raises a xcolor Error: undefined color 'RED' error for me, regardless of any color I enter. – Anti Earth Jun 2 '16 at 9:04
  • @AntiEarth: If you want to get help with your problem, you should post a new question (with a complete example of what you did and a link back to this post). – Caramdir Jun 3 '16 at 3:50
  • @AntiEarth Do you have the xcolor or color package loaded and red color defined? In other words, does {\color{red}Foo} anywhere in your document typeset red Foo? – Crowley Jul 19 '16 at 17:49
4

It actually depends on what class you use, for instance, for article.cls, you can have:

\documentclass{article}
\usepackage{color}
\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries\color{red}}}
\makeatother
\begin{document}
\tableofcontents
\section{This is a section}
This is a test
\end{document}
  • @user2716 Wouldn't the sectsty package do the job? – chl Dec 30 '10 at 9:43
  • @chl: Perhaps you are right but I personally prefer to do these kind of simple things myself rather than relying on a particular package. There are thousands of packages on CTAN and it is very hard to know about all of them. – IRAN Jan 11 '11 at 12:26
  • @user2716 I agree (I did the same when writing my PhD) but I feel the sectsty package offers a coherent and unified way of customizing headings. Anyway, you got my +1. – chl Jan 11 '11 at 12:30
  • Using \color there is not recommended because of an inserted "whatsit". See tex.stackexchange.com/a/101643/13552. – Jonathan Komar Jul 5 '18 at 6:57
0

Instead of using \section{\color{red} Foobar} use \textcolor{red}{\section{Foobar}}

  • This will insert unwanted version space into the sectional heading. – Werner Jun 21 '18 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.