1

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}

enter image description here

Daniel
  • 1,787
  • See also https://tex.stackexchange.com/questions/307082/full-page-landscape-figure-in-middle-of-document. There I used a separate abspage counter. – John Kormylo Apr 25 '19 at 12:56

1 Answers1

4

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}
Ulrike Fischer
  • 327,261
  • 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 08:58
  • 1
    \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 09:04