2

When using the geometry and fancyhdr packages it is possible to specify margins that are too small to hold the header and footer.

\documentclass{article}
\usepackage{geometry}
\usepackage{fancyhdr}
\geometry{vmargin=0.5in,
  head=\dimexpr1in+\dp\strutbox\relax,
  foot=\dimexpr1in+\dp\strutbox\relax}
\fancypagestyle{foo}{
  \fancyhead[L]{\rule{\textwidth}{1in}}
  \fancyfoot[L]{\rule{\textwidth}{1in}}}
\pagestyle{foo}
\begin{document}
This does not give any warning that the "margins"
are too small to hold the header and footer.
\end{document}

In my actual use case, I do not know the size of the header and footer in advance, although they are of constant size after \begin{document}. It is relatively easy to adjust the size of the header/footer to fit the content and fancyhdr warns you if the content does not fit. The issue is if the header/footer runs off the page, neither fancyhdr nor geometry warn you.

I would like to be able to specify that I want the margins to be either exactly 0.5 in or just big enough to hold the header/footer. In the case of the above MWE the margins should be 1 in (maybe the top margin should be a little bigger to include \headsep) instead of the 0.5 in that they are. The includehead option would give me a 1.5 in margin which is too big.

In the absence of being able to specify a minimum margin size, is it possible to at least raise a warning.

  • Yes, sorry. I didn't recognise the aliases you're using. (Not really aliases - I just don't know what to call them.) – cfr Jan 11 '16 at 22:20
  • This is really a(nother) geometry bug, isn't it? It should at least warn you that it can't satisfy the equations given the specified values. Ideally, it should recalculate earlier values, if necessary, to accommodate later ones. – cfr Jan 11 '16 at 22:24
  • @cfr it might be a bug, but it is not obvious to me how to fix it or even get a warning (I think I can for the topmargin) but running off the bottom means I have to add all the lengths up. – StrongBad Jan 11 '16 at 22:30
  • I think for the bottom you are setting the wrong length aren't you? You don't want the \footskip to be 1"+. You want the bottom margin, less the \footskip, to be 1"+. Or have I misunderstood? – cfr Jan 11 '16 at 22:57
  • @cfr it depends what you call the bottom margin. In this example, there needs to be 1 in from the bottom edge of the paper to the bottom of the text. – StrongBad Jan 12 '16 at 1:17
1

You could check the lengths at the beginning of the document and have geometry adjust the margin if necessary. I think that essentially you want to check that tmargin is at least \headheight+\headsep+<some minimal value>.

For example:

\documentclass{article}
\usepackage{geometry,fancyhdr,calc}
\geometry{vmargin=0.5in,
  headheight=\dimexpr1in+\dp\strutbox\relax,
  footskip=\dimexpr1in+\dp\strutbox\relax,
  verbose,
  showframe
}
\fancypagestyle{foo}{%
  \fancyhead[L]{\rule{\textwidth}{1in}}
  \fancyfoot[L]{\rule{\textwidth}{1in}}}
\pagestyle{foo}
\makeatletter
\AtBeginDocument{%
  \setlength\@tempdima{\headheight+\headsep+5pt}%
  \ifdim\Gm@tmargin<\@tempdima\geometry{tmargin=\@tempdima}\fi
}
\makeatother
\begin{document}
This does not give any warning that the `margins' are too small to hold the header and footer.
\end{document}

1" header

Changing the definitions for the page layout and header for a .1" header as follows

\geometry{vmargin=0.5in,
  headheight=\dimexpr.1in+\dp\strutbox\relax,
  footskip=\dimexpr1in+\dp\strutbox\relax,
  verbose,
  showframe
}
\fancypagestyle{foo}{%
  \fancyhead[L]{\rule{\textwidth}{.1in}}
  \fancyfoot[L]{\rule{\textwidth}{1in}}}
\pagestyle{foo}

results in a 1/2" top margin

.1in header with 1/2" margin

And if the headheight is not large enough, of course, fancyhdr will warn you.

  • It would be nice to be able to do this for the bottom, too. But, in that case, I guess what's needed is to adjust \textheight but geometry won't allow that and ignores the adjustment on the grounds that it constitutes an attempt to over-specify. – cfr Jan 11 '16 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.