# How can one put a marker to every page in a chapter?

I am trying to create some sort of tabbing for a template am designing. I want the title page to have the colors of the chapters and then all of the pages within a chapter to have a marking as shown below. The title page should use the page height and split it with respect to the total number of chapters. The rectangles shown should follow an odd-even page criteria so that the display is proper. Here is a sort of a MWE:

\documentclass[12pt,twoside,letterpaper]{report}

\usepackage[text={4.95in,7.5in},centering,bottom=1.5in,showframe]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usetikzlibrary{calc}

%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot{} % clear all footer fields
\ifodd\thepage
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\else
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north west) rectangle ($(current page.west)+(0.5,9)$);}
\fi
}
\renewcommand{\footrulewidth}{0pt}
}
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
\fancyfoot{} % clear all footer fields
\fancyfoot[RO]{\thepage$|$}
\fancyfoot[LE]{$|$\thepage}
\ifodd\thepage
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\else
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north west) rectangle ($(current page.west)+(0.5,9)$);}
\fi
}
\renewcommand{\footrulewidth}{0pt}

\pagestyle{fancy}

\begin{document}

\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\tikz[remember picture, overlay]{\draw[fill=magenta] ($(current page.north east)+(0,-5)$) rectangle ($(current page.east)+(-0.5,4)$);}}
\author{You}

\maketitle
\newpage
\chapter{Test 1}
%\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\newpage
%\tikz[remember picture, overlay]{\draw[fill=magenta] ($(current page.north east)+(0,-5)$) rectangle ($(current page.east)+(-0.5,4)$);}
\chapter{Test 2}
\lipsum
\chapter{Test 3}
\lipsum
\end{document}


Note that chapter 2 should have the magenta color with the marker shifted down by the right amount. I was thinking with a foreach statement but I don't know how to test if you are still in chapter 1 or 2 and so on. I think the tricky part will be then to set the colors depending on the amount of chapters. So probably having a default say 20 colors to be used in order as they appear (and hence allowing the user to decide what colors to use and for which chapter) for the chapter markers would be good.

-
Related/possible duplicate of: Adding chapter marker to far right edge of page –  Werner May 9 '13 at 0:35
@Werner I had read that post already but it was not too specific. I am looking for a workable example which I can then study, edit and work on. –  azetina May 9 '13 at 0:39

Here's a first version:

## Code

\documentclass{book}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{background}
\usepackage{xifthen}
\usepackage{lipsum}

\backgroundsetup%
{   contents={%
\begin{tikzpicture}[overlay]
\pgfmathsetmacro{\mytop}{-(\thechapter-1)*2.5}
\pgfmathsetmacro{\mybottom}{-\thechapter*2.5}
\ifcase\thechapter
\xdef\mycolor{black}
\or \xdef\mycolor{red}
\or \xdef\mycolor{orange}
\or \xdef\mycolor{yellow}
\or \xdef\mycolor{green}
\or \xdef\mycolor{blue}
\or \xdef\mycolor{violet}
\else \xdef\mycolor{black}
\fi
\fill[\mycolor] ($(current page.north east)+(0,\mytop)$) rectangle ($(current page.north east)+(-0.5,\mybottom)$);
\end{tikzpicture}
},
scale=1,
angle=0
}

\newcommand{\mycolor}%
{   \foreach \x [count=\c] in {blue,violet,red,orange,yellow,green} {\ifthenelse{\c=\thechapter}{\x}{}}
}

\begin{document}

\lipsum

\chapter{First}
\lipsum

\chapter{Second}
\lipsum

\chapter{Third}
\lipsum

\chapter{Fourth}
\lipsum

\chapter{Fifth}
\lipsum

\chapter{Sixth}
\lipsum

\end{document}


## Output

Edit 1: Now checking the total number of chapters and using left/right markers on odd/even pages:

## Code

\documentclass{book}
\usepackage[a4paper,margin=15mm]{geometry}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{lipsum}

\regtotcounter{chapter}

\backgroundsetup%
{   contents={%
\begin{tikzpicture}[overlay]
\pgfmathtruncatemacro{\mytotalchapters}{\totvalue{chapter} > 0 ? \totvalue{chapter} : 20}
\pgfmathsetmacro{\mypaperheight}{\paperheight/28.453}
\pgfmathsetmacro{\mytop}{-(\thechapter-1)/\mytotalchapters*\mypaperheight}
\pgfmathsetmacro{\mybottom}{-\thechapter/\mytotalchapters*\mypaperheight}
\ifcase\thechapter
\xdef\mycolor{white}
\or \xdef\mycolor{red}
\or \xdef\mycolor{orange}
\or \xdef\mycolor{yellow}
\or \xdef\mycolor{green}
\or \xdef\mycolor{blue}
\or \xdef\mycolor{violet}
\else \xdef\mycolor{black}
\fi
\ifthenelse{\isodd{\value{page}}}
{\fill[\mycolor] ($(current page.north east)+(0,\mytop)$) rectangle ($(current page.north east)+(-0.5,\mybottom)$);}
{\fill[\mycolor] ($(current page.north west)+(0,\mytop)$) rectangle ($(current page.north west)+(0.5,\mybottom)$);}
\end{tikzpicture}
},
scale=1,
angle=0
}

\begin{document}
Total chapters: \total{chapter}

Paper height: \pgfmathsetmacro{\yxc}{\paperheight/28.453}\yxc cm

Paper width: \pgfmathsetmacro{\cxy}{\paperwidth/28.453}\cxy cm

\lipsum

\chapter{First}
\lipsum

\chapter{Second}
\lipsum

\chapter{Third}
\lipsum

\chapter{Fourth}
\lipsum

\chapter{Fifth}
\lipsum

\chapter{Sixth}
\lipsum

\end{document}


## Output

-
Really nice. No need to explicitly load tikz; background loads it internally. –  Gonzalo Medina May 9 '13 at 3:01
Thanks, I keep forgetting that, and TikZ is always loaded in my standard template ;) –  Tom Bombadil May 9 '13 at 11:42
@TomBombadil Thanks, I will study your code in a few and then will get you if I have any questions. –  azetina May 9 '13 at 15:23