8

For my homework assignments I usually use the fancyhdr package to create a header that contains the group (A, B, C etc), homework title, my name + student number and the date. Besides that I practically always use the geometry package to adjust the page margins.

The problem that appears is that the margins seem to change per page, or more specific: the margins of the first page differ from the others. As an illustration of the problem, consider the following MWE.

\documentclass[a4paper]{article}
\usepackage{fancyhdr,lipsum} % lipsum for filler text
\usepackage[margin=4.4cm]{geometry}
\begin{document}
\lhead{Group, homework corrector\\$\,$\\}
\rhead{Homework title\\Name (student number)\\\today}
\pagestyle{fancy}
\lipsum[1-5]    % filler text
\newpage
\lipsum[6-10]   % filler text
\newpage
\lipsum[11-15]  % filler text
\end{document}

Here [a4paper] seems necessary to not let geometry change the dimensions of the paper, lipsum is used for filling, and there are 3 pages written to show that the second and third have the same margins, which differ from those of the first page. The problem should not come from a conflict between fancyhdr and geometry, as it does also arise when the latter is commented out.

Now there are two things I try to achieve, with the corresponding questions:

  1. How do I ensure consistent margins troughout the document, as described above?
  2. How do I get the text on the second and following pages to start at the height the header starts at on the first page when using \thispagestyle{fancy} instead of \pagestyle{fancy}?

I have tried to solve the second point by redefining the length of \headheight, but it seems sloppy to me, as there's no way to be sure the header adds exactly that height on the first page. Here's what I have tried to solve this second problem:

\documentclass[a4paper]{article}
\usepackage{fancyhdr,lipsum} % lipsum for filler text
\usepackage[margin=4.4cm]{geometry}
\begin{document}
\lhead{Group, homework corrector\\$\,$\\}
\rhead{Homework title\\Name (student number)\\\today}
\thispagestyle{fancy}
\lipsum[1-5]    % filler text
\newpage
\setlength{\headheight}{-3\baselineskip}
\lipsum[6-10]   % filler text
\newpage
\lipsum[11-15]  % filler text
\end{document}
5
  • Add headheight=35pt to the options for geometry
    – egreg
    Commented Apr 24, 2013 at 9:53
  • @egreg This does seem to get close, but the header of the first page is still a bit lower than the text on the following pages. Somehow this difference remains when changing the headheight both up and down. For example: both at headheight=10pt and at headheight=35pt there seems to be about one \baselineskip difference between the top of the header and the top of the text on the second page.
    – Betohaku
    Commented Apr 24, 2013 at 10:02
  • If I add headheight=35pt as said to your first example, I get identical positioning on both pages.
    – egreg
    Commented Apr 24, 2013 at 10:23
  • @egreg Yes, you're right. I checked it just for the second one, where it unfortunately didn't work. How did you get to the 35pt? It seems to work for all lengths greater than this (0pt-34pt fails, 35pt-60pt works fine).
    – Betohaku
    Commented Apr 24, 2013 at 10:31
  • Related: tex.stackexchange.com/questions/37585/…
    – lockstep
    Commented Apr 24, 2013 at 10:44

1 Answer 1

8

Add headheight=35pt to the geometry options (showframe is here only to show clearly the result).

\documentclass[a4paper]{article}
\usepackage{fancyhdr,lipsum} % lipsum for filler text
\usepackage[margin=4.4cm,headheight=35pt,showframe]{geometry}
\begin{document}
\lhead{Group, homework corrector\\$\,$\\}
\rhead{Homework title\\Name (student number)\\\today}
\pagestyle{fancy}
\lipsum[1-5]    % filler text
\newpage
\lipsum[6-10]   % filler text
\newpage
\lipsum[11-15]  % filler text
\end{document}

enter image description here

How do you get the 35pt? If I don't add the option, in the log file (and on the terminal) I get the warning

Package Fancyhdr Warning: \headheight is too small (12.0pt):
 Make it at least 34.54448pt.
 We now make it that large for the rest of the document.
 This may cause the page layout to be inconsistent, however.
3
  • This works great for the first problem (I up'd but didn't yet accept). Maybe you could also tell why the \setlength{\headheight}{-3\baselineskip} in the second problem should now be changed to \setlength{\headheight}{x} with x=-2.3\baselineskip or, correspondingly, x=-27.6pt?
    – Betohaku
    Commented Apr 24, 2013 at 10:57
  • @Betohaku You don't want to change \headheight mid document.
    – egreg
    Commented Apr 24, 2013 at 11:30
  • I understand that, I was hinting towards the sloppyness of it in the question. What I meant to ask is if you could explain the height difference (why is it that strange 2.3?), or if you had a more elegant solution. Of course changing lengths mid-document is awful, it was just a temporary solution, an attempt to at least get close. Thanks for pointing this out though, in case other readers try such things.
    – Betohaku
    Commented Apr 24, 2013 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .