Page LayoutIssues w/ Fancyhdr and Geometry (setting margins)

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
EFaden
Posts: 24
Joined: Thu Jun 04, 2009 2:58 am

Issues w/ Fancyhdr and Geometry (setting margins)

Post by EFaden »

Hey All,

Here is what I have

Code: Select all

\documentclass[12pt]{article}

\title{Blah Blah}
\author{Me}

\usepackage{fancyhdr}
\usepackage{lastpage}

%% Margins
\usepackage{geometry}
\geometry{hmargin={.5in,.5in},vmargin={.5in,.5in}}

%% Header/Footer
\pagestyle{fancy} 
\lhead{Blah Blah}
\chead{}
\rhead{Me\\\today}
\lfoot{}
\cfoot{}
\rfoot{\thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{1.0pt}
\renewcommand{\footrulewidth}{1.0pt}

<Rest of Document>
It's not doing what I want. What I am trying to do is set the page margins at .5in on all four sides. I want the header to start at .5in and go from there. I want the footer to start so it ends at .5in on the bottom. The problem is that isn't whats happening. I have played with a bunch of variables and can't seem to get it right. The other problem is that the first page seems to be different than the rest even if I manually set the pagestyle. So everytime I try to set the variables like textheight, etc the footer on the first page is in a different spot than the 2nd, 3rd etc pages. Anyone know how to do this?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Issues w/ Fancyhdr and Geometry (setting margins)

Post by localghost »

You have to include the headers and footers into the actual type area to get the margins you want. The following example shows how to do that.

Code: Select all

\documentclass[12pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[includeheadfoot,margin=0.5in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{blindtext}

%% Header/Footer
\fancyhf{}
\lhead{\rightmark}
\rhead{EFaden\\\today}
\rfoot{\thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{1.0pt}
\renewcommand{\footrulewidth}{1.0pt}
\pagestyle{fancy}

\title{Margin problem}
\author{EFaden}

\begin{document}
  \blinddocument
\end{document}
Pay special attention to the additional option for geometry. The manual has detailed explanations.


Best regards and welcome to the board
Thorsten
EFaden
Posts: 24
Joined: Thu Jun 04, 2009 2:58 am

Re: Issues w/ Fancyhdr and Geometry (setting margins)

Post by EFaden »

localghost,

I tried the tex you gave and it seems to me that the footers on page 1 and pages 2 & 3 are in different places. The footer on page 1 is "higher" (there is more of a gap between the footer on page 1 and the other pages), which is the problem I have been having. How do you fix that?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Issues w/ Fancyhdr and Geometry (setting margins)

Post by localghost »

You are right. I forgot to mention that there is a warning about the head too slim. This causes the differences in the footer on the first page. This can be "corrected" by adding a line to the preamble.

Code: Select all

\setlength{\headheight}{28pt}  % modification suggested by the compiler
But now all footers are too close to the bottom of the paper. I assume that this is caused by the header which contains two lines in your case. So the compiler has difficulties in calculating the rest. I suggest to modify your sample code as follows.

Code: Select all

\documentclass[12pt,letterpaper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage[includeheadfoot,margin=0.5in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{blindtext}

%% Header/Footer
\fancyhf{}
\lhead{EFaden}
\rhead{\today}
\rfoot{\thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{1.0pt}
\renewcommand{\footrulewidth}{1.0pt}
\pagestyle{fancy}

\title{Margin problem}
\author{EFaden}

\begin{document}
  \blinddocument
\end{document}
Headers with more than one line often cause trouble. If you can live with this layout, use it.
EFaden
Posts: 24
Joined: Thu Jun 04, 2009 2:58 am

Issues w/ Fancyhdr and Geometry (setting margins)

Post by EFaden »

Ah... thats very interesting. I tried the first line you suggested and it did exactly what you said, shifted the other part down. If you also add

Code: Select all

\setlength{\textheight}{9.0in}
it seems to combat that and raise the footer up to the correct location (or around it) which seems to make some sense. Assuming a page of 11in, we have .5 on the top for the margin, .5 on the top for the header, .5 on the bottom for the footer, and .5 on the bottom for the margin thus yielding 9.0in for the textheight. Seems to make some sense.

I could also remove the two line header, I didn't realize that was the cause of it. But this is what I think works....

Code: Select all

\documentclass[12pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[includeheadfoot,margin=0.5in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{blindtext}

\setlength{\headheight}{28pt}
\setlength{\textheight}{9.0in}

%% Header/Footer
\fancyhf{}
\lhead{\rightmark}
\rhead{EFaden\\\today}
\rfoot{\thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{1.0pt}
\renewcommand{\footrulewidth}{1.0pt}
\pagestyle{fancy}

\title{Margin problem}
\author{EFaden}

\begin{document}
  	\blinddocument
\end{document}
It seems that either that or the single line header would work. It's just interesting to me that the 2 line header really throws it off.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Issues w/ Fancyhdr and Geometry (setting margins)

Post by localghost »

EFaden wrote:[...] If you also add

Code: Select all

\setlength{\textheight}{9.0in}
it seems to combat that and raise the footer up to the correct location (or around it) which seems to make some sense. [...]
You better incorporate this into the options for the geometry package instead.

Code: Select all

\usepackage[includeheadfoot,margin=0.5in,textheight=9in]{geometry}
EFaden
Posts: 24
Joined: Thu Jun 04, 2009 2:58 am

Re: Issues w/ Fancyhdr and Geometry (setting margins)

Post by EFaden »

So put textheight in the geometry options, but the headheight by itself?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Issues w/ Fancyhdr and Geometry (setting margins)

Post by localghost »

As far as I know, you can specify all dimensions regarding page and paper dimensions in the option list.

Code: Select all

\usepackage[%
  head=28pt,
  includeheadfoot,
  margin=0.5in,
  textheight=9in
]{geometry}
For more information please refer to the manual.
Post Reply