# How to get rotated landscape page in LaTeX without flushing other floats?

I would like the landscape tables to appear rotated in the pdf using pdflscape package. However I would still like the text AND figures to float around the table.

As you can see in the portrait view (with afterpage and landscape environments turned off). The 'figure' is correctly placed at the top of page 3. When the afterpage and landscape are turned on the 'table' is correct but the 'figure' is placed BEFORE the table on a separate page.

Is it possible to get the 'figure' on top of page 3 with a rotated-page 'table' on page 2?

Additionally, If the first section of text was smaller I would like the 'figure' float to then be placed at the bottom of page 1.

Portrait with correct placement of 'figure'. Landscape with rotated 'table' but incorrect placement of 'figure' on p2 instead of top of p3.

MWE:

\documentclass[a4paper,twoside,12pt]{report}

\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{lipsum} %for dummy text

\begin{document}

\lipsum[1-4]

\begin{figure}[htb]
Testfigure \\
Testfigure \\
Testfigure \\
Testfigure \\
\caption{Figure caption}
\end{figure}

%\afterpage{         %turned off to demonstrate portrait pages
%\begin{landscape}   %turned off to demonstrate portrait pages
\begin{table}
\caption{Table Caption}
\begin{tabular}{|p{5cm}|p{9cm}|c}
4&5&6 \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F
\end{tabular}
\end{table}
%\end{landscape}    %turned off to demonstrate portrait pages
%}                  %turned off to demonstrate portrait pages

\lipsum[5-10]

\end{document}


The reason there is a \clearpage at the beginning and end of the landscape environment is that the dimensions of floats also change. You might wind up with floats too tall or too wide to print.

However, it is possible to save all the floats in the queue, then restore them once the geometry returns to normal. The first \clearpage will find nothing to do, and the second \clearpage will empty the queue again, so nothing is lost when the old queue is restored.

\documentclass[a4paper,twoside,12pt]{report}

\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{lipsum} %for dummy text

\makeatletter
\newcommand{\savefloats}{% this is probably overkill, but better safe than sorry
\global\let\old@toplist=\@toplist
\global\let\old@midlist=\@midlist
\global\let\old@botlist=\@botlist
\global\let\old@deferlist=\@deferlist
\global\let\old@dbltoplist=\@dbldtoplist
\global\let\old@dbldeferlist=\@dbldeferlist
\global\let\@toplist=\@empty
\global\let\@midlist=\@empty
\global\let\@botlist=\@empty
\global\let\@deferlist=\@empty
\global\let\@dbltoplist=\@empty
\global\let\@dbldeferlist=\@empty
\xdef\old@colroom{\the\@colroom}% store as macros
\xdef\old@toproom{\the\@toproom}% store as macros
\xdef\old@botroom{\the\@botroom}% store as macros
\xdef\old@topnum{\the\@topnum}% store as macros
\xdef\old@botnum{\the\@botnum}% store as macros
}
\newcommand{\restorefloats}{%
\global\let\@toplist=\old@toplist
\global\let\@midlist=\old@midlist
\global\let\@botlist=\old@botlist
\global\let\@deferlist=\old@deferlist
\global\let\@dbltoplist=\old@dbltoplist
\global\let\@dbldeferlist=\old@dbldeferlilst
\global\@colroom=\old@colroom\relax
\global\@toproom=\old@toproom\relax
\global\@botroom=\old@botroom\relax
\global\@topnum=\old@topnum\relax
\global\@botnum=\old@botnum\relax
\global\vsize=\@colroom
}
\makeatother

\begin{document}

\lipsum[1-4]

\begin{figure}[htb]
Testfigure \\
Testfigure \\
Testfigure \\
Testfigure \\
\caption{Figure caption}
\end{figure}

\afterpage{         %turned off to demonstrate portrait pages
\savefloats
\begin{landscape}   %turned off to demonstrate portrait pages
\begin{table}[p]
\caption{Table Caption}
\begin{tabular}{|p{5cm}|p{9cm}|c}
4&5&6 \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F \\
D&E&F
\end{tabular}
\end{table}
\end{landscape}    %turned off to demonstrate portrait pages
\restorefloats
}                 %turned off to demonstrate portrait pages

\lipsum[5-10]

\end{document}

• This does work, however if the first block of text \lipsum[1-4] was shorter (e.g. replaced by \lipsum[1-2]) the figure does not appear at the bottom of page 1... This behaviour does happen in portrait (without afterpage/pdflscape packages). – Martin Feb 23 '16 at 20:11
• Basically this section will come in the middle of a large chapter and I don't yet know if the figure will come first or second but I would like both to float before the next section of text starts. For some reason with afterpage package only the text refills correctly not other figures... – Martin Feb 23 '16 at 20:15
• I think I figured out what you are trying to achieve. – John Kormylo Feb 24 '16 at 3:40
• Almost that works almost perfectly... There is one last problem that the following text on page 3 does not take into account the height of the figure inserted. So if you look at the bottom of the page it runs into the page number and below. Is there any fix to shorten the height so that the text does not run into the bottom margin? – Martin Feb 25 '16 at 16:09
• The key was reducing \vsize. For some reason \@colroom was being ignored. – John Kormylo Feb 25 '16 at 19:41