# Flafter sometimes ignores a section title

I'm using the flafter package to prevent floats from appearing before their positions in the LaTeX source code.

When I put a float just after a section title and the section starts at the beginning of the page, sometimes the float appears above the section title, on the top of the page, while it should appear under the section title.

In the example below:

• by uncommenting paragraph2 the float will be under the section title (as expected);
• by commenting paragraph2 the float will be above the section title (at the top of the page).

I'd like to put the float under the section title in both cases.

\documentclass{article}
\usepackage{mwe}

\usepackage{flafter}

\begin{document}
\blindtext[4]

paragraph1

%paragraph2

\section{My section}
\begin{figure}[htb]
\includegraphics{example-image-a}
\end{figure}

\blindtext
\end{document}


The problem is actually independent of flafter, it happens even if you comment out the package altogether: as the section appears on page 2 why is the "h" float not after the section but on top of it?

The reason for this behavior is that a \section command attempts to keep the material following it on the same page. Therefore while LaTeX is still processing page 1 it looks at the \section and at the float following it and only then finally decides that the section doesn't fit on the first page.

However, by that time it has also concluded that the float doesn't fit either so it has been put on the deferlist. And from there it makes it way happily into the top area of the next page (in case of flafter because obviously (?), something that got deferred must be after its callout).

Guess that assumption is wrong so yes that is a bug or a deficiency in flafter but not sure that is something that can easily be fixed. In normal circumstances (i.e., when a float is placed between paragraphs this is not someting that would happen unless you set very very weird paragraph penalties (e.g., be favoring widow lines over finishing a paragraph). It only happens if LaTeX has look far ahead and ends up not using any material direcly in front of the float, which is the case with the section setup.

That looks like a potential bug, but as a workaround you can force the page break where the page break finally happened, before the section, then the float placement gets in sync:

\documentclass{article}
\usepackage{mwe}

\usepackage{flafter}

\begin{document}
\blindtext[4]

paragraph1

%paragraph2
\clearpage
\section{My section}

\begin{figure}[hbpt]
\includegraphics{example-image-a}
\end{figure}

\blindtext
\end{document}

• Thank you for your suggestion, however I would keep the document dynamic, I mean: by using this workaround, when I add something in the previous page, I would be forced to manually remove the page break. – cassiodoroVicinetti Jan 6 '15 at 15:48
• @cassiodoroVicinetti sure but it's a tricky area and no guarantee that a general fix is available in the timeframe of your document. (I pinged the rest of the latex team to look, so you might get lucky, but if you want the document finished this year, I'd add that \clearpage:-) – David Carlisle Jan 6 '15 at 15:50
• @cassiodoroVicinetti see what I mean: I give you the working document and then mail Frank, then you give him the tick for saying it's hard to fix:-) – David Carlisle Jan 6 '15 at 22:38
• Actually I also was thinking at putting a page break as a workaround before your answer. I don't want to say that your answer is useless, on the contrary I thank you, but Frank's answer not only says that it's hard to solve, but also provides a detailed explanation. I could give you the tick if you really want it because you're worried about your reputation points or kind of, but actually I prefer to know whether a thing can be done or not, and if not why it can not be done. – cassiodoroVicinetti Jan 6 '15 at 23:02
• @cassiodoroVicinetti no worries, I just thought it funny:-) I have known Frank over 20 years I doubt we'll really argue too much over this:-) – David Carlisle Jan 6 '15 at 23:10