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.

user2811963
  • 87
  • 1
  • 6

1 Answers1

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} 
esdd
  • 85,675
  • 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 01 '15 at 09:26
  • I am sorry but I do not understand what you mean with "borderless". – esdd Oct 01 '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 01 '15 at 15:40
  • I have expanded my answer. – esdd Oct 01 '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 02 '15 at 15:39
  • The scrlayer-scrpage version works for me also through DVI with landscape mode ;-) I do not really understand the fix from your link but I think that the second argument of \pgfqpoint have to be enlarged. – esdd Oct 03 '15 at 13:37
  • Ok, thanks a lot. I might take a look at the scrlayer-scrpage version, but I'd really prefer to stay with fancyhdr. In this moment I even find nice the white top margin. I'd prefer to get rid of it, but mainly because it comes from buggy behavior rather than for pure aesthetic reasons. – user2811963 Oct 03 '15 at 23:10
  • By trial and error, I found that changing the \pgfqpoint number in the fix at that URL, from 11840716sp to 12940716sp achieves the desired result of getting rid of the top margin. Don't ask me why, I just guessed the number by trial and error. – user2811963 Oct 04 '15 at 12:29