我正在尝试让所有sidewaysfigure
页面自动旋转。但出于某种原因,我得到了一个额外的旋转页面(见下面的代码和屏幕截图)。我该如何避免这种情况?(这是一系列横向图形问题的一部分,例如如何让图形正确地呈现横向?)
\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
您正在使用 \mainmatter,这意味着您有两个页面编号为 2。将代码限制在 mainmatter 部分:
\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}
另一种方法是根据绝对页码来设置标签:
\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}