[Tex/LaTex] make the geometry of the first page different from other pages

afterpagegeometry

I want to make the top margin in the first page larger than other pages. The code I use is:

\documentclass[12pt]{article}
\usepackage{geometry}
\geometry{
    top=8cm,
    hmargin=1in,
}
\usepackage{mwe}
\usepackage{afterpage}
\begin{document}
\lipsum
\afterpage{
    \newgeometry{
        top=3cm
    }
}
\lipsum
\end{document}

The output pdf has the same page layout for every page and I don't get what I want. How can I have the different layout for the first page?

Best Answer

Two things to be considered:

First, the \afterpage command expands its contents in a group, so changes are reverted unless they are \global. The effect of \newgeometry is local, so nothing happens when you execute it inside \afterpage.

Second, the \afterpage command inserts its argument after the current page has ended, so if you do \lipsum\afterpage{<something>}\lipsum, the <something> will be inserted in the page that follows the end of the first \lipsum (which is, in this case, past the middle of the document).

To work around these issues I used \restoregeometry instead, which is only one token, so it can be easily inserted \aftergroup, and I moved the call to \afterpage right at the beginning of the document, so it will take effect once the first page ends.

\documentclass[12pt]{article}
\usepackage{geometry}
\geometry{
    top=3cm,
    hmargin=1in,
}
\usepackage{mwe}
\usepackage{afterpage}
\begin{document}
\newgeometry{top=8cm}
\afterpage{\aftergroup\restoregeometry}
\lipsum
\lipsum
\end{document}