[Tex/LaTex] How to make figures appear landscape properly

afterpagelandscapesidewaysfigure

I know of two options to put figures in landscape.

The first is to use the sidewaysfigure environment. This works very well when it comes to keeping the sequence of figures. However, it leaves the page in portrait and turns the figure instead which makes it hard to read online. I also could not figure out how to put several figures (not sub-figures) on one page.

The second is to use a combination of the landscape environment and \afterpage. This works very well when it comes to showing pages in landscape with the figures easily readable. However, this messes up the sequence of floats. (See sidewaysfigure and landscape and, for the numbering problem, How to get landscape float with correct numbering?.)

Is there a proper way to get the best of both worlds, i.e. landscape pages while keeping the order of figures?

Note on the chosen solution

There are a couple of limitations with the chosen answer. On how to fix them see How to avoid additional page getting rotated?.

Best Answer

Putting more than one figure in a sidewaysfigure is easy: the environment doesn't care how many \captions you add. So use minipage or whatever.

Rotating a page in the pdf viewer is rather easy too: one only need to add a bit of code to the page attributes.

The main problem is that sidewaysfigure is a float. So it is not straightforward to identify the page which needs the rotating code, and it is also not straightforward how to remove the code from the page attributes for the pages after the float has been placed. Answers like Rotate single PDF page when viewing ignore this problem by using hard coded \newpage commands.

One possible solution is to use labels to mark up the pages which should be rotated:

\documentclass{article}
\usepackage{rotating}
\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}}}

\begin{document}
\lipsum[1]

\begin{sidewaysfigure}
\rotatesidewayslabel
\begin{minipage}{4cm}
a picture
\caption{figure}
\end{minipage}
\begin{minipage}{4cm}
a second picture
\caption{figure 2}
\end{minipage}

\bigskip
\centering
\includegraphics{example-image-duck}
\caption{figure}
\end{sidewaysfigure}

\lipsum[3-9]

\begin{sidewaysfigure}
\rotatesidewayslabel
\begin{minipage}{4cm}
a picture
\caption{figure}
\end{minipage}
\begin{minipage}{4cm}
a second picture
\caption{figure 2}
\end{minipage}

\bigskip
\centering
\includegraphics{example-image-duck}
\caption{figure}
\end{sidewaysfigure}

\lipsum[3-9]
\end{document}