[Tex/LaTex] How to change the page layout back and forth

geometrymarginspage-breaking

I'm trying to do some absolute placement on a page but because I use pdflscape, I can't use the textpos package for this. I tried around a bit with moving with \hspace* and \vspace* but this is very tedious and produces overfull boxes galore. So I tried to change the page layout for just one page which basically works but the next page is broken in ways I cannot comprehend.

Here is what I did; I store the old dimensions, set everything to zero or whatever (pdf)LaTeX needs to make it zero and then restore the old dimensions from the saved values:

\documentclass{article}

\usepackage[english]{babel}
\usepackage{blindtext}

\newlength{\oldtextheight}
\newlength{\oldtextwidth}
\newlength{\oldevensidemargin}
\newlength{\oldoddsidemargin}
\newlength{\oldcolumnsep}
\newlength{\oldtopmargin}
\newlength{\oldheadheight}
\newlength{\oldheadsep}
\newlength{\oldfootskip}

\begin{document}

\blindtext[5]

\newpage%
% save old page dimensions
\setlength{\oldtextheight}{\textheight}%
\setlength{\oldtextwidth}{\textwidth}%
\setlength{\oldevensidemargin}{\evensidemargin}%
\setlength{\oldoddsidemargin}{\oddsidemargin}%
\setlength{\oldcolumnsep}{\columnsep}%
\setlength{\oldtopmargin}{\topmargin}%
\setlength{\oldheadheight}{\headheight}%
\setlength{\oldheadsep}{\headsep}%
\setlength{\oldfootskip}{\footskip}%
% set all margins to 0
\setlength{\textheight}{\paperheight}%
\setlength{\textwidth}{\paperwidth}%
\setlength{\evensidemargin}{-1in}%
\setlength{\oddsidemargin}{-1in}%
\setlength{\columnsep}{0pt}%
\setlength{\topmargin}{-1in}%
\setlength{\headheight}{0pt}%
\setlength{\headsep}{0pt}%
\setlength{\footskip}{0pt}%
% do absolute placement stuff where textpos doesn't work:
\noindent\rule{5mm}{5mm}\newpage% the necessity for this \newpage is also strange
% reset page dimensions
\setlength{\textheight}{\oldtextheight}%
\setlength{\textwidth}{\oldtextwidth}%
\setlength{\evensidemargin}{\oldevensidemargin}%
\setlength{\oddsidemargin}{\oldoddsidemargin}%
\setlength{\columnsep}{\oldcolumnsep}%
\setlength{\topmargin}{\oldtopmargin}%
\setlength{\headheight}{\oldheadheight}%
\setlength{\headsep}{\oldheadsep}%
\setlength{\footskip}{\oldfootskip}%
%\newpage % this wouldn't help a bit

\blindtext[10]

\end{document}

While the margins seem more or less intact, the type area is larger than the actual page. As I reset \textheight along with everything else, this makes no sense to me. The following image shows page 3 from the above example (click to enlarge):

One page later, everything is back to normal. Any help on what is happening and how I can make a clean reset would be much appreciated.

Maybe I should add that I've read that newer versions of the geometry package have \newgeometry and \restoregeometry commands that appear to do just what I'm trying to do manually but the TeXLive distribution on my current Beta Ubuntu (Precise) isn't new enough and I'd rather not mess with my package manager (it always pays me back one day) or my TeX trees in general (it's dark art and never works like it should).

I did try the gmeometric package (\geometry{textheight=\paperheight,textwidth=\paperwidth} and \geometry{textheight=\oldtextheight,textwidth=\oldtextwidth}) but to no avail.

Best Answer

If you can afford to make your code depend on a version of geometry that is not yet included in Debian, Ubuntu nor in Fedora, you can use the following code to replace the one in the question. It is much shorter to be sure.

\documentclass{article}

\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{geometry}[2010/09/12]
\begin{document}

\blindtext[5]

\newgeometry{textheight=\paperheight,textwidth=\paperwidth}
% do absolute placement stuff where textpos doesn't work:
\noindent\rule{5mm}{5mm}
\restoregeometry

\blindtext[10]

\end{document}

I couldn't make out which trick geometry uses though. The last thing \restoregeometry does is to invoke \Gm@changelayout which just sets some lengths, some standard, some internal to geometry. Not even so much as a \clearpage.

So an answer that actually explains why the code in the question behaves so strangely would still be much appreciated and accepted as correct answer.