[Tex/LaTex] Coloring chapters and sections with respect to parts

colorformattingsectioning

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}
\fancyhead[LE,RO]{\slshape\thepage}
\fancyhead[RE]{\slshape \leftmark}
\fancyhead[LO]{\slshape \rightmark}

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

Best Answer

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}

\fancyhead[LE,RO]{\slshape\color{\MyColor}\thepage}
\fancyhead[RE]{\slshape\color{\MyColor}\leftmark}
\fancyhead[LO]{\slshape\color{\MyColor}\rightmark}

\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}%
      \addcontentsline{toc}{part}{\color{\MyColor}\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{\color{\MyColor}#1}%
    \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.}%
                         \addcontentsline{toc}{chapter}%
                                   {\color{\MyColor}\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{\color{\MyColor}#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{\color{\MyColor}#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \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:

enter image description here

enter image description here

enter image description here

And an image of the ToC:

enter image description here

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.

Related Question