# Show current chapter number on each page margin

I'm making a reference manual using scrbook. I'd like to include a "tab" that shows the current chapter number in a 1.5cm gray rectangle on the outside edge of every page. Within the rectangle would be the current chapter number. This would allow readers to quickly locate the beginning of a particular chapter by looking at the edge of the printed book.

As an added challenge, the chapter tab needs to move down the page as the chapter number increases. For example, in chapter 1, the chapter tab would be located 1cm from the top of the page; for chapter 2, it would be located 2.5cm from the top of the page (1cm white space, plus 1.5cm for the height of the previous chapter tab), and so on.

Is this possible? If so, how can do implement it?

Here's a possibility using the background package (change the settings according to your needs). Using \AddLabels the tabs are activated; \RemoveLabels deactivates the tabs:

\documentclass[openany]{scrbook}
\usepackage[contents={},opacity=1,scale=1,color=black]{background}
\usepackage[a6paper]{geometry}% just for the example
\usepackage{tikzpagenodes}
\usepackage{totcount}
\usepackage{lipsum}% just to generate text for the example
\usetikzlibrary{calc}

\newif\ifMaterial

\newlength\LabelSize
\setlength\LabelSize{2.5cm}

\AtBeginDocument{%
\regtotcounter{chapter}
\setlength\LabelSize{\dimexpr\textheight/\totvalue{chapter}\relax}
\ifdim\LabelSize>2.5cm\relax
\global\setlength\LabelSize{2.5cm}
\fi
}

\Materialtrue%
\ifMaterial%
\ifodd\value{page} %
\backgroundsetup{
angle=90,
position={current page.east|-current page text area.north  east},
vshift=15pt,
hshift=-\thechapter*\LabelSize,
contents={%
\tikz\node[fill=gray!30,anchor=west,text width=\LabelSize,
align=center,text height=15pt,text depth=10pt,font=\large\sffamily] {\thechapter};
}%
}
\else
\backgroundsetup{
angle=90,
position={current page.west|-current page text area.north west},
vshift=-15pt,
hshift=-\thechapter*\LabelSize,
contents={%
\tikz\node[fill=gray!30,anchor=west,text width=\LabelSize,
align=center,text height=15pt,text depth=10pt,font=\large\sffamily] {\thechapter};
}%
}
\fi
\BgMaterial%
\else\relax\fi}%
}

\newcommand\RemoveLabels{\Materialfalse}

\begin{document}

\chapter{Test Chapter One}
\lipsum[1-2]
\chapter{Test Chapter Two}
\lipsum[1-2]
\chapter{Test Chapter Three}
\lipsum[1-2]
\chapter{Test Chapter Four}
\lipsum[1-2]
\chapter{Test Chapter Five}
\RemoveLabels
\lipsum[1-2]

\end{document}


• This is fantastic, and very helpful. Thanks! Quick follow-up question: if I add an index to the document, the chapter tab appears on the index pages as well. Is it possible to suppress the chapter tab in the index? – Brent Jun 19 '13 at 5:08
• @Brent sure, it's possible. In some minutes I'll update my answer with this feature added. – Gonzalo Medina Jun 19 '13 at 14:39
• @Brent I've updated my answer. The new code has a command to activate the tabs and another one to deactivate them. – Gonzalo Medina Jun 19 '13 at 15:51
• Although I really like the screenshots you've provided, I get the following error messages for my document: ! Arithmetic overflow. <argument> ...xtheight /\totvalue {chapter}\relax l.119 \begin{document} ? ! Emergency stop. <argument> ...xtheight /\totvalue {chapter}\relax l.119 \begin{document}. How can I fix this? – Martin Thoma Dec 22 '13 at 15:33
• @GonzaloMedina this is a very nice solution, but what to do if I add a bibliography at the end of the document, and if I don't want the label of the previous chapter appears? I use in the documentclass option bibliography=totoc for add its entry at the TOC, but the label is the same. – Iraultza Belaustegigoitia Oct 6 '14 at 19:34

In April 2002, the web site of the Koma project, Markus Kohm himself presents both commented code for chapter-thumbs, complemented by a pdf as output.

