Using the float package, I can use the H specifier and position a float exactly where I want it. For example,
\documentclass{article}
\usepackage{lipsum}
\usepackage{float}
\usepackage[demo]{graphicx}
\restylefloat{figure}
\begin{document}
\lipsum[1-3]
\begin{figure}[H]
\begin{center}
\includegraphics{foo}
\end{center}
\end{figure}
\lipsum[10-20]
\end{document}
Sometimes this will cause an unsightly page break. For example, if I changed the \lipsum[1-3] to \lipsum[1-4], the graphic will not fit on the page, so the page will break and the float will appear at the top of the next page. The amount of space at the end of the broken page is not desirable. A way to fix this is with the afterpage package as follows:
\documentclass{article}
\usepackage{lipsum}
\usepackage{afterpage}
\usepackage{float}
\usepackage[demo]{graphicx}
\restylefloat{figure}
\begin{document}
\lipsum[1-4]
\afterpage{\clearpage
\begin{figure}[H]
\begin{center}
\includegraphics{foo}
\end{center}
\end{figure}
}
\lipsum[10-20]
\end{document}
That puts the float at the top of the next page as before, but avoids the page break. In effect, the afterpage business tells LaTeX to place the graphic at the top of the next page, and nowhere else, but allows the first page to fill up completely.
What I would like is combination of these two tricks. In other words, I would like to tell LaTeX to put a float right here if you can do so without breaking the page. If you can't do that, then insert it at the top of the next page. In pseudo-code it might go something like this
if you-can-do-this-without-a-page-break then
\begin{figure}[H]
\begin{center}
\includegraphics{foo}
\end{center}
\end{figure}
else:
\afterpage{\clearpage
\begin{figure}[H]
\begin{center}
\includegraphics{foo}
\end{center}
\end{figure}
}
It is a simple conditional, but I don't know how to determine whether the page break will occur. Presumably, it would require calculating the box size of the graphic and then seeing if that box size fill fit comfortably on the page if it were inserted at the requested position.
HI am deliberately disrupting LaTeX's freedom to float. I am doing this because, as it says in the documentation of thefloatpackage, "Many people find LaTeX's float placement specifiers too restrictive. A Commonly Uttered Complaint (CUC) calls for a way to place a float exactly at the spot where it occurs in the input file, i.e., to not have it float at all." Sometimes I do not want LaTeX to make the float decisions because sometimes I don't like the results. – mjandrews Jul 08 '14 at 07:19H. See http://tex.stackexchange.com/q/39017/15925 – Andrew Swann Jul 08 '14 at 07:27\begin{figure}[!htp]supposed to do what you're asking for? – egreg Jul 08 '14 at 08:27[htp]should do what you want. If it does not, post an example and perhaps someone could suggest a setting of the float placement parameters that does do what you want. – David Carlisle Jul 08 '14 at 08:52[htp]or[!htp]does the trick, I will happily stand corrected. – mjandrews Jul 08 '14 at 09:38