# Coloured chapter page markers

I've been experimenting with coloured swatches around the page number to indicate the chapter using KOMA and TikZ; each chapter also has an em-dash above the text in the same colour as a stylistic flair. The colour is defined prior to each chapter using \chaptercolor{colorname}, where \chaptercolor is defined as \newcommand{\chaptercolor}[1]{\colorlet{chaptercolor}{#1}}.

The \chaptercolor command has to be called before the chapter to set the line swatch, but this results in the colour swatch for the previous page also changing. I also tried creating a second \swatchcolor command to change the swatches separately to the chapter line mark, and that results in the page before a new chapter getting whatever the default colour for the swatch is.

How can I avoid this?

I've tried various combinations of KOMA options (e.g. cleardoublepage, open) and none of them seem to have the effect I'm after. Not even adding \clearpage before \colorlet in the definition of \chaptercolor seems to work. Here's a MWE that almost works.

\documentclass{scrbook}
\usepackage{scrlayer-scrpage}

%% KOMA options
\KOMAoption{chapterprefix}{true}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{11pt}
\KOMAoption{open}{any}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{twoside}{semi}
\KOMAoption{titlepage}{false}

%% Subfiles for shared preambles
\usepackage[math]{blindtext}

%% Colours - put this before typography so additional named colours can be defined.
\usepackage[table]{xcolor}
\colorlet{darkblue}{blue!40!black}
\usepackage{tikz}
\tikzset{%
small wedge/.pic={
\fill (0, 0) -- ++(-1.2, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- cycle;
},
}

\setkomafont{pagenumber}{\small\color{darkblue}\selectfont}

% Chapters are now "Tutorials"
% Chapters have an em-dash that is \chaptercolor
\newcommand{\chaptercolor}[1]{\colorlet{chaptercolor}{#1}}
\chaptercolor{white}
\renewcommand*{\chapapp}{Tutorial}
\renewcommand\chapterformat{{\fontsize{50}{50}\upshape\selectfont\textcolor{chaptercolor}{—}\par\nobreak\chapapp~\thechapter}}

%% Use this to set up the headers and footers
\DeclareNewLayer[
bottommargin,
background,
evenpage,
mode=picture,
contents={%
\putLR{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape] at (0,0) {small wedge};
\node[align=right] at (-10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.even}
\DeclareNewLayer[
bottommargin,
background,
oddpage,
mode=picture,
contents={%
\putLL{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape, xscale=-1] at (0,0) {small wedge};
\node[align=left] at (10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.odd}

\DeclareGraphicsExtensions{.pdf,.png,.jpg}

\subject{Test subject}
\title{Testing of page number colors}

\makeatletter
\let\thetitle\@title
\let\theauthor\@author
\let\thedate\@date
\makeatother

\begin{document}

\frontmatter
\maketitle

\tableofcontents

\mainmatter
\setcounter{secnumdepth}{1}

\chaptercolor{cyan!50}
\chapter{The first chapter of this work}

\blindtext
\blindmathpaper
\blindtext

\chaptercolor{magenta!50}
\chapter{Another chapter to test this thing}

\blindmathpaper
\Blinditemize
\blindtext[10]

\chaptercolor{orange!50}
\chapter{Another chapter to test this thing}

\blindmathpaper
\Blindenumerate
\blindtext[12]

\chaptercolor{green!50}
\chapter{Another chapter to test this thing}

\blindmathpaper
\Blinddescription
\blindtext[20]

\end{document}


I've based parts of my MWE on other questions, but I haven't found or figured out a solution that does exactly what I'm after. Upon further reflection, I like the idea of being able to set both colours separately, but the important thing is that the colour change must only take effect after a new chapter.

Now \chaptercolor defines a newchaptercolor that will become chaptercolor when \chapter is executed, which in turn is used for everything. This color change gets broadcasted with \globalcolorstrue.

\documentclass{scrbook}
\usepackage{scrlayer-scrpage}
%% KOMA options
\KOMAoption{chapterprefix}{true}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{11pt}
\KOMAoption{open}{any}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{twoside}{semi}
\KOMAoption{titlepage}{false}

%% Subfiles for shared preambles
\usepackage[math]{blindtext}

%% Colours - put this before typography so additional named colours can be defined.
\usepackage[table]{xcolor}
\colorlet{darkblue}{blue!40!black}
\usepackage{tikz}
\tikzset{%
small wedge/.pic={
\fill (0, 0) -- ++(-1.2, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- cycle;
},
}

\setkomafont{pagenumber}{\small\color{darkblue}\selectfont}

% Chapters are now "Tutorials"
% Chapters have an em-dash that is \chaptercolor
\newcommand{\chaptercolor}[1]{\colorlet{newchaptercolor}{#1}}
\chaptercolor{white}\colorlet{chaptercolor}{white}
\renewcommand*{\chapapp}{Tutorial}
\renewcommand\chapterformat{{\fontsize{50}{50}\upshape\selectfont%
\globalcolorstrue\colorlet{chaptercolor}{newchaptercolor}\textcolor{chaptercolor}{-}\par\nobreak\chapapp~\thechapter}}

%% Use this to set up the headers and footers
\DeclareNewLayer[
bottommargin,
background,
evenpage,
mode=picture,
contents={%
\putLR{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape] at (0,0) {small wedge};
\node[align=right] at (-10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.even}
\DeclareNewLayer[
bottommargin,
background,
oddpage,
mode=picture,
contents={%
\putLL{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape, xscale=-1] at (0,0) {small wedge};
\node[align=left] at (10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.odd}

\DeclareGraphicsExtensions{.pdf,.png,.jpg}

\subject{Test subject}
\title{Testing of page number colors}

\makeatletter
\let\thetitle\@title
\let\theauthor\@author
\let\thedate\@date
\makeatother

\begin{document}

\frontmatter
\maketitle

\tableofcontents

\mainmatter
\setcounter{secnumdepth}{1}

\chaptercolor{cyan!50}
\chapter{The first chapter of this work}
\blindtext
\blindmathpaper
\blindtext

\chaptercolor{magenta!50}
\chapter{Another chapter to test this thing}

\blindmathpaper
\Blinditemize
\blindtext[10]

\chaptercolor{orange!50}
\chapter{Another chapter to test this thing}

\blindmathpaper
\Blindenumerate
\blindtext[12]

\chaptercolor{green!50}
\chapter{Another chapter to test this thing}

\blindmathpaper
\Blinddescription
\blindtext[20]

\end{document}

• I tried that already. The page before the next chapter still ends up as white for me on my main document, but oddly enough not on this MWE. – Robbie Jan 8 '20 at 6:58
• @Robbie OK, I see. I changed the proposal to really set the color only when \chapter gets executed. – user194703 Jan 8 '20 at 7:19
• Does it matter whether \globalcolorstrue is in the chapter format command or in the preamble? – Robbie Jan 8 '20 at 8:14
• @Robbie It is probably cleaner to have it only in the chapter format. By setting \globalcolorstrue, all such definitions are being made globally available — until the current group ends. (see the xcolor manual on p. 21). If you use it in the preamble, some commands written by others that rely on \colorlet being local may break. – user194703 Jan 8 '20 at 8:17

One way would be to add a \clearpage (or \cleardoublepage) to your \chaptercolor command

 \newcommand{\chaptercolor}[1]{\clearpage\colorlet{chaptercolor}{#1}}


The other to use one of the hooks a current KOMA provides. You will then have to change the initialization code of the color:

\colorlet{chaptercolor}{white} %init
\newcommand{\chaptercolor}[1]{%

• Adding \clearpage doesn't achieve the desired result—it was the first thing I tried. The KOMA hooks aren't documented in the English version of the guide, but that does sound like an alternative approach. – Robbie Jan 8 '20 at 8:50
• \clearpage works fine for me in your example (I tried). – Ulrike Fischer Jan 8 '20 at 9:00
• \clearpage doesn't work when I have two separate colours for the chapter dash and the page number swatch, with two commands to set each colour. It only makes sense to call \clearpage within the chapter command, otherwise you get an extra blank page inserted. – Robbie Jan 8 '20 at 9:06