Rotating chapter number in label

I am using blocks on the side of the page to show the chapter number. I am using the example described on this forum (here). However, I would like to turn the chapter numbers by 90 degrees. I tried the angle option at several places, but nothing worked. Here is my MWE:

\documentclass[twoside,openright,numbers=endperiod,titlepage,headinclude,
footinclude=false,cleardoublepage=empty,abstractoff,
BCOR=5mm,b5paper,fontsize=11pt, american,
]{scrreprt}

\usepackage{tikzpagenodes}
\usepackage{totcount}

%makes blocks with chapter number on the side of the page
\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= 10pt,
hshift=-\thechapter*\LabelSize,
contents={%
\tikz\node[fill=gray!50,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=-10pt,
hshift=-\thechapter*\LabelSize,
contents={%
\tikz\node[fill=gray!50,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}

\include{Chapters/Introduction}
\cleardoublepage

\end{document}


I was not able to compile your Minimal Working Example. But I took it from here.

You were right, to rotate the chapter number, you have to play with the angle option. But you also have to change its position (with the two options vshift and hshift).

\documentclass[openany,english]{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
\usepackage{babel}
\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}


I do not really understand how the chapter numbers should be rotated. But here is a suggestion using package scrlayer-scrpage to define a new pair of layer page styles.

\documentclass[
twoside,open=right,
numbers=endperiod,
titlepage,
abstract=off,
BCOR=5mm,
a6paper,
american,
]{scrreprt}
\usepackage{babel}

\newlength\LabelSize
\setlength\LabelSize{2.5cm}
\newcommand\LabelWidth{25pt}
\newcommand\Angle{90}% change the angle here

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

\usepackage{xcolor}
\usepackage{rotating}
\usepackage{scrlayer-scrpage}
\clearpairofpagestyles
\ofoot*{\pagemark}

\DeclareNewLayer[
background,
oddpage,
textarea,
width=\LabelWidth,
height=\LabelSize,
hoffset=\dimexpr\paperwidth-\LabelWidth\relax,
mode=picture,
contents={%
\putLL{\textcolor{lightgray}{\rule{\layerwidth}{\layerheight}}}%
\putC{\makebox[0pt]{\rotatebox[origin=c]{\Angle}{\raisebox{-.5\height}{%
\Large\bfseries\sffamily\thechapter
}}}}%
}
]{chapnumbox.odd}
\DeclareNewLayer[
clone=chapnumbox.odd,
evenpage,
hoffset=0pt
]{chapnumbox.even}

\DeclareNewPageStyleByLayers
[onselect={\DeclarePageStyleAlias{plain}{plain.chapnumbox}}]
{chapnumbox}{chapnumbox.odd,chapnumbox.even}
\DeclareNewPageStyleByLayers
{plain.chapnumbox}{chapnumbox.odd,chapnumbox.even}

\usepackage{lipsum}% just to generate text for the example
\begin{document}

\tableofcontents
\clearpage
\pagestyle{chapnumbox}
\chapter{Chapter One}
\lipsum
\chapter{Chapter Two}
\lipsum
\chapter{Chapter Three}
\lipsum
\chapter{Chapter Four}
\lipsum
\chapter{Chapter Five}
\lipsum
\clearpage
\lipsum[1-2]
\end{document}


Result:

and with

\newcommand\Angle{-90}


or with

\newcommand\Angle{0}


• This works, besides the numbers are not placed in the grey boxes. But then I have to use a different pagestyle (as you defined the chamnumbox), but I have more lay-out things in my pagestyle, which will get lost using this one (like chapter title on top of the page). – Brigitte Bruijns Feb 8 '17 at 15:37
• The new defined pagestyle chapnumbox uses all the layers of pagestyle scrheadings. So you can still use \ohead etc. to put contents in the header or footer. Or do you load a different package for header and footer? If you add your settings to your MWE, I can show how this can be done by package scrlayer-scrpage. – esdd Feb 8 '17 at 16:05
• I have all the lay-out setting in one 'master'file in which the chapters are loaded, so no separate package or file. I am now using the fancy pagestyle, with fancyhead and fancyfoot. – Brigitte Bruijns Feb 10 '17 at 11:21
• If your are using \fancyhead and \fancyfoot and pagestyle fancy then package fancyhdr is used by your code probably. Maybe you can replace this package by scrlayer-scrpage and its commands. But if you want to keep fancyhdr then you can not use my suggestion. – esdd Feb 10 '17 at 12:45

To make your MWE working, you need to add \usepackage[contents={},opacity=1,scale=1,color=black]{background} to your preamble.

In your document, you have to add \AddLabels to activate the code.

This is the complete MWE

\documentclass[twoside,openright,numbers=endperiod,titlepage,headinclude,
footinclude=false,cleardoublepage=empty,abstractoff,
BCOR=5mm,b5paper,fontsize=11pt, american,
]{scrreprt}
\usepackage{lipsum}
\usepackage[contents={},opacity=1,scale=1,color=black]{background}

\usepackage{tikzpagenodes}
\usepackage{totcount}

%makes blocks with chapter number on the side of the page
\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=10pt,
hshift=-\thechapter*\LabelSize,
contents={%
\tikz\node[fill=gray!50,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=-10pt,
hshift=-\thechapter*\LabelSize,
contents={%
\tikz\node[fill=gray!50,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}


• @JohnKormylo: Thanks. I removed the \includes by more verbatim code. – Jan Dec 19 '16 at 16:10