6

With geometry, the placement of the header is set by specifying the distance from the text body below to the header above with headsep:

\documentclass{article}
\usepackage[showframe, headsep = 5mm]{geometry}
\usepackage{lipsum, fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[R]{A header plus the page number: \thepage}
\begin{document}
\lipsum
\end{document}

enter image description here

In MS Word, on the other hand, the placement of the header is set by specifying the distance from the page margin above to the header below:

enter image description here

My problem now is that I am given a guide that tells me the settings in MS Word, and I have to figure out how to translate that into LaTeX. And this setting I can't figure out. How can I with the help of geometry set the distance from the page margin above to the header below (in which case I would just set headsep = 0pt)?

8
  • Then you first have to explain what Word mean. What do they mean by header from to? Measured from where to where? Top of text to top of page? Baseline of header to top of page?
    – daleif
    Sep 1, 2015 at 17:50
  • 1
    One way of doing this is to get a hold of a PDF generated from word that meet the criteria, then put it in the background of a latex doc that has the same page dims, then start fidling. That is what I usually do when replicating Office generated stuff in LaTeX
    – daleif
    Sep 1, 2015 at 17:52
  • @daleif When I set the measure to 0, the text didn't extend beyond the paper margin, so it seems like Header from Top sets the distance from the page top margin to the top of the character boxes inside the header (not to imply that Word uses "boxes", of course).
    – Sverre
    Sep 1, 2015 at 17:53
  • In geometry you can set the headheight of the header box. So via topmargin, headheight, and headsep you can set what is needed to get x above the header box
    – daleif
    Sep 1, 2015 at 17:56
  • @daleif That's exactly what I thought, too. So I tried - and failed ... :-/
    – Sverre
    Sep 1, 2015 at 17:57

3 Answers 3

5

You should specify top together with the includehead setting:

enter image description here

\documentclass{article}

\usepackage[showframe,includehead,top=5mm]{geometry}

\usepackage{lipsum, fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{A header plus the page number: \thepage}
\begin{document}
\lipsum
\end{document}

From the geometry user's manual, Figure 2 displays the meaning of includehead (and includefoot):

enter image description here

If you now want to specify the distance from the top of the page (margin) to the top of the text body, additionally set headsep and headheight. For example, we can set headheight to \baselineskip by default, and then set

\geometry{
  showframe,
  includehead,
  top=5mm,
  headsep=\dimexpr20mm-\baselineskip,
  headheight=\baselineskip
}

to have the top of the body text exactly 25mm from the page top margin.

5
  • This sets the distance from the page margin to the header, but now how do I control the distance from the page margin to the body text?
    – Sverre
    Sep 1, 2015 at 18:06
  • @Sverre: That wasn't part of your original question, was it?
    – Werner
    Sep 1, 2015 at 18:07
  • True - it wasn't.
    – Sverre
    Sep 1, 2015 at 18:08
  • @Sverre: I've added some reference to set the other length as well.
    – Werner
    Sep 1, 2015 at 18:25
  • I'm not sure if using \baselineskip is a good idea. If fancyhdr thinks that a value greater than \baselineskip is needed for headheight, it will simply overwrite your specification and set its own height. And if so, the distances between text and margins will not be what the geometry code would lead you to think it will be. I think it's best to look and see if fancyhdr complains or not, and if it does, then set headheight to whatever fancyhdr asks for.
    – Sverre
    Sep 2, 2015 at 9:13
1

This I how I ended up doing it:

\documentclass{article}
\usepackage[showframe,
    tmargin = 24mm,% distance from page margin to body text
    headheight = 4.22mm,% height of header (the minimum required by fancyhdr)
    % tmargin (24mm) - headsep (X mm) - headheight (4.22mm) = 16mm (= the desired distance from page margin to header) =
    headsep = 3.78mm,% distance from body text to header    
    ]{geometry}
\usepackage{lipsum, fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[R]{A header plus the page number: \thepage}
\begin{document}
\lipsum
\end{document}

enter image description here

0

You could calculate the correct values manually since you’ll hav to set them only once, don’t you? If the values are changing quite often you can use this code

\makeatletter
\geometry{
   showframe,
   top=20mm,
   headsep = \dimexpr\Gm@tmargin-\headheight-5mm\relax,
}
\makeatother

Where \makeatletter enables @ as part of command names. Then in \dimexpr (dimension expression terminated by \relax) we us the top margin, which must be set before headsep width top (!), and subtract the \headheight and an additional 5mm. Resulting in the header starting 5mm from the top of the page.

2
  • And where does gm@tmargin come from? Might not be a good idea to expose internal macros for user use as the internals might change in the future
    – daleif
    Sep 1, 2015 at 18:00
  • @daleif: It’s an internal macro of geometry. And of course it’s not a good idea to use internal macros but using \topmargin didn’t work …
    – Tobi
    Sep 1, 2015 at 21:16

You must log in to answer this question.

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