6

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!

vanto
  • 497
  • 1
    Either use a \clearpage beforehand or swap the colorchange after chapter. – Johannes_B Sep 24 '14 at 08: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 08: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? –  Sep 24 '14 at 09: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
  • ad titlesec: indeed I see a warning. Do you have pointers to a nice way to replace the default KOMA chapters by a custom tikz chapter style? – vanto Sep 24 '14 at 17:59
  • The \chapter definitions tend to be rather complicated, but a complete replacement without all the bells and whistles is not too difficult. You will need a \refstepcounter and \addcontents for the toc Typically one starts chapters on odd pages only. – John Kormylo Sep 24 '14 at 18:16
  • You should be able use content from the following: Variable color for section titles; Color depending on section number – Werner Sep 25 '14 at 21:37

2 Answers2

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.

Johannes_B
  • 24,235
  • 10
  • 93
  • 248
  • 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 07: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 02 '14 at 14:43
  • Yap, the color mechanism relies on the chapternumber, the toc has none. I'll have a look at this and update the answer. It might take a few hours.. – Johannes_B Nov 02 '14 at 14:51
  • @Johannes_B sounds great! It always takes a few hours in LaTeX I find, but the result is just so pretty :) – Mathias Nielsen Nov 02 '14 at 15:49
  • I managed to solve it. Sort of. Just give it a default value (blackfor example) like so: \ifcase\arabic{chapter} black\or blue\or orange\or green\or red\fi%

    Of course this means no fancy coloring of chapter names in the ToC, but at least it prints with no errors.

    – Mathias Nielsen Nov 02 '14 at 19:31
  • There are a few issues here that need to be adressed. Stuff like the toc, lof, bibliography etc. are to help the reader. Introductions are also unnumbered sometimes. My first thought of giving all unnumbered chapters some kind of default color breaks there. If not, it would break with the option listof=totocnumbered and again in the frontmatter. Numbered and unnumbered chapters get mixed out of different reasons. the list of colors being worked on with ifcase can be as long as you want. Package xcolor with option x11names gives you a whole bunch of predefined colors. – Johannes_B Nov 04 '14 at 06:44
  • You can always implement more. It is a little late now and there was other stuff keeping me busy. The initial approach was playing around a bit, i'll keep this in my head and work on it. When the update comes, it will be a bit more robust. Sorry for the inconvenience. – Johannes_B Nov 04 '14 at 06:45
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

esdd
  • 85,675