Page header with gray background and without margin

After finishing my thesis in LaTeX (I am an absolute newcomer) I've been asked to format it in a specific design matching a doc-file I received.

I've managed to change titles and fonts and margins successfully, but I'm having trouble with the header.

Here's what I am trying to achieve:

I've tried messing around with fancyhdr - but after spending an hour on the documentation, I'm not sure it gives me what I need.

Can you give me a nudge into the right direction?

Particular questions: - What package should I be using to achieve this? - How do I create this floating box that ignores the page margin?

-
See: tex.stackexchange.com/questions/2288/… -- the commands you need are llap, rlap or makebox –  Marco Daniel Sep 19 '12 at 19:40

The example for the page number uses \llap/\rlap to print something to the left/right without moving the current point.

\documentclass[twoside]{book}
\usepackage[
]{geometry}

\usepackage{color}
\definecolor{bgpagenum}{gray}{.82}

\usepackage{fancyhdr}
\pagestyle{fancy}

\makeatletter
\let\ps@plain\ps@fancy
\makeatother

\fancyfoot{}
\renewcommand*{\footrulewidth}{0pt}

\rightmark
\rlap{%
\hspace{\marginparsep}%
\hspace{-\fboxsep}%
\colorbox{bgpagenum}{%
\makebox[\textwidth][l]{\thepage}%
}%
}%
}%
}
\llap{%
\colorbox{bgpagenum}{%
\makebox[\textwidth][r]{\thepage}%
}%
\hspace{\marginparsep}%
\hspace{-\fboxsep}%
}%
\leftmark
}%
}

% sans serif font as main font
\renewcommand*{\familydefault}{\sfdefault}

\begin{document}
\chapter{Asdfg}
\section{Hjkl}
\newpage
\section{Yuip}
\end{document}


I have used \marginparsep as horizontal distance from the page number to the text body. Then the page number would be vertical aligned with marginal notes, if there are any. If the page number should also be outside the marginal notes, then add \hspace{\marginparwidth}. The length the gray box can be calculated:

\dimexpr\paperwidth-\textwidth-1in-\oddsidemargin\relax % odd pages
\dimexpr\paperwidth-\textwidth-1in-\evensidemargin\relax % even pages


The example simplifies it by using a larger width (\textwidth).

The other things depend on used class/packages and, what have you already managed.

-
Thank you very much, that's a lifesaver! :-) –  tjdecke Sep 19 '12 at 20:10