How can I create a thin box box with the numbering of the current chapter on (for normal text page styles)? in the vertical middle of each even page e.g. a thin stripe at the right end of the page like this
-
Welcome to TeX.SX! Related: Wrap page number in colored box in footer or Formatting page number (fancyhdr?)– BobyandbobCommented Oct 27, 2017 at 14:14
-
Thanks for your comment! It's not too far from what I am looking for! But it's not in the vertical middle of the page (I updated my question) and I only want it on eve pages.– StückeCommented Oct 27, 2017 at 14:27
2 Answers
You can place random content on a page during the page shipout routine via eso-pic
.
The example below inserts the chapter number (represented by \thechapter
) vertically centred on the right side of the page. An extra conditional \showchapterthumb<TF>
turns on the showing of the chapter number thumbs if set to \..true
. The default is \..false
.
\documentclass{report}
\usepackage{lipsum}
\usepackage{eso-pic,xcolor}
\definecolor{chapterthumbbg}{HTML}{6F7175}
\AddToShipoutPictureFG{%
\AtPageLowerLeft{%
\ifshowchapterthumb\ifodd\value{page}\else
\makebox[\paperwidth][r]{%
\raisebox{\dimexpr.5\paperheight-.5\height}{%
\setlength{\fboxsep}{\baselineskip}%
\colorbox{chapterthumbbg}{\textcolor{white}{\thechapter}}%
}%
}%
\fi\fi
}
}
\newif\ifshowchapterthumb
\sloppy% Just for this document
\begin{document}
\showchapterthumbfalse% Default
\tableofcontents
\clearpage
\showchapterthumbtrue% Show chapter thumbs
\chapter{First chapter}\lipsum[1-5]
\section{First section}\lipsum[6-10]
\section{Second section}\lipsum[11-15]
\section{Third section}\lipsum[16-20]
\section{Last section}\lipsum[21-25]
\chapter{Second chapter}\lipsum[1-5]
\section{First section}\lipsum[6-10]
\section{Second section}\lipsum[11-15]
\section{Third section}\lipsum[16-20]
\section{Last section}\lipsum[21-25]
\chapter{Third chapter}\lipsum[1-5]
\section{First section}\lipsum[6-10]
\section{Second section}\lipsum[11-15]
\section{Third section}\lipsum[16-20]
\section{Last section}\lipsum[21-25]
\chapter{Last chapter}\lipsum[1-5]
\section{First section}\lipsum[6-10]
\section{Second section}\lipsum[11-15]
\section{Third section}\lipsum[16-20]
\section{Last section}\lipsum[21-25]
\end{document}
Background page numbers have been added for visual purposes only.
-
... can we move the box slightly more to the right ... all the way to the left edge of the page? now there are a few millimeters in between :)– StückeCommented Oct 29, 2017 at 19:37
-
@Jonas: I accidentally forgot to place a specific
%
within the definition. I've corrected that in the example code above.– Werner ♦Commented Oct 30, 2017 at 1:01 -
-
Something like this? It's easy with titleps
, which comes with titlesec
:
\documentclass[12pt, twoside]{book}%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[svgnames]{xcolor}
\usepackage[showframe, nomarginpar]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}
\newpagestyle{mine}{%
\widenhead[2em][0em]{0em}{0em}
\sethead[\smash{\colorbox{SlateGrey!70}{\rule[-1.5ex]{0pt}{5ex}\sffamily\Large\bfseries\thechapter}}][][]{}{}{}
\setfoot{}{}{}
}
\pagestyle{mine}
\begin{document}
\chapter{introduction}
\lipsum[1-8]
\end{document}
A variant: define page style as
\newpagestyle{thine}{%
\widenhead[2em][0em]{0em}{0em}
\sethead[\raisebox{-\dimexpr\headsep+\headheight+2ex\relax} {\colorbox{SlateGrey!70}{\rule[-1.5ex]{0pt}{5ex}\sffamily\Large\bfseries\thechapter}}][][]{}{}{}
\setfoot{}{}{}
}
Edit: I think I've understood what you want. Does this code yield what you want?
\newpagestyle{midpage}{%
\widenhead[2em][0em]{0em}{0em}
\sethead[\raisebox{-\dimexpr\headsep+\headheight+0.5\textheight\relax}{\colorbox{SlateGrey!70}{\parbox{1.4em}{\centering\rule[-2.8ex]{0pt}{8ex}\sffamily\Large\bfseries\thechapter}}}][][]{}{}{}
\setfoot{}{}{}
}
\pagestyle{midpage}