# Automatic rotation of pages containing sideways tables or figures in PDF output

Is it possible to have float-only pages, containing sideways tables or figures produced by the sidewaystable or sidewaysfigure environments of the rotating package, automatically rotated in the PDF output?

This question is related to 3 existing questions:

Rotate single PDF page when viewing

floating sideways table on a rotated page in pdf-viewer

Problems with newpage and pdfpageattr

I've tried the answers given there, based on \pdfpageattr and \afterpage from the afterpage package, with or without \BeforeBeginEnvironment and \AfterEndEnvironment from the etools package, or even modifying directly the definition of sidewaystable say, something like

\makeatletter
\renewenvironment{sidewaystable}
{\@rotfloat{table}%
\global\pdfpageattr\expandafter{/Rotate 90}}
{\afterpage{\global\pdfpageattr\expandafter{/Rotate 0}}%
\end@rotfloat}
\makeatother


Things always end up like in this screenshot:

Namely, not only the page with the actual sideways table is rotated, but also the page where the sidewaystable environment appears in the LaTeX source.

Of course, it's always possible to rotate pages manually in the PDF output, but it would be nice to have sidewaystable etc. rotate the pages automatically. I'm looking for a solution that only rotates the PDF page where the sideways table or figure is floated to, without otherwise altering the formatting in any way (namely, no use of \clearpage, \pagebreak or the landscape environment).

It depends a bit on the engine how easy it is. With engines using dvi you can use a \special, which is then executed at shipout. luatex has \latelua which can be used too. With pdflatex you have to use a \label/ref system to keep track of the right page. A possible implementation is here https://tex.stackexchange.com/a/472608/2388.

With the new pdfmanagement of LaTeX which is currently provided as a testphase package you can do this (depending on the engine it will require two compilations):

\RequirePackage{pdfmanagement-testphase}

\documentclass{article}
\usepackage{kantlipsum}
\usepackage{rotating}

\ExplSyntaxOn
\ExplSyntaxOff
\begin{document}
\kant[1]

\begin{table}[t]
\centering\fbox{xxxxxx}\caption{Table}
\end{table}

\kant[1-5]

\begin{sidewaystable}

\begin{tabular}{p{0.8\paperwidth}}
\hline
\kant[1-2]\\
\hline
\end{tabular}

\caption{large sideways table}
\end{sidewaystable}

\kant

\end{document}


Be aware that it is a testphase package and not yet compatible with everything.