[Tex/LaTex] Complete 3-part header from fancyhdr inside a single colored box

colorfancyhdr

There're other previous questions about using a color background with fancyhdr so that you get a colored header and/or footer. However, they discuss either using a different color box for each part of the header, or working with a single part header (i.e: a header with just the center part, for example).

But what if I have a complete header with three parts (left, center, and right), and I want all the three parts inside a single colored box?

Ideally, the colored box should have no margins, but the three header parts should keep their original margins, so I guess I cannot use the fancyhdr margin parameters for that.

I really need to use fancyhdr for the header. I can add more packages if necessary, but the header must still be created with fancyhdr.

Best Answer

Unfortunaly there is no MWE in the question. So here is only a suggestion that works for a simple three part header using package fancyhdr.

\documentclass{book}
\usepackage{xcolor}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LE]{%
  \strut\rlap{\color{blue!20}\rule[-\dp\strutbox]{\headwidth}{\headheight}}%
  Left part (even page)}
\fancyhead[LO]{%
  \strut\rlap{\color{blue!20}\rule[-\dp\strutbox]{\headwidth}{\headheight}}%
  Left part (odd page)}
\fancyhead[C]{Middle part}
\fancyhead[R]{Right part}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

enter image description here


\documentclass{book}
\usepackage{xcolor}

\usepackage[
    headsepline,plainheadsepline
  ]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{Inner part}
\chead{Middle part}
\ohead{Outer part}
\setkomafont{pagehead}{\upshape}

\DeclareNewLayer[
  background,
  head,
  contents={\color{blue!20}\rule[-\dp\strutbox]{\layerwidth}{\layerheight}}
]{head.bg}
\AddLayersAtBeginOfPageStyle{scrheadings}{head.bg}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

Or starting with KOMA-Script version 3.19

\documentclass{book}
\usepackage{xcolor}

\usepackage[
    headsepline,plainheadsepline
  ]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{Inner part}
\chead{Middle part}
\ohead{Outer part}
\setkomafont{pagehead}{\upshape}

\DeclareNewLayer[
  background,
  head,
  mode=picture,
  contents={\putLL{\color{blue!20}\rule{\layerwidth}{\layerheight}}}
]{head.bg}
\AddLayersAtBeginOfPageStyle{scrheadings}{head.bg}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

With scrlayer-scrpage it is also easy to color the top margin including the header.

\documentclass{book}
\usepackage{xcolor}

\usepackage[
    %headsepline,plainheadsepline
  ]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{Inner part}
\chead{Middle part}
\ohead{Outer part}
\setkomafont{pagehead}{\upshape}

\DeclareNewLayer[
  background,
  topmargin,
  addheight=\headheight,
  contents={\color{blue!20}\rule{\layerwidth}{\layerheight}}
]{head.bg}
\AddLayersAtBeginOfPageStyle{scrheadings}{head.bg}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

enter image description here

If you want to use fancyhdr instead you can draw the background image using TikZ with the remember picture,ovelay options. So you have to run the code twice to get the rectangle on the right position.

\documentclass{book}
\usepackage{tikz}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand\headrulewidth{0pt}
\fancyhead[LE]{%
  \tikz[remember picture,overlay,baseline]
    \fill[blue!20](current page.north west|-0,-\dp\strutbox)
    rectangle(current page.north east);%
  Left part (even page)}
\fancyhead[LO]{%
  \tikz[remember picture,overlay,baseline]
    \fill[blue!20](current page.north west|-0,-\dp\strutbox)
    rectangle(current page.north east);%
  Left part (odd page)}
\fancyhead[C]{Middle part}
\fancyhead[R]{Right part}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document} 
Related Question