2

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.

4

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} 
  • Thanks a lot, esdd. The first approach (fancyhdr-based) was what I needed. Would it be possible to make the color box borderless while keeping the header text with its current margins? – user2811963 Oct 1 '15 at 9:26
  • I am sorry but I do not understand what you mean with "borderless". – esdd Oct 1 '15 at 12:49
  • Yes, I didn't explain it right. I meant "margin-less" (ie: the colored background arriving to the paper limits at the right, the left, and the top, but without changing the header margins because I want the text to remain where it is). I guess I can achieve it by modifying the length and height of the rectangle, but I also need to move it to the left limit of the paper, and I don't know how to do that. – user2811963 Oct 1 '15 at 15:40
  • I have expanded my answer. – esdd Oct 1 '15 at 22:14
  • Thanks a lot, esdd. I've however hit a bug that affects tikz when building through DVI while being in landscape mode. I've added the fix proposed at the bottom of that page, but, however it still leaves a white margin at the top of the header. Not too bad because there're no white margins at the left nor at right, just at the top. I'm using '\usepackage[a4paper,landscape]{geometry}' so I don't exactly know why the fix from that page isn't 100% successful for me. – user2811963 Oct 2 '15 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.