1

I would like to use the geometry package alongside fancyhdr. To control the distance between header and main text in my article documentclass, this question recommends use of the headsep parameter:

\documentclass[11pt, a4paper]{article}
\usepackage[includehead, headsep=5pt]{geometry}
\usepackage{fontspec}
\usepackage{fancyhdr}

\pagestyle{fancy} \fancyhf{} \fancyhead[C]{A header}%\a new line in the header}

\renewcommand{\headrulewidth}{1.5pt}

\begin{document} \section*{Education}

\end{document}

This seems to work fine, and modifying the headsep indeed modifies the distance between the header and the section title. enter image description here However, it so happens that my header is multi-line. as soon as I uncomment line 9 as follows:

\fancyhead[C]{A header\\a new line in the header}

The section title clashes with the header, which I do not want. enter image description here How can I get headsep to remain aware that my header is now multi line?

user32882
  • 1,594
  • Perhaps you should also modify headheight? – Bernard Aug 18 '21 at 09:22
  • It's just a bit confusing to me why that would be necessary, since according to the geometry package docs "If includehead is set to true, headheight and headsep are considered as a part of height". So I would argue that geometry should calculate the appropriate headheight for me ? – user32882 Aug 18 '21 at 09:26
  • Oh! sorry, I skimmed this point. What was your new value of headsep? – Bernard Aug 18 '21 at 09:31
  • I just kept the same value of headsep. But do you think that should matter anyway? I would have expected geometry to calculate the correct headheight such that no matter what headsep I pick it would always work. – user32882 Aug 18 '21 at 09:34
  • It doesn't seem to. I've just tested, and setting headsep to 4ex yields a correct result. There are warnings as to headheight being too small, and advising to make it at least 25.52pt. – Bernard Aug 18 '21 at 09:38
  • so includehead only works for single line headers? That seems a little... limiting? – user32882 Aug 18 '21 at 09:40
  • There is a default headheight, which is set to 12pt. – Bernard Aug 18 '21 at 09:42
  • Oh... so geometry doesn't dynamically calculate the headheight based on the header generated by fancyhdr. I see what you're saying. In that case, can I redefine the default headheight in geometry based on my real fancyhdr header instead of hardcoding it like that? In other words, is the real headheight value stored in a variable somewhere and can I pass that to geometry? – user32882 Aug 18 '21 at 09:44
  • 1
    It's a key; you can change its value through the optional arguments for geometry in your preamble, or specify it with geometry{headheight=…}. I think it's better to specify it in the ex unit, so if you decide to modify the font size of your document, you shoudn't have any modification to do for headheight. – Bernard Aug 18 '21 at 09:52
  • 1
    or set up fancyhdr first including then set up geometry after you have set up the heading, if you increase the size of the content after setting up the page the output you show isn't that surprising – David Carlisle Aug 18 '21 at 10:41
  • A future version of fancyhdr will have a (limited) facility to automatically calculate \headheight and pass it to geometry. I have it working in my private development copy. For now use \usepackage[includehead, headsep=30pt,headheight=26pt]{geometry} or similar. For the value needed for headheight`` see your.log` file. – Pieter van Oostrum Aug 18 '21 at 13:38
  • IIRC, fancyhdr changes \headheight in the middle of the document. Geometry runs in the preamble and no long has any control. – John Kormylo Aug 18 '21 at 16:12

2 Answers2

1

Starting with the OP's code, I just augmented the \headheight and decremented the \topmargin by equal amounts. Then, the header behaves as I presume the OP would desire.

MWEs below show single-line and double-line header.

\documentclass[11pt, a4paper]{article}
\usepackage[includehead, headsep=5pt]{geometry}
\headheight=\dimexpr\headheight+15pt 
\topmargin=\dimexpr\topmargin-15pt 
\usepackage{fontspec}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{A header a new line in the header}

\renewcommand{\headrulewidth}{1.5pt}

\begin{document} \section*{Education}

\end{document}

enter image description here

\documentclass[11pt, a4paper]{article}
\usepackage[includehead, headsep=5pt]{geometry}
\headheight=\dimexpr\headheight+15pt 
\topmargin=\dimexpr\topmargin-15pt 
\usepackage{fontspec}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{A header\\ a new line in the header}

\renewcommand{\headrulewidth}{1.5pt}

\begin{document} \section*{Education}

\end{document}

enter image description here

0

To get a nice layout of a page

z

use the layout package to understand the various dimensions involved in designing a page and their relationships.

To have two lines in the header space you need to set headheight=25pt (5) or more, if plan to add more lines or a figure. Then to separate the header from the text area set headsep=20pt (6) or similar. Finally to allocate enough space from the top of the physical page to the top of the text area set the top= 1in or more.

x . y y

The headheight, the headsep, and others are fixed spaces reserved for your use. They are not aware of overflows nor they collapse in the absence of content.

There’s more than one way to skin a cat. If you overspecify geometry will complain.

The log file is also instructive.

*geometry* driver: auto-detecting
*geometry* detected driver: luatex
*geometry* verbose mode - [ preamble ] result:
* driver: luatex
* paper: a4paper
* layout: <same size as paper>
* layoutoffset:(h,v)=(0.0pt,0.0pt)
* modes: includehead 
* h-part:(L,W,R)=(89.62709pt, 418.25368pt, 89.6271pt)
* v-part:(T,H,B)=(60.0pt, 683.6402pt, 101.40665pt)
* \paperwidth=597.50787pt
* \paperheight=845.04684pt
* \textwidth=418.25368pt
* \textheight=633.6402pt
* \oddsidemargin=17.3571pt
* \evensidemargin=17.3571pt
* \topmargin=-12.26999pt
* \headheight=25.0pt
* \headsep=25.0pt
* \topskip=11.0pt
* \footskip=30.0pt
* \marginparwidth=50.0pt
* \marginparsep=10.0pt
* \columnsep=10.0pt
* \skip\footins=10.0pt plus 4.0pt minus 2.0pt
* \hoffset=0.0pt
* \voffset=0.0pt
* \mag=1000
* \@twocolumnfalse
* \@twosidefalse
* \@mparswitchfalse
* \@reversemarginfalse
* (1in=72.27pt=25.4mm, 1cm=28.453pt)

This is a MWE

\documentclass[11pt, a4paper]{article}
\usepackage[ headheight=25pt, headsep=25pt, top = 60pt, includehead]{geometry}
\usepackage{fontspec}
\usepackage{fancyhdr}

\usepackage{layout}% added<<<<<

\pagestyle{fancy} \fancyhf{} \fancyhead[C]{A header \ a new line in the header}

\renewcommand{\headrulewidth}{1.5pt}

\begin{document} \layout% added<<<<< \section*{Education}
\end{document}

Simon Dispa
  • 39,141