This seems like a bug to me, involving a conflict between the storeareas
command and the KOMAoptions
. Here are a few workarounds.
I have simplified the example a little. The one change I have made is to add a clearpage
after each page. Otherwise, in some cases the wrong page is affected or pages are joined together.
You can get it to work by using a second storeareas
command:
\documentclass[pagesize,paper=a4]{scrreprt}
\begin{document}
First page in portrait\\
\clearpage
\storeareas\myvalues
\KOMAoptions{pagesize,paper=landscape,DIV=20}
\storeareas\landscapevalues
Second page in landscape\\
\clearpage
\myvalues
Third page in portrait again\\
\clearpage
\landscapevalues
Fourth page IS in landscape!!!!\\
\end{document}
Or, you can scrap the storeareas
command altogether:
\documentclass[pagesize,paper=a4]{scrreprt}
\begin{document}
First page in portrait\\
\clearpage
\KOMAoptions{pagesize,paper=landscape,DIV=20}
Second page in landscape\\
\clearpage
\KOMAoptions{pagesize,paper=portrait,DIV=calc}
Third page in portrait again\\
\clearpage
\KOMAoptions{pagesize,paper=landscape,DIV=20}
Fourth page IS in landscape!!!!\\
\end{document}
The simplest change is hacky and shows how fragile things are. You can change the paper "back" to a4 right before the second landscape declaration. (It actually also works to simply change the second paper=landscape
to paper=a4
, but this seems even hackier.)
\documentclass[pagesize,paper=a4]{scrreprt}
\begin{document}
First page in portrait\\
\clearpage
\storeareas\myvalues
\KOMAoptions{pagesize,paper=landscape,DIV=20}
Second page in landscape\\
\clearpage
\myvalues
Third page in portrait again\\
\clearpage
\KOMAoptions{paper=a4}
\KOMAoptions{pagesize, paper=landscape,DIV=20}
Fourth page IS in landscape!!!!\\
\end{document}
This issue has nothing to do with the lscape
package: indeed, you can reproduce it with the following simple program
\documentclass[a4paper,twoside]{article}
\begin{document}
A\par
\clearpage
% \setlength{\topskip}{0pt} % uncomment this line to see it happen
B\par
\clearpage
\cleardoublepage
C\par
\end{document}
(uncomment the commented line to see it happen).
I’ve made some investigations, and the culprit seems to be the \@doclearpage
macro in LaTeX’s output routine, more precisely the following line of code
\setbox\@tempboxa\vsplit\@cclv to\z@ \unvbox\@tempboxa
(line 6696 in latex.ltx
, latest version), which is meant to send back to the main vertical list “any left-over non-boxes (writes, specials, etc.)”, as the comments in ltoutput.dtx
say. Unfortunately, if the \topskip
glue is zero, the \vbox{}
inserted by a \clearpage
command is split off too, because it does fit in zero points, in that case; so, that box is returned to the main vertical list, where it causes the ensuing \cleardoublepage
command to insert two extra pages.
To be honest, I’ve not checked all the details, but the following program can be used to confirm, more or less, the above analysis:
\documentclass[a4paper,twoside]{article}
\makeatletter
\def\clearpage{%
\ifvmode
\ifnum \@dbltopnum =\m@ne
\ifdim \pagetotal <\topskip
\hbox{\special{Comment: A}}%
\fi
\fi
\fi
\newpage
\write\m@ne{}%
\vbox{\special{Comment: B}}%
\penalty -\@Mi
}
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
\hbox{\special{Comment: C}}\newpage
\if@twocolumn\hbox{\special{Comment: D}}\newpage\fi\fi\fi}
\makeatother
\showboxbreadth = 1000
\showboxdepth = 10
% \tracingoutput = 1
\begin{document}
A\par
\clearpage
% \setlength{\topskip}{0pt}
B\par
\clearpage
\showlists
% \vbox{} % <<< HERE <<<
\cleardoublepage
C\par
\end{document}
It redefines the \clearpage
and the \cleardoublepage
commands so that they produce boxes that can be unambiguously identified in trace listings (don’t worry if you get some warnings that say “Non-PDF special ignored!”). Run it first as it is, and look at the trace produced by \showlists
; then uncomment the line that says
% \setlength{\topskip}{0pt}
and look again at the transcript: you’ll see that the result is the same as though you had a \vbox{}
command in your source file at the point indicated by % <<< HERE <<<
.
Uncomment \tracingoutput = 1
to get further diagnostic information.
Best Answer
It occurred to me that what you really want is to make the entire page act as a float, rather than put a float into the page.
Changing the margins was harder than I thought. KOMA is obscure on the subject, and geometry forces a \newpage. Even when I reset all the parameters myself, things like \centering or \vfill don't work. I got \vfill to work by using a \vbox, or \hfill to work by using a \hbox, but not both at the same time.
I tried to use \NewEnviron, but \BODY and \afterpage don't work together. Lastly, I could not get the page number to show up on the next page, and eventually just overlayed the thing.