Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 15 down vote accepted

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.

share|improve this answer
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
add comment

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}
share|improve this answer
    
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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