# Geometry: strange behaviour after \restoregeometry in afterpage environment (and longtable)

I have some strange problem tying place a "fake float" with large tables. Here is a MWE:

\documentclass[twoside, a4paper]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{geometry}
\usepackage{lipsum,blindtext}
\usepackage{tabu,longtable,booktabs}
\usepackage{showframe}
\usepackage{calc}
\usepackage{afterpage}
\usepackage{rotating}
\usepackage{lscape}

% Set main geometry
\geometry{inner=\paperwidth/9, textwidth=6\paperwidth/9, outer=2\paperwidth/9,
top=\paperheight/9, bottom=2\paperheight/9}

\begin{document}

% Set special layout and save it.
\newgeometry{outer=\paperwidth/9, inner=\paperwidth/9,
textwidth=7\paperwidth/9,top=\paperheight/9,
bottom=\paperwidth/9}
\savegeometry{L}
\restoregeometry

\Blindtext

\afterpage{% First case: vertical long table
\begin{longtabu}to \textwidth {XX}
\caption{Lorem Ipsum}\\
\toprule[2pt]
\rowfont{\bfseries}
Lorem & Ipsum\\
\midrule

\toprule[2pt]
\rowfont{\bfseries}
Lorem & Ipsum\\
\midrule

\lipsum[1] & \lipsum[2] \\
\midrule
\lipsum[1] & \lipsum[2] \\
\bottomrule[2pt]
\end{longtabu}
\restoregeometry
}

\Blindtext

\afterpage{%% Secons case: wide table in landscape env.
\begin{landscape}
\begin{longtabu}  {XX}
\caption{Lorem Ipsum}\\
\toprule[2pt]
\rowfont{\bfseries}
Lorem & Ipsum\\
\midrule

\toprule
\rowfont{\bfseries}
Lorem & Ipsum\\
\midrule

\blindtext & \blindtext \\
\midrule
\blindtext & \blindtext \\
\midrule
\blindtext & \blindtext \\
\bottomrule[2pt]
\end{longtabu}
\end{landscape}
\restoregeometry
}

\Blindtext

\Blindtext
\end{document}


In the first case, the idea is enlarge a bit a page in order to fit a slightly larger table than \textheight. After the long table ends \textwidth is correctly reset to its original value, although bottom margin is not. The page number is also bad placed.

The second problem comes with wide table in landscape mode. As you can see in pag. 7, after exit landscape environment, next page have correct left an right margins, bot bottom one isn't. This is even stranger for me. the text area drawn shows a correct layout, and see the text spilling beyond lower box gets me puzzled.

Any help will be welcome.

• @DavidCarlisle And it is not even longtable.... – cfr Feb 13 '16 at 0:41