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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI'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.
xcolor Error: undefined color 'RED'
error for me, regardless of any color I enter.
Jun 2, 2016 at 9:04
xcolor
or color
package loaded and red
color defined? In other words, does {\color{red}Foo}
anywhere in your document typeset red Foo?
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}
sectsty
package offers a coherent and unified way of customizing headings. Anyway, you got my +1.
\color
there is not recommended because of an inserted "whatsit". See tex.stackexchange.com/a/101643/13552.
Jul 5, 2018 at 6:57
Instead of using \section{\color{red} Foobar}
use \textcolor{red}{\section{Foobar}}