# Rectangle as background for header

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.

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.

• If it is as height as the text, it varies when text changes, right? Sep 24 '14 at 13:12
• Yes, but headers only have 1 line of height. Sep 24 '14 at 13:32
• I mean the height of a line depends on its contents. A line like "xxxxxxx" is not as tall as "IIIIIII". Is this what you want? Or you want the rectangle is of height \baselineskip? Sep 24 '14 at 13:36
• I guess what I meant was \baselineskip! Sorry for the confusion. Sep 24 '14 at 13:38

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}
\begin{tikzpicture}[remember picture, overlay]
\fill [blue!50!white] (current page header area.north west) rectangle (current     page header area.south east);
\end{tikzpicture}

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


Renderd document:

• This is crazy good, wow! Just one small problem: when I use the code you've provided in conjunction with a \fancyhead[]{"blatext"} command, the rectangle covers up "blatext" given as argument. Do you know of a way to not let this happen? Sep 24 '14 at 13:36
• I tried to reconstruct your problem by using \fancyhead[l]{above tikz code} and also with [r] instead of \lhead and it didn't expose the problem you describe. It was also possible to use the \lhead as described above with an additional \fancyhead[r]{blatext}. I assume you want to set another header text at a given position (l,c,r). Could you please specify it more precisely. Sep 25 '14 at 5:18