0

I'd like to have landscape (tcolorbox) floats, and the naive way of doing them with afterpage and pdflscape does not really work. While I did get the result I wanted for print, I would also like to have the landscape pages rotated in the PDF just like with pdflscape. Since all my landscape figures will be on float pages, I use \iffloatpage, but this is not enough because I have other portrait figures that should not be rotated.

The only solution I can see would be adding a ref for each landscape float page, and then at the beginning of each page, check if the page of the ref is the current page, and if so, rotate the page. But that seems very complicated. Surely, there is a simpler way of knowing whether the float is a landscape float or not.

Another potential solution would be to simply assume that all landscape floats will be immediately after the current page, i.e. store the current page number in a counter, and at the beginning of each new page, rotate the page and decrement the counter, until the counter reaches 0. The problem is that if there are already portrait floats waiting, this might not rotate the right pages.

\documentclass[10pt,a4paper]{scrarticle}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage[most]{tcolorbox}
\usepackage{atbegshi}
\usepackage{fancyhdr}
\begin{document}

\NewDocumentCommand{\setpdfrotation}{m}{ \global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate #1} }

\tcbset{ enhanced, breakable }

\tcbset{ landscapefloat/.style={ float=p, width=\textheight, tikz={rotate=90,transform shape}, break at=\textwidth } }

\AtBeginShipout{% \AtBeginShipoutAddToBox{% \iffloatpage{\setpdfrotation{90}}{}% }% }

\begin{tcolorbox}[title={Landscape figure}, landscapefloat]% \textcolor{yellow}{\rule{15cm}{5cm}}\ \textcolor{orange}{\rule{15cm}{5cm}}\ \textcolor{red}{\rule{15cm}{5cm}}\ \textcolor{purple}{\rule{15cm}{5cm}}\ \textcolor{blue}{\rule{15cm}{5cm}}\ \textcolor{green}{\rule{15cm}{5cm}} \end{tcolorbox}

Some text

\begin{tcolorbox}[title={Portrait figure}, float=p]% This should not be rotated. \end{tcolorbox} \end{document}

xavierm02
  • 787

2 Answers2

1

With the pdfmanagement (that you load with \DocumentMetadata) you can place a rotation "on this page". I use the watermark key to smuggle it on every part of your box:

\DocumentMetadata{}
\documentclass[10pt,a4paper]{scrarticle}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{fancyhdr}

\ExplSyntaxOn \NewDocumentCommand{\setpdfrotation}{}{\pdfmanagement_add:nnn{ThisPage}{Rotate}{90}} \ExplSyntaxOff \begin{document}

\tcbset{ enhanced, breakable }

\tcbset{ landscapefloat/.style={ float=p, width=\textheight, tikz={rotate=90,transform shape}, break at=\textwidth, watermark text={\setpdfrotation} } }

\begin{tcolorbox}[title={Landscape figure}, landscapefloat]%
\textcolor{yellow}{\rule{15cm}{5cm}}\ \textcolor{orange}{\rule{15cm}{5cm}}\ \textcolor{red}{\rule{15cm}{5cm}}\ \textcolor{purple}{\rule{15cm}{5cm}}\ \textcolor{blue}{\rule{15cm}{5cm}}\ \textcolor{green}{\rule{15cm}{5cm}} \end{tcolorbox}

Some text

\begin{tcolorbox}[title={Portrait figure}, float=p]% This should not be rotated. \end{tcolorbox} \end{document}

Ulrike Fischer
  • 327,261
  • This is perfect. Thank you! (Note to readers: this seems to still require two compilations) – xavierm02 May 31 '23 at 22:45
  • (Another note: the watermark text seems to disable overlays, but this can be fixed by replacing it with overlay app) – xavierm02 Jun 01 '23 at 09:07
  • yes sorry I hadn't much time to find a good key to add something on everypage (as a label is writting it must be somewhere where typesetting (whatsits) don't harm). – Ulrike Fischer Jun 01 '23 at 09:10
  • You have already been immensely helpful, and could not know that I had an overlay since I did not include it in my question. I just added these comments to help other people that will use this code. Thanks again! – xavierm02 Jun 01 '23 at 10:03
0

Using this answer by Ulrike Fischer, I got something that seems to work:

\documentclass[10pt,a4paper]{scrarticle}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{fancyhdr}
\usepackage{pdflscape}
\usepackage{eso-pic,zref-user}
\begin{document}

\newcounter{cntsideways}

\makeatletter

\newcommand\rotatesidewayslabel{\stepcounter{cntsideways}% \zlabel{tmp\thecntsideways}\zlabel{rotate\zref@extractdefault{tmp\thecntsideways}{page}{0}}}

\AddToShipoutPictureBG{% \ifnum\zref@extractdefault{rotate\number\value{page}}{page}{0}=0% \PLS@RemoveRotate% \else % \PLS@AddRotate{90}% \fi% } \makeatother

\tcbset{ enhanced, breakable }

\tcbset{ landscapefloat/.style={ float=p, width=\textheight, tikz={rotate=90,transform shape}, break at=\textwidth, every float={\rotatesidewayslabel} } }

\begin{tcolorbox}[title={Landscape figure}, landscapefloat]% \textcolor{yellow}{\rule{15cm}{5cm}}\ \textcolor{orange}{\rule{15cm}{5cm}}\ \textcolor{red}{\rule{15cm}{5cm}}\ \textcolor{purple}{\rule{15cm}{5cm}}\ \textcolor{blue}{\rule{15cm}{5cm}}\ \textcolor{green}{\rule{15cm}{5cm}} \end{tcolorbox}

Some text

\begin{tcolorbox}[title={Portrait figure}, float=p]% This should not be rotated. \end{tcolorbox} \end{document}

xavierm02
  • 787