5

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}

\clearscrheadfoot 
\pagestyle{scrheadings}
\ihead[\color{\chaptercolor}\leftmark]{\color{\chaptercolor}\leftmark} 
\ohead[\color{\chaptercolor}\rightmark]{\color{\chaptercolor}\rightmark}
\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!

  • 1
    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
  • 1
    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
2

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
\clearscrheadfoot 
\pagestyle{scrheadings}
\addtokomafont{pageheadfoot}{\color{\chaptercolor}}
\addtokomafont{pagenumber}{\color{\chaptercolor}}
\ihead[\leftmark]{\leftmark} 
\ohead[\rightmark]{\rightmark}
\cfoot[\pagemark]{\pagemark}

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

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

enter image description here

6

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}

\clearscrheadfoot 
\ihead[\leftmark]{\leftmark} 
\ohead[\rightmark]{\rightmark}
\cfoot[\pagemark]{\pagemark}
%Now adding color to all elements in the pagehead
%This is much easier to maintain
\addtokomafont{pagehead}{\color{\chaptercolor}}

\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]%
        {\paperwidth}{\headheight}%
    }%
]{plain.scrheadings.head.oneside.background}
\DeclareLayer[clone=scrheadings.head.oneside,
    contents={%
        \color{\chaptercolor}%
        \rule[-\dp\strutbox]%
        {\paperwidth}{\headheight}%
    }%
]{scrheadings.head.oneside.background}

Resulting in the follwing output:

enter image description here

Another thing that might be worth a look gives this output

enter image description here

This can be achieved using the following code.

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


\clearscrheadfoot 
\ihead[\leftmark]{\leftmark} 
\ohead[\rightmark]{\rightmark}
\cfoot[\pagemark]{\pagemark}


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

%Adding the Layer to the pagestyles
\AddLayersAtBeginOfPageStyle{scrheadings}{%
    scrheadings.head.background,%
}
\AddLayersAtBeginOfPageStyle{plain.scrheadings}{%
scrheadings.head.background}

\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}
\addtokomafont{chapter}{\color{\chaptertitlecolor}}

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:

enter image description here

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

enter image description here

Please Remember

Colours can be very distracting and disturb the readability. Printing coloured pages is also quite expensive.

  • Thanks for this answer. The thing is, the color is also used in the title, so I need to set the color before the chapter command is called. Of course I could set the color twice, first for the chapter, then set the chapter and the for the headings, but I thought there must be a nice solution for that. – vanto Sep 24 '14 at 17:54
  • The question as is answered i think. You have two possibilities now: 1) extend the question 2) create a new question. Either way, there has to be something to understand what is going on; i.e. a minimal working example (MWE) Personally, i would prefer the second, meaning a new question. – Johannes_B Sep 24 '14 at 19:43
  • 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
  • Thanks Johannes for the suggestions and the efforts you put into MWEs and screenshots. Although it didn't directly solve my problem, it is for sure very helpful for others stumbling upon this post. Appreciated. BTW, I believe that comments about the distractiveness and expensiveness of colors are neither helpful nor appropriate in this context. – vanto Sep 25 '14 at 21:49
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.