[Tex/LaTex] Headrule length in fancyhdr

fancyhdrheader-footerrules

Latex PageUsing the fancyhdr package, is it possible to change(increase or decrease) the headrule length without changing the header length?

In the picture related, one can see that the headrule rectangle(in red) does not have the same size length as header. This is exactly what I want. Actually, I would like headrule and textwidth with the same length, but header should be bigger than both.

By using, for example:

\fancyheadoffset{1 cm} 

I can make the header bigger than textwidth. But the headrule grows as well, and both header and headrule have the same length.

In this other example that I am adding, one can see the expanded header, with the logo, and the headrule with the same size as the header.
What if I want to make the headrule with the same size as the footrule (or with whatever size/align I want), keeping the header unchanged?

Another Example

Best Answer

You can simply redefine \headrule; in the following example I used two new lengths \HFleft and \HFright to control the left and right trimming of the headrule, respectively; simply changing the values for those lengths you can shorten the headrule in the desired way. An auxiliary \FHoffset length is used as the argument of \fancyheadoffset. Some examples:

Initially, \FHoffset is set to 0cm as well as both trimmings:

\documentclass{article}
\usepackage{lipsum}% just to generate filler text for the example
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[L]{Left}
\fancyhead[R]{Right}
\fancyhead[C]{Center}

% Length to control the \fancyheadoffset and the calculation of \headline
% simultaneously
\newlength\FHoffset
\setlength\FHoffset{1cm}

\addtolength\headwidth{2\FHoffset}

\fancyheadoffset{\FHoffset}

% these lengths will control the headrule trimming to the left and right 
\newlength\FHleft
\newlength\FHright

% here the trimmings are controlled by the user
\setlength\FHleft{1cm}
\setlength\FHright{0cm}

% The new definition of headrule that will take into acount the trimming(s)
\newbox\FHline
\setbox\FHline=\hbox{\hsize=\paperwidth%
  \hspace*{\FHleft}%
  \rule{\dimexpr\headwidth-\FHleft-\FHright\relax}{\headrulewidth}\hspace*{\FHright}%
}
\renewcommand\headrule{\vskip-.7\baselineskip\copy\FHline}

\begin{document}

\section{Test Section}
\subsection{Test Subsection}
\lipsum[1-20]

\end{document}

enter image description here

Setting \FHoffset to 1cm and \FHleft and \FHright to 1cm, the width of the headrule will be equal to \textwidth and the headrule will span the text width:

enter image description here

Changing the settings to

\setlength\FHoffset{1cm}
\setlength\FHleft{1cm}
\setlength\FHright{0cm}

the result is now

enter image description here

and with

\setlength\FHoffset{1cm}
\setlength\FHleft{2cm}
\setlength\FHright{4cm}

we get

enter image description here

Related Question