How to avoid additional page getting rotated?

I am trying to get all sidewaysfigures automatically rotated. But for some reason I get an extra rotated page (see code and screenshot below). How can I avoid this? (This is part of a sequence of problems with sideways oriented figures, see e.g. How to make figures appear landscape properly?)

\documentclass[oneside]{scrbook}

% Rotation trick from https://tex.stackexchange.com/a/472608/36836
\usepackage{rotfloat}
\usepackage{pdflscape,lipsum}
\usepackage{eso-pic,zref-user}
\newcounter{cntsideways}

\makeatletter

\AddToShipoutPictureBG{%
\ifnum\zref@extractdefault{rotate\number\value{page}}{page}{0}=0
\PLS@RemoveRotate
\else
\PLS@AddRotate{90}%
\fi%
}

\newcommand\rotatesidewayslabel{\stepcounter{cntsideways}%
\zlabel{tmp\thecntsideways}\zlabel{rotate\zref@extractdefault{tmp\thecntsideways}{page}{0}}}

\makeatother

% patch all sidewaysfigures to rotate
\usepackage{xpatch}
\xapptocmd{\sidewaysfigure}{\rotatesidewayslabel}{}{}

\begin{document}
\title{Title}

\maketitle
\tableofcontents{}

\mainmatter

\chapter{One}

\begin{sidewaysfigure}
\caption{}

\end{sidewaysfigure}

\end{document}


1 Answer

You are using \mainmatter and this means that you have two pages with the number 2. Restrict the code to the mainmatter part:

\documentclass[oneside]{scrbook}

% Rotation trick from https://tex.stackexchange.com/a/472608/36836
\usepackage{rotfloat}
\usepackage{pdflscape,lipsum}
\usepackage{eso-pic,zref-user}
\newcounter{cntsideways}

\makeatletter

\AddToShipoutPictureBG{%
\if@mainmatter
\ifnum\zref@extractdefault{rotate\number\value{page}}{page}{0}=0
\PLS@RemoveRotate
\else
\PLS@AddRotate{90}%
\fi%
\fi
}

\newcommand\rotatesidewayslabel{\stepcounter{cntsideways}%
\zlabel{tmp\thecntsideways}\zlabel{rotate\zref@extractdefault{tmp\thecntsideways}{page}{0}}}

\makeatother

% patch all sidewaysfigures to rotate
\usepackage{xpatch}
\xapptocmd{\sidewaysfigure}{\rotatesidewayslabel}{}{}

\begin{document}
\frontmatter
\title{Title}

\maketitle
\tableofcontents{}

\mainmatter

\chapter{One}

\begin{sidewaysfigure}
\caption{}

\end{sidewaysfigure}

\end{document}


The alternative is to base the labels on an absolute page number:

\documentclass[oneside]{scrbook}

% Rotation trick from https://tex.stackexchange.com/a/472608/36836
\usepackage{rotfloat}
\usepackage{pdflscape,lipsum}
\usepackage{eso-pic,zref-user,zref-abspage}
\newcounter{cntsideways}

\makeatletter

\AddToShipoutPictureBG{%
\ifnum\zref@extractdefault{rotate\the\numexpr\value{abspage}+1}{abspage}{0}=0
\PLS@RemoveRotate
\else
\PLS@AddRotate{90}%
\fi%
}

\newcommand\rotatesidewayslabel{\stepcounter{cntsideways}%
\zlabel{tmp\thecntsideways}\zlabel{rotate\zref@extractdefault{tmp\thecntsideways}{abspage}{0}}}

\makeatother

% patch all sidewaysfigures to rotate
\usepackage{xpatch}
\xapptocmd{\sidewaysfigure}{\rotatesidewayslabel}{}{}

\begin{document}
\frontmatter
\title{Title}

\maketitle
\tableofcontents{}

\mainmatter

\chapter{One}

\begin{sidewaysfigure}
\caption{}

\end{sidewaysfigure}

\end{document}

• Thanks. I noticed that the first solution only works when \frontmatter is added. I guess the second is more universally applicable. Apart from that is one solution preferable to the other? – Daniel Apr 23 '19 at 8:58
• \frontmatter sets the \@mainmatter switch to false. You could also move the \AddToShipoutPictureBG simply behind \mainmatter. The abspage version is probably better (and I wonder if it needs the tmp counter, but I don't have the time now to look). – Ulrike Fischer Apr 23 '19 at 9:04