# Specify distance from page margin to header with geometry

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{lipsum, fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\begin{document}
\lipsum
\end{document}


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:

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

• 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? Sep 1 '15 at 17:50
• 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 Sep 1 '15 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). Sep 1 '15 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 Sep 1 '15 at 17:56
• @daleif That's exactly what I thought, too. So I tried - and failed ... :-/ Sep 1 '15 at 17:57

You should specify top together with the includehead setting:

\documentclass{article}

\usepackage{lipsum, fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\begin{document}
\lipsum
\end{document}


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

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,
top=5mm,
}


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

• 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? Sep 1 '15 at 18:06
• @Sverre: That wasn't part of your original question, was it? Sep 1 '15 at 18:07
• True - it wasn't. Sep 1 '15 at 18:08
• @Sverre: I've added some reference to set the other length as well. Sep 1 '15 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. Sep 2 '15 at 9:13

This I how I ended up doing it:

\documentclass{article}
\usepackage[showframe,
tmargin = 24mm,% distance from page margin to body text
% tmargin (24mm) - headsep (X mm) - headheight (4.22mm) = 16mm (= the desired distance from page margin to header) =
]{geometry}
\usepackage{lipsum, fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\begin{document}
\lipsum
\end{document}


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,

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.
• @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 …