Text FormattingColored chapter and section headings

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
evilmustardseed
Posts: 1
Joined: Tue Dec 08, 2020 6:18 pm

Colored chapter and section headings

Post by evilmustardseed »

How do I change the colors of chapter, section and subsection headings in a book document class? I can change the color of text within the document using the xcolor package and the \color{} command, but it won't work when I include the command in the name of a chapter or section.

So far, I have tried the following within the document:

\chapter{\color{purple}Chapter title}

\chapter{\textcolor{purple}{Chapter title}}

Both of these returned Error: Undefined color `Purple' even though purple is a color included in xcolor. Indeed, when I use a command within the body of the text such as {\color{purple}blah blah blah} the text is output in the appropriate color with no error message.

Is there a way to change the color of chapter and section headings globally in the preamble? Can I change the colors of the chapter and section headings individually so that each chapter heading has different colors?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Colored chapter and section headings

Post by Ijon Tichy »

If you are using the standard book class you need an extra package like titlesec to change fonts or colors of headings. With a more configurable class like the KOMA-Script class scrbook, changing fonts or colors is very easy:

Code: Select all

\documentclass{scrbook}

\usepackage{xcolor}
\usepackage{blindtext}
\addtokomafont{disposition}{\color{purple}}% color of all headings
\addtokomafont{chapterentry}{\normalcolor}% but not in the ToC

\begin{document}
\tableofcontents
\blinddocument
\end{document}
or different colors for different levels:

Code: Select all

\documentclass{scrbook}

\usepackage{xcolor}
\usepackage{blindtext}
\addtokomafont{chapter}{\color{purple}}
\addtokomafont{section}{\color{magenta}}
\addtokomafont{subsection}{\color{green}}

\begin{document}
\tableofcontents
\blinddocument
\end{document}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
neuro
Posts: 3
Joined: Sat Jan 11, 2020 11:00 am

Colored chapter and section headings

Post by neuro »

Does anybody have an idea how to do this *within* the text?
I have to change the section colors for different sections, but all ideas I found are used in the preamble for the whole document...
(I cannot use koma, since I am using a very old latex/lyx)
And since I am using lyx, it would be helpful to change the style for all following sections, instead of using the \section command..
Post Reply