# Automatically Center Text in Chapter Thumb Box

This code is building from the post found here.

In my MWE given below:

\documentclass{book}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage{ifthen}
\usepackage{lipsum}

\pagestyle{plain}

% background common settings
\SetBgScale{1}
\SetBgAngle{0}
\SetBgOpacity{1}
\SetBgContents{}

% auxiliary counter
\newcounter{chapshift}

% the list of colors to be used (add more if needed)
\newcommand\BoxColor{%
\ifcase\thechapshift blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi}

% the main command; the mandatory argument sets the color of the vertical box
\makeatletter
\newcommand\ChapFrame{%
\ifthenelse{\isodd{\thepage}}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
text height=4cm,align=center,anchor=north east]
at ($(current page.north east) + (-0cm,-2*\thechapshift cm)$)
{\rotatebox{90}{\hspace*{.8cm}\parbox[c][.3cm][t]{3.4cm}{%
\raggedright\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}}%
}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
text height=4cm,align=center,anchor=north west]
at ($(current page.north west) + (-0cm,-2*\thechapshift cm)$)
{\rotatebox{90}{\hspace*{.8cm}\parbox[c][.3cm][t]{3.4cm}{%
\raggedright\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}}
}
\bg@material}%
\stepcounter{chapshift}
}
\makeatother

% redefinition of \chaptermark to contain only the title
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}}

%======================================================================================
%======================================================================================

\usepackage{etoolbox,fancyhdr} % Required for header and footer configuration

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\sffamily\normalsize\bfseries \ #1}{}} % Chapter text font settings
\renewcommand{\sectionmark}[1]{\markright{\sffamily\normalsize\thesection\hspace{5pt}#1}{}} % Section text font settings
\fancyhf{} \fancyhead[LE,RO]{\sffamily\normalsize\thepage} % Font setting for the page number in the header
\fancyhead[LO]{\rightmark} % Print the nearest section name on the left side of odd pages
\fancyhead[RE]{\leftmark} % Print the current chapter name on the right side of even pages
\newcommand{\footrulecolor}[1]{\patchcmd{\footrule}{\hrule}{\color{#1}\hrule}{}{}}
\renewcommand{\footrulewidth}{0pt} % Removes the rule in the footer

% Removes the header from odd empty pages at the end of chapters
\makeatletter
\renewcommand{\cleardoublepage}{
\clearpage\ifodd\c@page\else
\hbox{}
\vspace*{\fill}
\thispagestyle{empty}
\newpage
\fi}

\begin{document}

\chapter[Intro]{Introduction}
\ChapFrame
\lipsum[1-7]
\section{This is how we do it}
\lipsum[1-7]

\chapter{Results}
\ChapFrame
\lipsum[1-7]

\chapter{Some Sample Code}
\ChapFrame
\lipsum[1-7]

\end{document}


Can you tell me if it is possible to automatically center the text in the colored boxes in the chapter thumbs?

Also, how to get the code to function in such a manner that the chapter thumb boxes and text only show up on the right side of the pages instead of alternating?

To center the text in the thumb box, just modify the following code snippet pertaining to the \SetBgContents command:

{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
text height=4cm,align=center,anchor=north east]
at ($(current page.north east) + (-0cm,-2*\thechapshift cm)$)
{\rotatebox{90}{\parbox{4cm}{%
\centering\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}}%
}
{\SetBgContents{%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
text height=4cm,align=center,anchor=north west]
at ($(current page.north west) + (-0cm,-2*\thechapshift cm)$)
{\rotatebox{90}{\parbox{4cm}{%
\centering\textcolor{black}{\scshape\leftmark}}}};
\end{tikzpicture}}
}


As you see, the modified parts are:

1. \hspace*{.8cm} should be removed.
2. The \raggedright alignment should be changed to \centering.
3. \parbox[c][.3cm][t]{3.4cm} should be changed to \parbox{4cm} because you already set the text width (mentioned as text height before rotation) to 4cm in node options [..., text height=4cm, ...].

• Hello, just one more modification...how to get the chapter thumbs to always show up on the right-hand side of the pages instead of alternating like above? – Joe Aug 24 '15 at 3:42
• @Joe The default for a book class is twoside, so for odd pages the thumb is on the left and for even pages it is on the right (i.e. always on the outer margin). If you need the chapter thumb to be on the right-hand side, you should use \documentclass[oneside]{book} and only the first definition of \SetBgContents is required. – AboAmmar Aug 24 '15 at 16:19
• Thanks. I have having one more issue. I would like the chapter thumb to not be present in the first page of the chapter as I have an image on the first page of the chapter. Is this possible? – Joe Aug 24 '15 at 17:20