# Coloured swatch per chapter

I know that this should be possible, but I'm not sure how to go about it.

I would like to have a small rectangle appear on each page (outside edge), which runs right up to the edge of the page, and for it to be in the same place for each page - so that when the book is printed, the colour is visible on the outside edge of the page.

My current thinking is that i could maybe include something in my fancychap which did it for me, but I really don't know a huge amount about latex, so where to go from here has me a little lost...

Doing somthing like this gets be someway there:

%................"Fancy Headers" - Nice page headings
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{calc}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}

\fancypagestyle{plain}{%
}


But all that does is put the colorbox in the corner, outside of the page number. I'd rather it was a few inches down the page (i.e. outside of the header).

This gives this:

which isn't all the way there. The last time i tried something like this it took me hours, and this is a little worse than the previous thing i tried. Can any of you think of a simple(ish) way i could go about this?

I already have this for my titles:

\usepackage[strict]{changepage}

\makeatletter

\usepackage[explicit]{titlesec}
\titleformat
{\chapter} % command
[display] % shape
{\fontsize{76}{80}\usefont{OT1}{pzc}{m}{n}\selectfont}% format
{\vspace*{-135pt}%
\ifodd \c@page \raggedleft \else \raggedright \fi \colorbox{cyan}{\parbox[l][25mm][c]{100mm}{\ifodd \c@page \raggedleft \thechapter \hspace*{20mm} \else \raggedright \hspace*{20mm} \thechapter \fi}}% title code
}% label
{-136pt}% sep
{\bfseries\Huge \ifodd \c@page \raggedleft \else \raggedright \fi%
\ifodd \c@page \raggedleft \else \raggedright \fi  \colorbox{cyan}{\parbox[l][15mm][c]{100mm}{\ifodd \c@page \hspace*{7mm} \raggedright #1 \else \raggedleft #1 \hspace*{7mm} \fi}}%
}

%\titlespacing*{\chapter}{-20mm}{0.5ex}{0.5ex}[-20mm]

\makeatother


which places coloured boxes around the titles and their numbers which extend off the edge of the page. Ideally, the swatches on the other pages would line up with these ones. The bonus of using the header to do it would be that you wouldn't have to add in any special logic to deal with the pages that don't have headers (like the title page, contents, and other pages where the chapter starts and stuff).

Here is another suggestion using scrlayer-scrpage for the header.

\documentclass
%[oneside]
{book}
\usepackage{xcolor}
\usepackage{blindtext}

\usepackage[
automark,
autooneside=false,
markcase=noupper,
]{scrlayer-scrpage}
\clearpairofpagestyles

% define new layers
\DeclareNewLayer[
background,
oddpage,
align=tr,
hoffset=\paperwidth,
voffset=0pt,%distance from top
width=1cm,% width of the swatch
contents={\textcolor{swatchcolor}{\rule{\layerwidth}{\layerheight}}}
]{swatch.odd}
\DeclareNewLayer[
clone=swatch.odd,
evenpage,
align=l,
hoffset=0pt
]{swatch.even}

% add layers to every pagestyle

\newcommand\swatchcolor[1]{\colorlet{swatchcolor}{#1}}
\swatchcolor{white}

\begin{document}
\tableofcontents
\chapter{First chapter}\swatchcolor{orange}
\section{First section}
\Blindtext[20]
\chapter{Second chapter}\swatchcolor{purple}
\section{First section}
\Blindtext[20]
\chapter{Third chapter}\swatchcolor{blue!80!black}
\section{First section}
\Blindtext[20]
\end{document}


It works also if you use an oneside layout. Then the swatch is on every page at the right (=outer) margin automatically.

\documentclass[oneside]{book}


This uses \atxy{This page x}{alt page x}{y}{content} to place something at alternating locations on even/odd pages, and \updateText{content} to change the content. In this case, my content is a .5in x .5in colored box, which I change part-way through the document.

Because my paper is 8.5 in wide, my this page x is 8in, while my alt page x is 0 in (for a 1/2 in box). Likewise, the y value is .5 in.

\documentclass[letter]{article}
\usepackage{everypage}
\usepackage{lipsum,xcolor}
% THESE ARE LaTeX DEFAULTS; CAN CHANGE IF NEEDED.
\def\PageTopMargin{1in}
\def\PageLeftMargin{1in}
\def\localX{0in}
\def\ThisX{0in}
\def\AltX{0in}
\newcommand\atxy[4]{%
\def\XYtext{#4}
\gdef\ThisX{#1}\gdef\AltX{#2}%