# Graphics in every side page margin depending on odd/even

I want something working like a fancyhdr, but on the page margin: A colored box overlayed with the document title (or any other text) on every page, placed vertically inside the page margin. The box should be placed on the outer margin (right on odd, left on even pages) and should be easily enabled/disabled (e.g. using fancy styles). An example is the blue box to the left in the below image for an even page:

On an odd page, I would like to have the same box/text, but rotated 180 °:

My concrete example is a blue box, but a general solution that would allow graphics as well is also higly appreciated!

• This question, and Ulrike's answer, might be of help: tex.stackexchange.com/q/568846/158639
– chsk
Apr 16 at 6:48
• You can use fancyhdr and put the image in the outside section of the page head in a \begin{picture}(0,0)\put... so it takes no space in teh head and is offset to whatever coordinate you use with \put Apr 16 at 7:43

This uses \AddToHook{shipout/background}, which is relatively new and built into LaTeX.

Note that at shipout \value{page} is reliable. You only need things like changepage or ifoddpage when using floats or the last paragraph on a page (well, any paragraph that might wind up last).

\documentclass[a4paper]{article}

\usepackage{lipsum}
\usepackage{tikz}
\usepackage{xcolor}

\definecolor{MyBlue}{HTML}{0053A6}

\newcommand\ColorMargin{%
\ifodd\value{page}%
\put (1 cm,-1cm){% relative to upper left corner
\begin{tikzpicture}[baseline=(current bounding box.north)]
\node[rotate=90, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}%
\else
\put (\paperwidth - 2 cm,-1cm){%
\begin{tikzpicture}[baseline=(current bounding box.north)]
\node[rotate=270, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}%
\fi
}

\begin{document}

\sloppy\lipsum[1-50]\lipsum[1-50]
\RemoveFromHook{shipout/background}[myhook]
\lipsum[1-50]

\end{document}

• Could you please explain shortly, what the differences to my answer are? Apr 19 at 7:51
• Fewer packages. Your answer is fine. I gave it a +1. Apr 19 at 13:26
• Well, fewer packages is always highly appreciated! Apr 21 at 16:07

The comments to my question lead me to a lot of different answers. I compiled an MWE from them using eso-picture:

\documentclass{article}[a4paper]

\usepackage{lipsum}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{changepage}
\strictpagecheck
\usepackage{xcolor}
\usepackage{eso-pic}

\definecolor{MyBlue}{HTML}{0053A6}

\newcommand\ColorMargin{%
\checkoddpage
\ifoddpage
\put (1 cm,480){
\begin{tikzpicture}
\node[rotate=90, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}
\else
\put (\paperwidth - 2 cm,480){
\begin{tikzpicture}
\node[rotate=270, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}
\fi
}