[Tex/LaTex] Why this float in multicols is moved to next page while there is enough place on the current one

floatsmulticolpage-breaking

In the following MWE, the second float (within a multicols environment) is moved to page 4 while there is (seems to be) enough place where its code is given (on page 3). Why?

\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe}
\usepackage{dblfloatfix}
\usepackage{fixltx2e}
\usepackage{multicol}
%
\newcommand{\test}{%
  \lipsum[1-3]
  \begin{figure*}
    \centering
    \includegraphics[width=4cm]{example-image}
    \caption{Foo bar}
  \end{figure*}
  \lipsum[5-6]
}
%
\begin{document}
\section{A section}
\subsection{A First Subsection}
\begin{multicols}{2}%
\test
\end{multicols}
\subsection{A Second Subsection}
\begin{multicols}{2}%
\test
\end{multicols}
\end{document}

Best Answer

See Frank Mittelbach's answer to How to influence the position of float environments like figure and table in LaTeX?

Here's the relevant passage from that answer

Double-column floats are always deferred first
When LaTeX encounters a page-wide float environment (indicated by a * at the end of the environment name, e.g., figure*) in two column-mode, it immediately moves it to the deferred queue. The reason for this behavior again lies in the "greedy" behavior of its algorithm: if LaTeX is currently assembling the second column of that page, the first column has already been assembled and stored away; recall that because LaTeX does not back-track there is no way to fit the float on the current page. To keep the algorithm simple, it does the same even if working on the first column (where it could in theory do better even without back-tracking).

Thus, in order to place such a float onto the current page, one has to manually move it to an earlier place in the source -- before the start of the current page. If this is done, obviously any further change in the document could make this adjustment obsolete; hence, such adjustments are best done (if at all) only at the very last stage of document production --- when all material has been written and the focus is on fine-tuning the visual appearance.

(Emphasis in the second paragraph added by me).

Indeed, if I change your example and pull the figure* environment up, so the code appears when TeX is processing page 2, the float will go to the top of page 3.

\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe}
\usepackage{dblfloatfix}
\usepackage{fixltx2e}
\usepackage{multicol}

\begin{document}
\section{A section}
\subsection{A First Subsection}
\begin{multicols}{2}
\lipsum[1-3]

\begin{figure*}
\centering
\includegraphics[width=4cm]{example-image}
\caption{Foo bar}
\end{figure*}

\lipsum[5-6]
\end{multicols}

\subsection{A Second Subsection}
\begin{multicols}{2}

\begin{figure*}
\centering
\includegraphics[width=4cm]{example-image}
\caption{Foo bar}
\end{figure*}

\lipsum[1-3]
\lipsum[5-6]
\end{multicols}
\end{document}

enter image description here