According to him, it should work with every class with \chapter{}-environment and is based on scrpage2. While xcolor allows coloured backgrounds, graphicx provides rotation of the entries to be printed. In difference to the previously presented solution by Gonzalo Medina, the one by Markus Kohm displays the mark only on the odd side (the right one).

• I think I started using that code first on my document. But also I have problems with the bibliography label. – Iraultza Belaustegigoitia Oct 6 '14 at 19:36
• There is an updated version of this code using the newer scrlayer-scrpage package in the examples from the KOMA-Script book (use the files in the Anhang-B folder). – Andrew Dunning Jan 24 at 14:48

The following suggestion bases on an example in the appendix of the KOMA-Script book (German):

\documentclass[10pt,twoside,parskip=half]{scrbook} % Document class for PhD manuscript
\usepackage[
paperwidth=170mm,
paperheight=240mm,
top=2.5cm, bottom=3cm,
inner=30mm, outer=25mm,
heightrounded=true
]{geometry}

\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{scrlayer-scrpage}

% the following bases on an example in the KOMA-Script book:
\newcommand*{\firstchapterthumbskip}{.1\paperheight}
\newcommand*{\lastchapterthumbskip}{\firstchapterthumbskip}
\newcommand*{\chapterthumbheight}{2em}
\newcommand*{\chapterthumbwidth}{.1\paperheight}
\newcommand*{\chapterthumbskip}{.1\paperheight}
\newcommand*{\chapterthumbboxcolor}{gray!30}
\newcommand*{\chapterthumbcolor}{black}
\newcommand*{\chapterthumbformat}{\thechapter}
\newkomafont{chapterthumb}{\normalfont\Large\sffamily\color{\chapterthumbcolor}}

\makeatletter
\newcommand*\chapterthumb@box{%
\parbox[c][\chapterthumbheight][c]{\chapterthumbwidth}{%
\centering\usekomafont{chapterthumb}{\chapterthumbformat}%
}%
}
\newcommand*{\chapterthumbbox}{%
\if@mainmatter
\ifnum\value{chapter}>\z@
\ifnum \value{chapterthumb}<\z@
\else
\begingroup
\protected@edef\reserved@a{\chapterthumbformat}%
\ifx\reserved@a\lastchapterthumbformat\else
\stepcounter{chapterthumb}%
\global\let\lastchapterthumbformat\reserved@a
\fi
\@tempcnta=\numexpr
\dimexpr
\paperheight
-\firstchapterthumbskip
-\chapterthumbwidth
-\lastchapterthumbskip
\relax / \dimexpr
\chapterthumbskip
\relax
+1
\relax
\ifnum \value{chapterthumb}<\@tempcnta
\else
\setcounter{chapterthumb}{0}%
\fi
\vspace*{%
\dimexpr
\firstchapterthumbskip
+ ( \chapterthumbskip )
* \value{chapterthumb}%
- \baselineskip
\relax
}\par
\setlength{\fboxsep}{0pt}%
\ifodd\value{page}
\hfill
\makebox[0pt][r]{%
\rotatebox[origin=tr]{90}{%
\colorbox{\chapterthumbboxcolor}{%
\parbox[t][\chapterthumbheight]{\chapterthumbwidth}{%
\chapterthumb@box
\vskip3mm
}}}}%
\else
\hspace*{-3mm}%
\makebox[0pt][l]{%
\rotatebox[origin=tr]{90}{%
\colorbox{\chapterthumbboxcolor}{%
\parbox{\chapterthumbwidth}{%
\vskip3mm
\chapterthumb@box
}}}}%
\fi
\endgroup
\fi
\fi
\fi
}
\makeatother

\newcounter{chapterthumb}
\setcounter{chapterthumb}{10000}
\newcommand*{\lastchapterthumbformat}{\relax}

\DeclareNewLayer[%
background,%
outermargin,%
contents=\chapterthumbbox
]{chapterthumb}

\newcommand*\EnableChapterthumb{%
}
\newcommand*\DisableChapterthumb{%
\RemoveLayersFromPageStyle{@everystyle@}{chapterthumb}%
}

\EnableChapterthumb

\usepackage{blindtext}% only for dummy text
\begin{document}
\tableofcontents
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\end{document}


Result: