[Tex/LaTex] Rectangle as background for header

fancyhdrtikz-pgf

I'm having a horrible problem with fancyhdr and tikz that I was hoping you'd be able to solve for me.

I've tried desperately to get a simple, colored rectangle to show up as background for my headers, much like in the attached picture.enter image description here

However, for the life of me, no matter what I try, I can't seem to get something like this done. I've been able to draw rectangles, sure, but none of them have the following characteristics:

  • Same width as the page's;
  • Same height as the header text.

Can you please help me? I have no idea how to do this, and I have very little knowledge of how tikz and fancyhdr work.

Best Answer

Here an attempt using default fancyhdr and tikzpagenodes. I didn't check if it's working with KOMA too. If you need additional help just add a comment.

Till now i didn't got it working for forced line-breaks (\\) in the header. Perhaps you find a solution of adding support for this on your own. I also did not have a closer look at the tikzpagenodes documentation. Should be worth a try.

\documentclass[11pt]{article}
\usepackage{tikzpagenodes}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{ %
 \begin{tikzpicture}[remember picture, overlay]
  \fill [blue!50!white] (current page header area.north west) rectangle (current     page header area.south east);
 \end{tikzpicture}
 This is my colored header}

\begin{document}
\section{First section}
The first page...
\newpage
\section{Second section}
This is a second page...
\end{document}

Renderd document:

page1 page2