0

I'm working in a text with a lot of figures and some of them are landscape oriented. In the text I created a landscape figure, followed by some paragraphs and then a regular figure. I expected that the landscape oriented figure appears first, and then the regular figure, however it is not happening. How can I place the landscape figure first than the other one? I have aready tested the afterpage command

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[%
    left=3.00cm,
    right=2.00cm,
    top=3.00cm,
    bottom=2.00cm
]{geometry}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{pdflscape}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{afterpage}
\begin{document}
\blindtext\par
\blindtext\par
\blindtext\par
\afterpage{%
\begin{landscape}%<--- Created first
\begin{figure}
\centering
\includegraphics[height=12cm]{example-image-a}
\caption{Example image a}
\end{figure}
\end{landscape}
}
\blindtext\par
\blindtext\par
\begin{figure}[htb]%<--- Created second
\centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{Example image b}
\end{figure}
\blindtext\par
\end{document}

enter image description here

EDIT 1:

I used the @JohnKormylo's suggestion and it worked. However, the figure numbers are not in the correct order. How can I solve it?

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[%
    left=3.00cm,
    right=2.00cm,
    top=3.00cm,
    bottom=2.00cm
]{geometry}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{pdflscape}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{afterpage}
\makeatletter%<---@JohnKormylo's suggestion
\newcommand{\savefloats}{%
  \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}
  \xdef\old@toproom{\the\@toproom}
  \xdef\old@botroom{\the\@botroom}
  \xdef\old@topnum{\the\@topnum}
  \xdef\old@botnum{\the\@botnum}
}
\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}
\blindtext\par
\blindtext\par
\blindtext\par
\afterpage{\savefloats%<--- Solution
\begin{landscape}%<--- Created first
\begin{figure}
\centering
\includegraphics[height=12cm]{example-image-a}
\caption{Example image a (must be Figure 1)}
\end{figure}
\end{landscape}
\restorefloats}%<--- Solution
\blindtext\par
\blindtext\par
\begin{figure}[htb]%<--- Created second
\centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{Example image b (must be Figure 2)}
\end{figure}
\blindtext\par
\end{document}

enter image description here

7
  • If you throw in one more \blindtext, or anything to force the portrait figure to not be created until AFTER the landcape page, it will go second. \begin{landscape} performs \clearpage which will print any floats in the queue immediately. Oct 18, 2017 at 15:37
  • latex always keeps figures in the order they are entered, but you have used \afterpage to change the order. Perhaps you are looking for sidewaysfigure from the rotating package? Oct 18, 2017 at 15:52
  • @DavidCarlisle sidewasyfigure is perfect for what I need. However I've been tryed with landscape environment because the pdf rotated page. It is a dilemma, float the figure or pretty pdf page orientation Oct 18, 2017 at 15:55
  • 1
  • @JohnKormylo It worked!!! Oct 18, 2017 at 16:57

2 Answers 2

2

Floats are always kept in order that they are executed, but \afterpage just stores its content as tokens so the landscape figure is seen after the normal one if you do it that way. (\afterpage is very fragile you should only use it as a last resort)

You can use sidewaysfigure to have rotaated figures that float as normal.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[%
    left=3.00cm,
    right=2.00cm,
    top=3.00cm,
    bottom=2.00cm
]{geometry}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{rotating}
\usepackage{blindtext}
\begin{document}
\blindtext\par
\blindtext\par
\blindtext\par

\begin{sidewaysfigure}
\centering
\includegraphics[height=12cm]{example-image-a}
\caption{Example image a}
\end{sidewaysfigure}

\blindtext\par
\blindtext\par
\begin{figure}[htbp]%<--- Created second (don't forget p :-)
\centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{Example image b}
\end{figure}
\blindtext\par
\end{document}
3
  • sidewaysfigure is perfect, so how can I rotate the pdf page like landscape does? Oct 18, 2017 at 16:00
  • 2
    @DanielValenciaC. well you can't really (or at least sideways figure doesn't) as lscape knows it is dealing with landcape pages but sideways figure just rotates the figure. Your example is large so it takes the whole page but in general you might have a small sideways figure appearing (rotated) at the top of a normal portrait page. sidewaysfigure just rotates the contents of the float it does not know where the float will be positioned (and the latex float algorithm doesn't know the contents are rotated) Oct 18, 2017 at 16:15
  • One can rotate single pages with the pdflscape package: \makeatletter\PLS@Rotate{0}abc\newpage \PLS@Rotate{90} abc\newpage \PLS@Rotate{0} abc works fine. With floats the main problem is how to do it on the correct page and how to stop it (it can not be grouped). Oct 19, 2017 at 13:02
1

I combined the \savefloats and \restorefloats from here with the \savebox from earlier.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[%
    left=3.00cm,
    right=2.00cm,
    top=3.00cm,
    bottom=2.00cm
]{geometry}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{pdflscape}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{afterpage}

\makeatletter
\newcommand{\setcaptype}[1]{\def\@captype{#1}}

\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

\newsavebox{\lsfigure}

\begin{document}
\blindtext\par
\blindtext\par
\blindtext\par
\savebox{\lsfigure}{% increment figure/caption counter
  \begin{minipage}{\textheight}
  \setcaptype{figure}\centering
  \includegraphics[height=12cm]{example-image-a}
  \caption{Example image a}
  \end{minipage}}%
\afterpage{\savefloats
  \begin{landscape}%<--- Created first
  \begin{figure}
  \usebox{\lsfigure}
  \end{figure}
  \end{landscape}
  \restorefloats}%
\blindtext\par
\blindtext\par
\begin{figure}[htb]%<--- Created second
\centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{Example image b}
\end{figure}
\blindtext\par
\end{document}

You must log in to answer this question.

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