[Tex/LaTex] Show current chapter number on each page margin

book-designchaptersmarginsthumb-index

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?

Best Answer

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
}

\newcommand\AddLabels{%
\Materialtrue%
\AddEverypageHook{%
\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}
\AddLabels
\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}

enter image description here

Related Question