# Coloring chapters and sections with respect to parts

I am using the book document class and I want to produce a document with several parts, chapters, sections, sections and subsections. I would like to make it easier for the reader by coloring everything with respect to parts. For instance, I would like:

• The title for Part A to be blue
• The chapter titles inside Part A to be blue
• All the heading for the chapter titles in Part A to be blue

and then..

• The title for Part B to be red
• The chapter titles inside Part B to be red
• All the heading for the chapter titles in Part B to be red

etc..

Would anybody be able to help with something like that? Thanks a lot in advance!

PS. The beginning of my document looks like this:

\documentclass[11pt,letterpaper]{book}
\usepackage{amsfonts,amsthm,amssymb}
\usepackage[tbtags]{amsmath}
\usepackage{color}

\usepackage{fancyhdr}
\pagestyle{fancy}

\renewcommand{\thepart}{\Alph{part}}
\renewcommand{\thechapter}{\Roman{chapter}}

-

## migrated from stackoverflow.comMar 26 '12 at 22:55

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – Gonzalo Medina Mar 26 '12 at 23:41

Here' a possible solution using the titlesec package to add the color to the chapter headings; the color for the part headings (and their ToC entries) was added through a redefinition of the internal macros \part, \@part and \@spart; the color for the chapter entries in the ToC was achieved by redefining \@chapter. The color schema was implemente through a simple \ifcase:

\documentclass[11pt,letterpaper]{book}
\usepackage{amsfonts,amsthm,amssymb}
\usepackage[tbtags]{amsmath}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{chngcntr}

\newcommand\MyColor{black}

\usepackage{fancyhdr}
\pagestyle{fancy}

\counterwithin{chapter}{part}
\renewcommand{\thepart}{\Alph{part}}
\renewcommand{\thechapter}{\Roman{chapter}}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries\color{\MyColor}}{\chaptertitlename\ \thechapter}{20pt}{\Huge}

\makeatletter
\renewcommand\part{%
\if@openright
\cleardoublepage
\else
\clearpage
\fi
\thispagestyle{plain}%
\renewcommand\MyColor{%
\ifcase\value{part} \or blue\or red\or olive\or magenta\else yellow\fi}
\if@twocolumn
\onecolumn
\@tempswatrue
\else
\@tempswafalse
\fi
\null\vfil
\secdef\@part\@spart}
\def\@part[#1]#2{%
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\else
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\bfseries\color{\MyColor} \partname\nobreakspace\thepart
\par
\vskip 20\p@
\fi
\Huge \bfseries #2\par}%
\@endpart}
\def\@spart#1{%
{\centering
\interlinepenalty \@M
\normalfont
\Huge \bfseries #1\par}%
\@endpart}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{\color{\MyColor}\protect\numberline{\thechapter}#1}%
\else
\fi
\else
\fi
\chaptermark{#1}%
\if@twocolumn
\else
\fi}
\makeatother

\begin{document}

\tableofcontents

\part{Test Part One}
\chapter{Chapter A-One}
\lipsum[1]
\section{Test section A-One One}
\lipsum[1-10]

\chapter{Chapter A-Two}
\lipsum[1]
\section{Test section A-Two One}
\lipsum[1-10]

\part{Test Part Two}
\chapter{Chapter B-One}
\lipsum[1-7]
\chapter{Chapter B-Two}
\lipsum[1-7]

\part{Test Part Three}
\chapter{Chapter C-One}
\lipsum[1-7]
\chapter{Chapter C-Two}
\lipsum[1-7]

\end{document}


Here are some pages of the final document:

And an image of the ToC:

Just as a personal comment, this coloring schema has to be handled very carefully (selecting the appropriate colors is crucial) in order not to become distracting and to avoid achieving an unpleasant layout.

-
Thanks!Worked like a charm! I will definitely be careful when it comes to the choice of the colours, I'm aware of how ugly/distracting this can potentially be. – geo909 Mar 29 '12 at 9:22
I was wondering if you have anything in the top of your head about how to make the chapter/parts in the table of contents in the corresponding colors, as well.. – geo909 Mar 29 '12 at 10:48
@geo909: see my updated answer. – Gonzalo Medina Mar 29 '12 at 12:19
Many thanks! That's exactly what I wanted.. – geo909 Mar 29 '12 at 15:47
@GonzaloMedina Nice output with edited chapter titles. – azetina Mar 29 '12 at 19:20

You can modify header formatting using the sectsty package. For example after loading the package, use \partfont{\color{blue}} to change the color of part headings, \chapterfont{\color{blue}} for chapter headings, etc. Other formatting can be applied using this package as well; It might be useful to refer to the sectsty package documentation for other modifiers (font size, font weights, font shape, etc.).

In this case, I define a variable \pcolor so that the color can be changed for each part. Before each part, use \renewcommand\pcolor{} to define the color for the following part; to prevent the color change from affecting the previous section, use \clearpage before changing the color. Page header font color is changed by modifying the fancyhdr definitions.

Here is a MWE showing the changes:

\documentclass[11pt,letterpaper]{book}
\usepackage{amsfonts,amsthm,amssymb}
\usepackage[tbtags]{amsmath}
\usepackage{color}
\usepackage[english]{babel}
\usepackage{lipsum}

\newcommand\pcolor{black}

\usepackage{fancyhdr}
\pagestyle{fancy}

\renewcommand{\thepart}{\Alph{part}}
\renewcommand{\thechapter}{\Roman{chapter}}

\usepackage{sectsty}
\partfont{\color{\pcolor}}
\chapterfont{\color{\pcolor}}

\begin{document}
\renewcommand\pcolor{blue}
\part{Part 1}

\chapter{Chapter 1}
\lipsum
\chapter{Chapter 2}
\lipsum

\clearpage
\renewcommand\pcolor{red}
\part{Part 2}
\chapter{Chapter 3}
\lipsum
\chapter{Chapter 4}
\lipsum

\end{document}


Here a some snapshots of the output showing chapter header formatting and page header formatting.

-
Thanks a lot! It almost worked but I did have a minor issue. In particular, the \clearpage thing didn't work for me; even when I was using it as in your example, I was getting the header of the last page of each part to be in the color of the next one.. Maybe this can somehow fixed but the previous answer was working so I went with it. Thanks again. – geo909 Mar 29 '12 at 9:25