# Newgeometry and afterpage don't adjust the geometry

In the following sample, the geometry doesn't change even though I believe it should. What am I doing wrong and how can I fix it?

\documentclass{article}
\usepackage{lipsum}
\usepackage{afterpage}
\usepackage[bottom=5cm]{geometry}
\begin{document}
\afterpage{\newgeometry{bottom=1cm}}
\lipsum
\lipsum
\end{document}

• Are you trying to make the first page shorter than the following ones? – egreg Sep 11 '15 at 10:50
• Do not use afterpage or newgeometry for this. Just use \enlargethispage on the first page to change its size. – David Carlisle Sep 11 '15 at 11:17
• I have a letter with a giant footer on the first page. The pages after the first one have small ones that would fit without any fiddling. So yes I am trying to decrease the typearea on the first page in favour of the footer – Max Sep 11 '15 at 11:21
• so use \enlargethispage{-3cm} on the first page. – David Carlisle Sep 11 '15 at 11:25

\documentclass{article}
\usepackage{lipsum}
%\usepackage[bottom=5cm]{geometry}
\begin{document}
\enlargethispage{-5cm}
\lipsum
\lipsum
\end{document}

• I am sorry. While this solves it for the minimal sample, I run into issues with geometry. If I use the showframe option to that package, I can see that while the text obeys the \enlargethispage the textarea and more importantly the footer does not and runs off the page's end. – Max Sep 11 '15 at 11:45
• @Max Yes but the normal thing is to put the footer into \smash{some big image} so the height is hidden and so it overprints the space left in the text area. – David Carlisle Sep 11 '15 at 12:11

afterpage puts it argument in a group and then (most of) the settings of \newgeometry don't work as you can see here:

\documentclass{article}
\usepackage{lipsum}
\usepackage{afterpage}
\usepackage[bottom=5cm]{geometry}
\begin{document}
\lipsum
\lipsum
{\newgeometry{bottom=1cm}} %bottom ignored
\lipsum
\lipsum
\newgeometry{bottom=1cm}
\lipsum
\lipsum

\end{document}

• Hmm Is there some way to break out of the group or some other way to achieve this? – Max Sep 11 '15 at 11:23
• Use David's suggestion. You can hide the height of the large footer with e.g. \lfoot{\smash{\rule{3cm}{5cm}}}. afterpage injects code in the output routine and you really don't want it to leak out. – Ulrike Fischer Sep 11 '15 at 12:39

After searching the entire internet for a solution (the ones listed here did not work for my setup), I finally came up with this one:

%setup your default geometry:
\usepackage[a4paper, left=2.3cm, right=2.3cm, bottom=2.0cm, top=3.0cm]{geometry}

%use this package to get the "on-every-page-do-this-Hook" below
\usepackage{everypage}

\begin{document}

%this line registers the reset-hook:

%and this line sets your special geometry which is cleared by the hook above on page N (with N=2 in this case)
\newgeometry{left=2.3cm, right=1.7cm, bottom=2.0cm, top=9.7cm}


EDIT:

After wrapping up my proposal to a compilable example spanning several pages with lorem-ipsum-text I found it actually not working properly!

So, instead, the afterpage-package in combination with \globaldefs=1 seems like the way to go here:

\documentclass{article}
\usepackage{lipsum}
\usepackage{afterpage}
\usepackage[bottom=5cm]{geometry}
\begin{document}
\afterpage{\globaldefs=1 \newgeometry{bottom=1cm}}
\lipsum
\lipsum
\end{document}

• This worked exactly as I needed it, and the \globaldefs=1 did exactly what it needed. Could you add a bit of explanation as to what \globaldefs=1 does to fix this or provide a link? – oliversm Oct 15 '19 at 16:27
• Glad to hear that it worked for you! If I got it right, the \globaldefs=1 statement lifts the subsequent newgeometry... statement out of the deeper scope from which it will be invoked (i. e. the scope that it is given by the \afterpage-logic) up to the global scope where it has to overwrite the desired parameter setting (bottom=5cm by bottom=1cm). Here's a first anchor: https://en.wikibooks.org/wiki/TeX/globaldefs – Sebastian Oct 16 '19 at 18:00

The trick is not using \newgeometry, that would issue a page break anyway, but \enlargethispage; with fancyhdr you are ensured that the footer will stay in position, it's only sufficient to \smash the giant footer for the first page.

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{graphicx}

\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot[C]{\small\thepage}

\fancypagestyle{first}{%
\fancyhf{}%
\fancyfoot[C]{\giantfooter}%
}

\newcommand{\giantfooter}{%
\leavevmode\smash{\includegraphics[height=5cm]{duck}}%
}

\begin{document}
\thispagestyle{first}\enlargethispage{-5cm}

\lipsum[1-20]

\end{document}


Here is a solution

\documentclass{article}
\usepackage{lipsum}

\usepackage{atbegshi}
\usepackage[bottom=5cm]{geometry}
\begin{document}
\makeatletter
\AtBeginShipoutNext{%