# Wrong position of page number post landscape page by KOMA

I construct a enviroment named "landscapepage" by typearea to insert landscape pages in my article.

Everything is all right except that page numbers post those inserted landscape pages are in wrong position as shown in the attached picture.

Why? And how to deal with it?

MWE:

\documentclass{article}
\usepackage{typearea,geometry,fancyhdr}
\geometry{showframe}

\newenvironment{landscapepage}[1][]
{%
\clearpage
\savegeometry{prelandscape}
\KOMAoptions{paper=landscape,pagesize,DIV=15,#1}
\recalctypearea
}
{%
\clearpage
\KOMAoptions{paper=portrait,pagesize}
\recalctypearea
}

\begin{document}
\newgeometry{top=3cm,bottom=3cm}
\section{This is my Portrait Page}
This is my Portrait Page
\clearpage
%%%%%%%%%%%%%%%%%%%%%%%

\begin{landscapepage}[DIV=8]
\pagestyle{fancy}
\fancyhfoffset[E,O]{0pt}
\fancyhf{}
\rfoot{\roman{page}}

\section{This is my Landscape Page}
Text in my landscape section\footnote{Footnote in Landscape}
\clearpage
Text in my landscape second page
\end{landscapepage}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{This is again Portrait Page}
\end{document}


You will find the following warning in the log file of your MWE:

Package typearea Warning: \typearea used at group level 2. Using \typearea inside any group, e.g. environments, math mode, boxes, etc. may result in many type setting problems. You should move the command \typearea outside all groups on input line 26.

Note that \recalctypearea is a short version of  \typearea[current]{last}. So you should not use \recalctypearea in a group. Note that the \loadgeometry{prelandscape} can be removed from your MWE without any changes in the document, because it is also inside the group.

You could define switches for landscape and portrait pages:

\documentclass{article}
\usepackage[usegeometry]{typearea}
\usepackage[showframe]{geometry}
\usepackage{fancyhdr}

\newcommand\landscapepageon[1][]
{%
\cleardoublepage
\savegeometry{prelandscape}
\KOMAoptions{paper=landscape,DIV=15,#1}
\recalctypearea
\bgroup% begins a group
}
\newcommand\landscapepageoff{%
\cleardoublepage
\egroup% ends a group
\KOMAoptions{paper=portrait}
\recalctypearea
}

\begin{document}
\newgeometry{top=3cm,bottom=3cm}
\section{This is my Portrait Page}
This is my Portrait Page

\landscapepageon[DIV=8]
\pagestyle{fancy}
\fancyhfoffset[E,O]{0pt}
\fancyhf{}
\renewcommand\thepage{\roman{page}}
\fancyfoot[R]{\thepage}

\section{This is my Landscape Page}
Text in my landscape section\footnote{Footnote in Landscape}
\clearpage
Text in my landscape second page
\landscapepageoff

\section{This is again Portrait Page}
\end{document}


Remark: \landscapepageon must be followed by \landscapepageoff because of the \bgroup and \egroup in the code. They are needed foryour local changes on the landscape pages.

Additional remark: Do not use \rfoot{\roman{page}}. There would be a roman page number in document but arabic page number in TOC.

You can restore it by repeating \newgeometry{top=3cm,bottom=3cm}

before the portrait page.

Besides, why do you set the DIV to 15 and then redefine it to 8 as soon as you start the landscape page? I get the same lay-out when I set the DIV to 8 in line 9.

• It's boring to remember repeating the former \newgeometry each time when returning to portrait page, so I use \savegeometry to save the current layout befor going into landscape page, and then restore it by \loadgeometry before going into portrait page during defination of my enviroment landscapepage. Why does this way not have the same effectivity with repeating \newgeometry? And DIV=15 is a default option. I can change it whenever I want by the argument #1. – lyl Jun 6 at 14:18