How to make figures appear landscape properly?

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?.

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
\ifnum\zref@extractdefault{rotate\number\value{page}}{page}{0}=0
\PLS@RemoveRotate
\else
\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}

• This rotates the list of figures page as well in my document. I haven't been able to get a MWE, though, if I just add \listoffigures to your code everything seems fine. Any idea? – Claudio Jan 31 at 8:17
• @Claudio I can imagine a number of things that could go wrong -- starting from that you didn't apply my code correctly, didn't do enough runs, that something is special in the page numbering of your document, and not ending at the possibility that there is flaw in my code. But without MWE I will not speculate. – Ulrike Fischer Jan 31 at 9:02
• after some investigation, it seems that the issue is not the list of figures. It is caused by using \frontmatter and \mainmatter, maybe because of the switching from roman to arabic numerals. If you switch the class to book and add \frontmatter\lipsum[1]\mainmatter right after \begin{document} in your example, you should get this behaviour. – Claudio Jan 31 at 10:12
• @Claudio add \if@mainmatter ... \fi to the \AddToShipoutPictureBG code to avoid that it is executed in the frontmatter. – Ulrike Fischer Jan 31 at 15:38
• @Daniel you can use \usepackage[figuresright]{rotating} so that all pictures look to the same side (I would prefer this). If you want that rotating puts the pictures sometimes looking to the right or to the left, you will have to add a test for odd/even pages and then use \PLS@AddRotate{90} or \PLS@AddRotate{-90}. – Ulrike Fischer Feb 11 at 9:13