Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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

3 Answers 3

up vote 10 down vote accepted

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

share|improve this answer
    
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{}
\fancyhead[LO]{\rightmark}
\fancyhead[RO]{\makebox[0pt][l]{\makebox[2cm][r]{\thepage}}}
\fancyhead[LE]{\makebox[0pt][r]{\makebox[2cm][l]{\thepage}}}
\fancyhead[RE]{\leftmark}

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:

enter image description here

Here's a right hand page:

enter image description here

share|improve this answer
    
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
    \hrule\@height\headrulewidth\@width\headwidth \vskip-\headrulewidth}}

This something like

\makeatletter
    \def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
        \hrule\@height\headrulewidth\@width 2in \vskip-\headrulewidth}}
\makeatother

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

You can easily change this definition to move the headrule around.

share|improve this answer
    
I tried your suggestion. Although the headrule is in fact smaller, it keeps left aligned with the headerbox. As soon as I get 10 more points I will be able to post pictures and I will show you exactly what happens. –  S.V. May 15 '12 at 23:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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