2

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:

example for even page

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

enter image description here

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

2
  • 1
    This question, and Ulrike's answer, might be of help: tex.stackexchange.com/q/568846/158639
    – chsk
    Apr 16, 2021 at 6:48
  • 1
    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, 2021 at 7:43

2 Answers 2

1

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
}

\AddToHook{shipout/background}[myhook]{\ColorMargin}

\begin{document}

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

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

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
}

\AddToShipoutPicture{\ColorMargin}

\begin{document}

\sloppy\lipsum[1-50]\lipsum[1-50]
\ClearShipoutPicture
\lipsum[1-50]

\end{document}

which results in output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .