# How to have colored headings with one color per chapter

I'm typesetting a proceedings-style book that contains articles in their chronological order. Since the articles are also categorized, each category is assigned a color and the the chapter styles are supposed to use that color as background color for some fancy chapter art. This part works as expected with titlesec.

In addition to that, I'd like to also change the color of the headings, which should be the same "chapter color" for the current chapter.

The problem I observe with my naive approach is that the headings color is changed too early, i.e. the page before the new chapter is already printed with the new color.

Here is a MWE:

\documentclass[fontsize=12pt, paper=a4]{scrbook}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage[automark]{scrpage2}
\usepackage{xcolor}

\newcommand{\chaptercolor}{red}

\cfoot[\color{\chaptercolor}\pagemark]{\color{\chaptercolor}\pagemark}

\begin{document}

\renewcommand{\chaptercolor}{blue}
\chapter{Ch1}
\Blindtext

\renewcommand{\chaptercolor}{green}
\chapter{Ch2}
\Blindtext

\end{document}


As you can see, on page 2 the heading is already green although this page still belongs to chapter 1, which is supposed to be blue.

What I also tried was to use two different variables (headercolor and chaptercolor), where I used \headercolor in the *head statements and used titlesec's \titelformat to assign the chaptercolor to the headercolor only when the chapter title itself is set. Unfortunately this didn't work either.

Update: I'm looking for a preamble solution. The MWE oversimplifies my current script and since the color is also used in the chapter title itself, it is important that the chapter color is defined before the chapter starts. I'd like to keep this kind of "interface" as stable as possible so that authors don't need to mess with details.

Any advise on how I can lazily set the color so that the color change in the headings happens only on the very first page of the new chapter is highly appreciated. Thanks!

• Either use a \clearpage beforehand or swap the colorchange after chapter. – Johannes_B Sep 24 '14 at 8:28
• The succesor of scrpage2 called scrlayer-scrpage allows to set backgroundcolors in easy way (there is an example in the documentation for yellow headers). You might find background colors with black text more readable. – Johannes_B Sep 24 '14 at 8:30
• Personally, I would not use colours for the sections/chapters. Have you thought of using coloured thumb-indexes as explained on page 20 in the fancyhdr manual? – user10274 Sep 24 '14 at 9:32
• Unfortunately the design is given. @Johannes_B: Manually adding a \clearpage helped indeed, however I could not find any way to make this automatically for each starting chapter. Any hints on how this can be achieved? – vanto Sep 24 '14 at 14:14
• btw: titlesec doesn't work that well together with KOMA. You should receive a \Huge warning. – Johannes_B Sep 24 '14 at 16:52

Define a new command wich executes \cleardoublepage and changes the color:

\newcommand{\changechaptercolor}[1]{%
\cleardoublepage%
\renewcommand\chaptercolor{#1}%
}


Then you can use

\documentclass[fontsize=12pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage{blindtext}

\usepackage{xcolor}
\newcommand\chaptercolor{red}
\newcommand{\changechaptercolor}[1]{%
\cleardoublepage%
\renewcommand\chaptercolor{#1}%
}

\usepackage[automark]{scrpage2}% or scrlayer-scrpage
\cfoot[\pagemark]{\pagemark}

\begin{document}
\changechaptercolor{blue}
\chapter{Ch1}
\Blindtext

\changechaptercolor{green}
\chapter{Ch2}
\Blindtext
\end{document}


As mentioned in the comments, it is sufficient to place the colorchange (done via \chaptercolor) after calling the chapter command.

\documentclass{scrbook}
\usepackage{blindtext}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{xcolor}

\cfoot[\pagemark]{\pagemark}
%Now adding color to all elements in the pagehead
%This is much easier to maintain

\begin{document}

\chapter{Mark Twain}
\newcommand{\chaptercolor}{blue}
\Blindtext

\chapter{Ambrose Bierce}
\renewcommand{\chaptercolor}{green}
\Blindtext

\end{document}


But i would prefer seeing normal black text on a mild colored background. For a one sided document you can define something like this

\DeclareLayer[clone=plain.scrheadings.head.oneside,
contents={%
\color{\chaptercolor}%
\rule[-\dp\strutbox]%
}%
contents={%
\color{\chaptercolor}%
\rule[-\dp\strutbox]%
}%


Resulting in the follwing output:

Another thing that might be worth a look gives this output

This can be achieved using the following code.

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{xcolor}

\cfoot[\pagemark]{\pagemark}

%Defining the Layer
\makeatletter
\newlength{\topheight}
\setlength{\topheight}{\sls@topmargin}
\DeclareLayer[
background,
contents={%
\color{\chaptercolor}%
\rule{\paperwidth}{\topheight}%
}%
\makeatother

%Adding the Layer to the pagestyles
}

\usepackage{etoolbox}
\newcommand{\basecolor}{%
\ifcase\arabic{chapter}\or blue\or orange\or green\or red\fi%
}
\newcommand{\chaptercolor}{\basecolor!20!white}
\begin{document}

\chapter{Mark Twain}
\Blindtext
\Blindtext
\chapter{Walt Whitman}
\Blindtext
\Blindtext
\Blindtext
\chapter{F. Scott Fitzgerald}
\Blindtext
\Blindtext
\chapter{Ambrose Bierce}
\Blindtext
\Blindtext
\end{document}


You can make it even more fancy by changing the color of the chapter title by adding

\newcommand{\chaptertitlecolor}{\basecolor!70!white}


Both, the colored bar on top and the title share the same basecolor. But i decided to go for a darker variant for better readability. The output looks like this:

One could even go further and print the header in a darker shade of the basecolor, which could look something like this

• Look at my second example, there is no manual change of colors, all are predefined in the preamble. add \addtokomafont{chapter}{\color{\chaptercolor}}. Works like a charm. – Johannes_B Sep 25 '14 at 7:32
• When trying to use this with \tableofcontents, I get an error about the color \relax not being defined. Any inputs as to how I might use this with ToC? – Mathias Nielsen Nov 2 '14 at 14:43