[Tex/LaTex] Change Geometry after first page *without* knowing where the second page begins

geometry

I have this for my first page:

% Custom margins - FIRST PAGE
\newcommand{\lrmargin}{1in}
\usepackage[top=0.5in, 
            bottom=1in, 
            left=\lrmargin, 
            right=\lrmargin, 
            heightrounded]{geometry}

And I have a bunch of text. Somewhere in there, Latex creates a second page of text for me. If I then go exactly where that is, and paste the following:

% CUSTOM MARGINS - SECOND PAGE
\renewcommand{\tbmargin}{1in}
\renewcommand{\lrmargin}{1in}
\newgeometry{top=\tbmargin,
             bottom=\tbmargin,
             left=\lrmargin, 
             right=\lrmargin}

I get the expected behavior. Now, if I change the margins of the first page again, that would necessarily change what text would appear on the second page and that messes up my changing the geometry of the second page. Instead what I get is two pages with the "FIRST PAGE" settings, and the second page has a huge amount of blank space and a third page with the correct settings.

I know that to use a different geometry on another page, you use the \newgeometry command. My question is – where do I place that in my latex code so that it always effects the second (and third etc.) pages without letting the first call to geometry affect that?

Best Answer

Caveat: Use at Your Own Risk!

As D.C. says (and as it is well known), it is basically impossible to have the text width change at a page boundary in an “automatic” fashion, that is, whatever the text that spans the two pages is. This is because TeX chooses the line breaks within each paragraph before it knows where it is going to cut off each page, and doesn’t change them any more afterwords. In principle, it is possible to prohibit page breaks within paragraphs by setting the \interlinepenalty parameter to a value ≥ 10000, but in practice this is only feasible if you have very short paragraphs that never exceed two or three lines in length. Note, however, that this could be the case inside a table of contents or similar index; another situation in which this might be true is inside at itemize or enumerate environment.

However, if you do not need to change the line width, but only the margins, the following code shows a crude hack by means of which this could be achieved. Because of the \globaldefs=1 it contains, I explicitly decline any warranty that this methods will not break anything: use it solely at your own risk!

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage[showframe]{geometry}
\usepackage{afterpage}
\usepackage{lipsum}

\geometry{left=3cm,textwidth=12cm,top=2cm,bottom=2cm,heightrounded}



\begin{document}

\newgeometry{left=5cm,textwidth=12cm,top=5cm,bottom=7cm,heightrounded}

This is a test.

\afterpage{\globaldefs=1 \restoregeometry}

Now \( \verb|\globaldefs|=\texttt{\number\globaldefs} \).

\lipsum[1-16]

\end{document}