3

I wanted to know whether there is a way to set the background color of my header. Here is my code so far:

\usepackage{fancyhdr}
\usepackage{lastpage}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\textsl{Title}}
\fancyhead[R]{\textsl{Page \thepage{} of \pageref{LastPage}}}
\renewcommand{\headrulewidth}{0pt}
%\renewcommand{\footrulewidth}{0pt}
3

2 Answers 2

4

enter image description here

\documentclass{article}



\usepackage{fancyhdr,color}
\usepackage{lastpage}


\pagestyle{fancy}
\fancyhf{}
\setlength\headheight{17pt}
\lhead{\textcolor{red}{\rule[-4pt]{\textwidth}{15pt}}%
\hspace{-\textwidth}%
\textcolor{white}{\textsl{Title}}}
\rhead{\textcolor{white}{\textsl{Page \thepage{} of \pageref{LastPage}}}}

\begin{document}
zz
\end{document}
3
  • This somehow shifts "Title" a bit to the right, but the "Page of..." is completely on the left (and because of the italics a bit of the last character is outside the box)
    – weisbrja
    Dec 31, 2020 at 16:42
  • Okay I found out why it shifted the "Title": I didn't have the code all in one line. But, I could'nt figure out, how to move the "Page of..." more to the right.
    – weisbrja
    Dec 31, 2020 at 16:52
  • Play with phantom. \rhead{\textcolor{white}{\textsl{\Page \thepage{} of \pageref{LastPage}\phantom{xx}}}} will move the right header to the left to deal with the italics. Dec 31, 2020 at 16:59
4

You can set the header in a \colorbox in the centre only, and then spread the contents to the left/right/centre as needed.

enter image description here

\documentclass{article}

\usepackage{fancyhdr,xcolor}
\usepackage{lastpage,lipsum}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{%
  \setlength{\fboxsep}{0pt}% Remove \fbox gap
  \colorbox{blue!15!white}{%
    \makebox[\textwidth]{%
      \rlap{\textsl{Title}}% Left header
      \hfill
      \clap{}% Center header
      \hfill
      \llap{\textsl{Page \thepage\ of \pageref{LastPage}}}% Right header
    }% \makebox
  }% \colorbox
}% \fancyhead
\renewcommand{\headrulewidth}{0pt}

\begin{document}

\sloppy\lipsum[1-50]

\end{document}

All elements are placed with zero width (via \rlap for the left header, \clap for the centre and \llap for the right header) so as to not affect its placement relative to their width.

7
  • What exactly is a fbox?
    – weisbrja
    Dec 31, 2020 at 19:00
  • I like this solution more, but I have one problem with it. How do I make the box taller?
    – weisbrja
    Dec 31, 2020 at 19:05
  • @Brain: It's shorthand for a framed box that has a rule around it (of width \fboxrule) and a gap of \fboxsep between the rule and the box content. There is a more elaborate \framebox command as well that does the same thing with a set of optional arguments for the content width and alignment (similar to \makebox).
    – Werner
    Dec 31, 2020 at 19:07
  • @Brain: It depends. If you just want a little more room, add a \strut just before \rlap. If you want it much taller, then one can insert a strut of your liking, but you'll also have to change \headheight. Removing the \fboxsep (setting it to 0pt in my answer) makes the box tight against the content. One could use the default \fboxsep but, again, it may require a change in \headheight as well. You'll receive a warning to that effect in the .log. It can be circumvented, but you'll have to be specific about your needs though.
    – Werner
    Dec 31, 2020 at 19:09
  • When I add a \strut, the header get's too long. How would I go about fixing that?
    – weisbrja
    Jan 2, 2021 at 20:03

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.