Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by Pieter van Oostrum for Fancyhdr automatic header height

The problem is, at the time when the header and the footer are constructed, the body has already been made, and it can not be changed anymore. So you must set the geometry before the page construction is started.

If the height of your headers does not depend on the contents of the page, you can measure the height and then issue a \newgeometry command with the measured height. You will have to repeat this each time the header is changed (or at least when its height could change). Same for the footer.

Here is an idea:

\documentclass{article}[10pt]\usepackage{lipsum}\usepackage[showframe, margin=1in, includehead, includefoot]{geometry}\usepackage{fancyhdr}\pagestyle{fancy}\newlength{\HH}\newlength{\FH}\newcommand{\myhead}[2][]{\settoheight{\HH}{\parbox[b]{\textwidth}{#2}}\fancyhead[#1]{#2}}\newcommand{\myfoot}[2][]{\settoheight{\FH}{\parbox[b]{\textwidth}{#2}}\fancyfoot[#1]{\parbox[b]{\textwidth}{#2}}}\fancyhead{}\myhead[L]{A multiline footer\\Quite tall\\Whose height I do not want have to mention explicitly\\One more line\\And another one}\fancyfoot{}\myfoot[L]{My custom footer\\When it is longer\\It leaves the page\\And that's not what I want\\Definitely\\One more line\\And another one\\And an invisible one}\begin{document}\newgeometry{headheight=\dimexpr\HH+0.3\baselineskip, footskip=\dimexpr\FH+\baselineskip, includehead, includefoot}\lipsum[1-10]\end{document}

N.B. Fancyhdr version 5 will have a command to make this easier.

Image may be NSFW.
Clik here to view.
enter image description here


Viewing all articles
Browse latest Browse all 2

Trending Articles