[Tex/LaTex] What to do if \clearpage does not flush the floats exactly

formattingpage-breaking

Consider this MWE:

\documentclass[%
12pt,
journal,
onecolumn,
twoside,
draftcls,
letterpaper,
]{IEEEtran}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{adjustbox}
\usepackage{setspace} %\singlespacing
\usepackage[nopar]{lipsum}
\usepackage{xstring}
\usepackage{afterpage}

\begin{document}
\lipsum[1]

\lipsum[2]

\lipsum[3]

\lipsum[4]

\clearpage % \newpage % no effect %[1]
% \afterpage{\clearpage} % has effect, but page 1 doesn't end with [4], starts also \lipsum[5] %[2]

\begin{figure}[hbt]
\centering
\begin{tabular}{p{0.5\textwidth} p{0.5\textwidth}}
  \vspace{0pt} \includegraphics[width=0.41\textwidth]{pic1.pdf} &
  \vspace{0pt} \includegraphics[width=0.41\textwidth]{pic1.pdf}
\end{tabular}
\caption[hda-beh]{Something something, something, something something;
(some) something and something something (thing); some thing:
something something, something something}
\label{fig:something}
\end{figure}

\lipsum[5]

\lipsum[6]

\end{document}

… where I'm using this pic1.pdf (an illustration I've had, with most content removed).

I'm using \clearpage because I want the page to end after the fourth paragraph; then I'd expect the floats to be on page 2, and I'd expect the text to continue from page 3. However, with the above code as is (choice %[1]), I actually get this (click on thumbnails for full size):

test10
test11
test12

… that is, unexpectedly, text is placed on page 2, and the floats on page 3.

If I use \afterpage{\clearpage} (that is, I uncomment %[2] and comment %[1]), I get this instead:

test20
test21
test22

… that is, now floats are on page 2, as expected – but page 1 does not end with paragraph 4, instead it includes a bit of paragraph 5 (circled in red), which I don't want!

So how do I tell latex: "end the page HERE, flush floats on next page, and start with text on the page after that"?

Best Answer

Put the \clearpage after the \end{figure} rather than before the \begin{figure}.

What's happening is that LaTeX treats figures and tables as floats. It sees that your figure needs to be on a page by itself, so it adds that figure to the queue of floats that need to be processed later. That "processed later" in this case is the end of the document.

You want to tell LaTeX, "No! Do it now!" So do that -- just after the figure has been defined. All that that \clearpage before you defined the figure accomplished was to start a new page. There were no unprocessed floats in the queue at the time it hit your \clearpage.