Using 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?

-
Welcome to TeX.SX! Would you please clarify better what you need? Is it a twoside or oneside document? Where do you want to increase the rule length? –  egreg May 15 '12 at 21:58
Hi egreg. As soon as I get 10 more reputation I will post a picture that clarifies what I want. –  S.V. May 15 '12 at 23:21
You can already insert an image. Just use the button and remove the ! in front of the bracket; a user with enough rep will reinstate it. –  egreg May 15 '12 at 23:25
@user14628: I've updated my answer with some improvements. –  Gonzalo Medina May 17 '12 at 0:21

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}

% simultaneously
\newlength\FHoffset
\setlength\FHoffset{1cm}

% 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}%
}

\begin{document}

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

\end{document}


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:

Changing the settings to

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


the result is now

and with

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


we get

-
Fantastic! This is exactly what I want! How did you learn all these? –  S.V. May 17 '12 at 3:57
@user14628: that's a hard question to answer! Reading lots of code and documentation helped. –  Gonzalo Medina May 17 '12 at 4:09

Assuming that you want to put the page number in the margin, not widening the rule, this is how it can be accomplished:

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}


In the right hand pages, the right field of the header consists of a zero width box, in which a 2cm wide box is typeset, with the page number flush right. In the left hand pages we do the opposite.

Here's a left hand page:

Here's a right hand page:

-
Thank you egreg! But I think I am looking for somethng different. Take a look at the new picture in the OP. –  S.V. May 16 '12 at 0:53

In fancyhdr.sty headrule is defined as

\def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi


This something like

\makeatletter

should do the trick (we changed the width of the header, \headwidth, in the definition, to a hardcoded size of 2 inches).