2

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.

  • Sometimes this will cause an unsightly page break. This is precisely why floats are designed to, well, float! – Paul Gessler Jul 8 '14 at 3:16
  • @PaulGessler Indeed. And by using H I am deliberately disrupting LaTeX's freedom to float. I am doing this because, as it says in the documentation of the float package, "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 8 '14 at 7:19
  • 1
    It is usually best to understand latex's float parameters and tweak them rather than using H. See tex.stackexchange.com/q/39017/15925 – Andrew Swann Jul 8 '14 at 7:27
  • 1
    Isn't \begin{figure}[!htp] supposed to do what you're asking for? – egreg Jul 8 '14 at 8:27
  • @AndrewSwann Modifying the float parameters sometimes is sufficient but in my experience so far sometimes no amount of tweaking these parameters ever fully satisfies. I often write short articles with lots of maths and lots graphics, and particularly in these case allowing LaTeX to make the float decisions can sometimes lead to unimpressive results. I sometimes end up not floating them and manually positioning them. There are obvious drawbacks to this. – mjandrews Jul 8 '14 at 8:37
2

On the advice of some of the comments I received, I took a proper look at whether simply doing something like \begin{figure}[!ht] would produce this desired effect. From experimenting with different floats and different positions. I am a little embarrassed to say that, yes, it seems like that is all that was needed after all. Maybe it won't suffice always --- I can't check all possible scenarios --- but it certainly looks like it will work most of the time.

In that case, my original question may have been pointless. It is probably difficult to do exactly what I wanted (i.e. evaluating the conditional above), and there may no reason to so anyway given that LaTeX's normal float mechanism will produce the desired effect.

  • As @egreg mentions above, \begin{figure}[!htp] seems to have the behaviour you want. – Roly May 4 '15 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.