3

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{headings}{normal}
\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{pagehead}{\small\color{darkblue}\selectfont}
\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*{\chapterheadstartvskip}{\vspace*{-\topskip}}
\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}

\AddLayersAtBeginOfPageStyle{scrheadings}{wedge.odd,wedge.even}%
\pagestyle{scrheadings}
\renewcommand*{\chapterpagestyle}{scrheadings}
\clearscrheadfoot
\lohead{This is a test}
\rehead{\leftmark}

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

% % Metadata goes here
\titlehead{Test document}
\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.

Robbie
  • 2,893

2 Answers2

2

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{headings}{normal}
\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{pagehead}{\small\color{darkblue}\selectfont}
\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*{\chapterheadstartvskip}{\vspace*{-\topskip}}
\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}

\AddLayersAtBeginOfPageStyle{scrheadings}{wedge.odd,wedge.even}%
\pagestyle{scrheadings}
\renewcommand*{\chapterpagestyle}{scrheadings}
\clearscrheadfoot
\lohead{This is a test}
\rehead{\leftmark}

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

% % Metadata goes here
\titlehead{Test document}
\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 08 '20 at 06:58
  • @Robbie OK, I see. I changed the proposal to really set the color only when \chapter gets executed. –  Jan 08 '20 at 07:19
  • Does it matter whether \globalcolorstrue is in the chapter format command or in the preamble? – Robbie Jan 08 '20 at 08: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. –  Jan 08 '20 at 08:17
1

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]{%
  \AddtoOneTimeDoHook{heading/postinit/chapter}{\colorlet{chaptercolor}{#1}}}
Ulrike Fischer
  • 327,261
  • 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 08 '20 at 08:50
  • \clearpage works fine for me in your example (I tried). – Ulrike Fischer Jan 08 '20 at 09: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 08 '20 at 09:06
  • It isn't clear from you question that you want different colors. In this case you can simply set the chaptercolor before the \chapter command, and the pagenumbercolor after it. Or use two arguments. – Ulrike Fischer Jan 08 '20 at 09:08
  • @UlrikeFischer This is precisely the first version of my answer. I changed it because the OP was saying it wouldn't work for them. –  Jan 08 '20 at 15:04
  • @Schrödinger'scat Well it works for the MWE. If the MWE is bad and doesn't show the real problem then I don't see what one can do. I won't start to guess around here. – Ulrike Fischer Jan 08 '20 at 17:43
  • Well, yes. All I wanted to say is that this has been proposed before, and the OP says explicitly "Not even adding \clearpage before \colorlet in the definition of \chaptercolor seems to work.", which is why my answer became what it is now. –  Jan 08 '20 at 17:46
  • @Schrödinger'scat ah, I hadn't seen your first version, only the edit. Well your first version was correct and I don't think that you should have changed it without more info from the OP. It is quite confusing for future readers when the answer contains code that isn't needed to get the MWE to work. – Ulrike Fischer Jan 08 '20 at 18:13
  • You have a point. I could add a sentence at the beginning of my answer. Or let it be since you have posted yours. Will leave the decision to you. –  Jan 08 '20 at 18:18
  • 1
    It's also partly my fault as I changed my mind during the testing phase of writing this. I'll update the main question to match it. – Robbie Jan 10 '20 at 04:04