32

I got a Latex document which renders fine, some content is rotated using the turn environment from the rotate package. Everything works like it should.

Except: It know that it is possible to display a single page in a pdf in another orientation. The document viewer then rotates this page when viewing. This would help the readers of my document to read the turned text.

How is this possible? Perhaps somehow using the pdfpageattr command?

Again: I do not to want to rotate the content, I just want to tell the PDF-Reader that it should rotate the page when displaying.

3
  • 1
    You could have a look at how pdflscape does it. Commented Jan 10, 2012 at 19:53
  • I already tried that, but I can not read Latex Module Code so I do not understand what happens there.
    – theomega
    Commented Jan 10, 2012 at 20:04
  • 2
    So you want a series of regular (portrait) pages, and then a rotated (landscape) page, and then some more regular (portrait) pages, right? Is it possible to completely separate the content on the landscape page? That is, is it (say) a page-sized table or figure, or does the landscape page form part of the regular text flow, but you just want it rotated?
    – Werner
    Commented Jan 10, 2012 at 20:29

4 Answers 4

18

You can indeed use the pdfpageattr command. You can do it as follows:

\documentclass{article}
\begin{document}
  text

  \pagebreak[4]
  \global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}
  text
\end{document}

This will rotate the second page by 90 degrees. It should be obvious how to get different rotations.

6
  • If I change the important line with the folowing: \global\pdfpageattr\expandafter{/Rotate 90}, it also work. Why?
    – Azoun
    Commented Jan 10, 2012 at 21:03
  • then the current attributes won't be saved. Which is usually not a problem, unless you have some other attributes set already. Commented Jan 11, 2012 at 0:09
  • 7
    Could you please show how to do this for a single page, i.e. how to change it back to the default after one page. This would improve the answer very much. I would recommend to use afterpage. Commented Apr 24, 2012 at 10:32
  • 3
    @MartinScharrer It's been a while and your suggestion hasn't been included in this answer. I landed on this question wanting to know how to do exactly what you describe! Would you be able to expand on your suggestion with an edit, or a separate answer?
    – joran
    Commented Sep 12, 2012 at 18:29
  • 1
    +1, and to rotate back I just added the same thing afterward but with Rotate 0 instead of Rotate 90.
    – spdickson
    Commented Mar 7, 2014 at 15:54
18

The accepted answer did not work for me, since it rotated all pages in the PDF instead of a single one. I managed to get it to work using the pdflscape package instead, as suggested in this answer:

\usepackage{pdflscape}
...
\begin{landscape}
...
\end{landscape}

I needed this to display figures. Note that, when inside the landscape environment, you must use \linewidth for a figure's width, \textwidth will not work.

2
  • The pdfpageattr didn't work for me either. pdflscape works fine.
    – Yifeng Mu
    Commented Feb 9, 2018 at 2:56
  • 1
    upvote for the \linewidth clarification! Commented Apr 7, 2020 at 20:43
13

A slightly more complete version to the accepted answer would be to create an environment that turns the page back to portrait:

\newenvironment{rotatepage}%
    {\clearpage\pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}}%
    {\clearpage\pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 0}}%

And then \begin{rotatepage} <the content on this page is turned 90°> \end{rotatepage}.


But we can extend this further. We might want to use the features provided by the rotating package. And we might use documents that use different margins for odd and even pages. Then an automated solution that turns the page by 90° or 270° respectively would be nice. An example using KOMA script:

\documentclass[twoside]{scrreprt}
\usepackage{scrextend,rotating}

\makeatletter
\newenvironment{rotatepage}
        {%
            \if@twoside%
                \ifthispageodd{\pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}}{%
                \pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 270}}%
            \else%
                \pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}%
            \fi%
        }%
        {\pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 0}}%

\makeatother

And then \begin{rotatepage}\begin{sidewaystable} <the content on this page is turned 90°> \end{sidewaystable}\end{rotatepage}


Finally, we can combine rotatepage and the environments provided by the rotating package. If we want to turn pages automatically that use sidewaytable, we can hook the rotatepage environment into sidewaytable with etoolbox. In addition to the code above we would need:

\BeforeBeginEnvironment{sidewaystable}{\begin{rotatepage}}
\AfterEndEnvironment{sidewaystable}{\end{rotatepage}}

Of course, similar adjustments could be made for sidewaysfigure etc.

3
  • This is a good answer, but the afterpage solution ended up being simpler for me - linking for visibility because this question comes up more often in search: tex.stackexchange.com/a/433414/77706
    – Ross
    Commented Dec 8, 2018 at 21:23
  • 2
    I suggest to add \clearpage so that the environment does not affect multiple pages. \newenvironment{rotatepage}% {\clearpage\pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}}% {\clearpage\pagebreak[4]\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 0}}%
    – robust
    Commented Nov 21, 2019 at 0:44
  • Unfortunately this does not seem to work with twosided report as it rotates the page somewhat randomly.
    – Hojin Cho
    Commented Aug 5 at 4:27
2

In a comment to Roelof Spijker's answer Martin Scharrer requested the use of the afterpage package to rotate the following pages back again:

\documentclass[twoside]{report}
\usepackage{afterpage}
\begin{document}
text
\makeatletter%
\if@twoside%
  \cleardoublepage%
  \global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}%
  \afterpage{\afterpage{\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 0}}}%
\else%
  \clearpage%
  \global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}%
  \afterpage{\global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 0}}%
\fi%
\makeatother%
text
\newpage
text
\newpage
text
\end{document}

Creating an environment (answer of Jörg) seems to be the better solution imho, thus this answer is just for demonstrative purpose (but might be helpful in another context).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .