Is there a command which does the same like \clearpage (typesetting all floats which have not been typeset yet) but then does not insert a pagebreak but continues with the following text/graphics on the same page?
- 603,163
- 12,160
4 Answers
I believe you may be after the \FloatBarrier command from the placeins package. It forces Tex to typeset all remaining floats at that point and doesn't include a \clearpage afterwards.
- 5,702
Yes, and this is discussed as one of the advantages of the afterpage package. Use
\afterpage{\clearpage}
This will flush all processed floats after the current page has been completely set, technically avoiding a visible \clearpage. See the first use-case in the afterpage documentation (page 1):
Sometimes LaTeX's float positioning mechanism gets overloaded, and all floating figures and tables drift to the end of the document. One may flush out all the unprocessed floats by issuing a
\clearpagecommand, but this has the effect of making the current page end prematurely. Now you can issue\afterpage{\clearpage}and the current page will be filled up with text as usual, but then a\clearpagecommand will flush out all the floats before the next text page begins.
- 603,163
-
6But \afterpage still issues a \clearpage. At least that's the case for me, and that's also how i understand the documentation. – Janos Aug 12 '14 at 20:41
-
8Unfortunately, didn't work as desired. @philipp described it below: if a new section starts in the middle of the page, the floats from that new section will still appear above the section title. The solution with
\FloatBarriersolution did work as desired. – akhmed May 14 '15 at 00:02
I made a MWE to compare different solutions.
Code
\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{afterpage}
\usepackage{placeins}
\begin{document}
\section{foo}
\lipsum[1-2]
\begin{figure}[htb]
\includegraphics[width=\textwidth, height=40mm]{example-image-a}
\end{figure}
\lipsum[4]
\begin{figure}[htb]
\includegraphics[width=\textwidth, height=\textheight]{example-image-b}
\end{figure}
%\clearpage % (variant 1)
%\afterpage{\clearpage} % (variant 2)
%\FloatBarrier % (variant 3)
%\afterpage{\FloatBarrier} % (variant 4)
\lipsum[3]
\section{bar}
\lipsum[5-7]
\end{document}
Result
Variant 0: nothing
- Image B is moved to the end.
Variant 1: \clearpage
- Image B is placed before anything after
\clearpage.
Variant 2 \afterpage{\clearpage}
(see answer by Werner)
- Image B is placed after the current page ends.
Variant 3: \FloatBarrier
(see answer by Max)
Note, in this specific example variant 3 behaves as variant 1, but that's not the case in general (see example below).
Variant 4: \afterpage{\FloatBarrier}
(see answer by stefanct)
Note, in this specific example variant 4 behaves as variant 2, but that's not the case in general (see example below).
Difference between \clearpage and \FloatBarrier
Another example without Fig. B.
Code
\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{afterpage}
\usepackage{placeins}
\begin{document}
\section{foo}
\lipsum[1-2]
\begin{figure}[htb]
\includegraphics[width=\textwidth, height=40mm]{example-image-a}
\end{figure}
\lipsum[4]
\clearpage % (variant 1)
%\FloatBarrier % (variant 3)
\section{bar}
\lipsum[5-7]
\end{document}
Result
Variant 1: \clearpage
Variant 3: \FloatBarrier
\FloatBarrierdoes not insert a page break if there are no floats to insert.
- 9,161
I had success by combining the two previous answers, i.e., \afterpage{\FloatBarrier} (to be used directly after the float, maybe even within) in a case where a page-size table was pushed at the very end of an acmart document. This was inspired by a completely different question. Naturally this requires both packages (placeins and afterpage). The alternatives have not - for some reason - not produced nice/acceptable results.
- 841
- 6
- 16







texlive-latex-extrato useplaceinspackage since it is not part of the standardtexlive-latex-recommended. – akhmed May 14 '15 at 00:07\FloatBarrierinkpsewhich placeins.stycan in fact issue a\clearpagebut only if "Some floats are stuck,"; else it seems to issue a\newpage. – sdaau Jun 17 '15 at 10:26