# Add a black box to the page margin of a few pages

I have a latex document of class book, with several chapters and a table of content. In the end, I add still a few pdf files using the includepdf command. The tricky part, however, is now to add still a black box with 'I' respective 'II' written in it, depending on the running index of the pdf. For the first pdf the black box should be in the upper right corner and only on the odd pages, for the second pdf the box should be under the first one and also only on the odd pages. What I have so far is

\documentclass[b5paper]{book}
\usepackage[final]{pdfpages}
\usepackage{tikz}
\usepackage{ifthen}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\chapter{First Chapter}
\lipsum \lipsum

\newpage
\ifodd\value{page}
\begin{tikzpicture}[remember picture,overlay]
\node[minimum width=2cm, minimum height=2cm, font=\Huge, rotate=90,
rectangle, opacity=1, fill=black, text=white]
at (current page.north east) {I};
\end{tikzpicture}
}
\fi

% Some pdf with several pages
%\includepdf[pages=-,pagecommand={},width=\textwidth]{1.pdf}
\lipsum

\newpage
\ifodd\value{page}
\begin{tikzpicture}[remember picture,overlay]
\node[minimum width=2cm, minimum height=2cm, font=\Huge, rotate=90,
rectangle, opacity=1, fill=black, text=white]
at (current page.north east) {II};
\end{tikzpicture}
}
\fi
%\includepdf[pages=-,pagecommand={},width=\textwidth]{2.pdf}
\lipsum
\end{document}


(I commented the includepdf parts out, as I cannot add here any for a running example, hence some libpsum text instead...)

My problems are

• that the ifodd command is not recognized
• how to tell Latex to put the tikz figure only for those pages pages that are created with the next inputpdf command
• how to add an offset to the at location part in the tikz figure.

EDIT: Just for completeness, this is the fully functional block how I use it now (after accepting the answer) in front of each inputpdf and there I just adjust the coordinates by hand.

\AddEverypageHook{
\ifnum\value{page}>120
\ifnum\value{page}<137
{\ifodd\value{page}
\begin{tikzpicture}[remember picture,overlay]
\node[minimum width=2cm, minimum height=2cm, font=\Huge, rotate=90,
rectangle, opacity=1, fill=black, text=white]
at (14.2,-2) {II};
\end{tikzpicture}
\fi}
\fi
\fi}

• What sort of offset? With the calc tikzlibrary you can use, for example, ($(\current page.center)+(1,1)$). With the tikzpagenodes package you can access, for example, (current page margin area.north west). Dec 3 '16 at 20:54
• With the offset I mean that by using current page.north east the black box is too far in the corner, so I would need to move it slightly to the left and down. Unfortunately the calc library seems to collide with the inputpdf library and throws an error. Maybe the other option is to use only coordinates and figure it out by hand, where to place it. Dec 4 '16 at 7:36

Here is how to use everypage:

\documentclass[b5paper]{book}
\usepackage[final]{pdfpages}
\usepackage{tikz}
\usepackage{everypage}
\usepackage{lipsum}% MWE only

\begin{tikzpicture}[remember picture,overlay]
\node[minimum width=2cm, minimum height=2cm, font=\Huge, rotate=90,
rectangle, opacity=1, fill=black, text=white, above left=\topmargin]
at (current page.north east) {I};
\end{tikzpicture}
\fi}
\begin{document}

\tableofcontents

\chapter{First Chapter}
\lipsum \lipsum[1-10]

\end{document}


As for selecting certain pages instead of just odd ones, the key is to put the info into the aux file. This can be done using custom macros (see sideways figure) or by using \refstepcounter, \label and \getpagerefnumber.

• Thanks, now it works that they only appear on odd page numbers. However, I have to find out still about how to use it only on certain pages. Do you know by chance if there is a way like ifodd to tell that Latex should do it just 15 times (the number of occurances is fixed). I'll have a look to your other references and see how to make use of it. Dec 4 '16 at 7:39
• With the page numbers I figured out as well now, I just place two more if-clauses within the AddEveryPageHook like this: \ifnum\value{page}>120 \ifnum\value{page}<140 ... \fi \fi. And then I had such a block in front of each inputpdfand it worked. Thanks! Dec 4 '16 at 8:05