1

How do I color all headlines of a specific type in a document easily? I want every \subsection{} to be in the color "peru": \definecolor{peru}{rgb}{0.8,0.52,0.25}, and I don't want to write \subsection{\color{peru} abc } for every subsection I create. Should I redefine the \subsection command with \renewcommand, or is there an easier way?

Also: Could you set a "standard" color for each headline type, so that you could have the sections in green, subsections in peru and the subsubsections in orange?

2
  • You can do all the things you ask for but the answer depends quite strongly on the used class.
    – campa
    Nov 18, 2019 at 13:13
  • This is primarily for the article class, but it would also be useful in the book class.
    – Vebjorn
    Nov 18, 2019 at 13:28

1 Answer 1

1

The low-level solution would be to look at the definition of \subsection, which in the article reads

\newcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\large\bfseries}}

Then you could patch this definition by adding your colour in the last argument (and putting everythig between \makeatletter and \makeatother).

A more user-friendly approach is through the sectsty package

\documentclass{article}

\usepackage{sectsty}
\usepackage{xcolor}

% \chapterfont{\color{orange}} % for book.cls and report.cls
\sectionfont{\color{red}}
\subsectionfont{\color{blue}}
\subsubsectionfont{\color{teal}}

\begin{document}

\section{Foo}
\subsection{Bar}
\subsubsection{Baz}
Text.

\end{document}

enter image description here

1
  • I want to write a class .cls file for these types of articles and thought I wanted to make these commands: \setsectioncolor{} and \sectioncolor. That way I can renew section and put it in like this: {\sectioncolor\normalfont\large\bfseries}. But how should I define these? \newcommand{\setsectioncolor}[1]{\color[#1]} \newcommand{\sectcolor}{}.
    – Vebjorn
    Nov 18, 2019 at 17:19

You must log in to answer this question.

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