1

Let's assume you have a huge book with lots of floating and fixed-position figures:

\documentclass{svmono}% V5.10 from https://resource-cms.springernature.com/springer-cms/rest/v1/content/20566/data/monographs .  For testing purposes, I hope, book would do as well.
…
\usepackage{float}
…
\begin{document}
…
\begin{figure}[h]% or anything not including H
  Figure 1. A floating figure
\end{figure}
…
\begin{figure}[H]
  Figure 2. A fixed-position figure
\end{figure}
…
\begin{figure}[h]% or anything not including H
  Figure 3. Another floating figure
\end{figure}
…
\end{document}

Will these two different kinds of figures always appear in the DVI or PDF output in the same order as in the LaTeX input? (In the example above, the wished order is 1,2,3. The order 2,1,3 or 1,3,2 in the output would be bad.) Or do you need to take special precautions so that the order is actually retained? The documentation in float.pdf, dated 2001/11/08, says nothing about this order, or at least, not explicitly. (So far, the order has been maintained for my examples, but, perhaps, I was simply lucky.)

By the way, is there any real difference in the placement between [h] and [ht]? And how about [!h] vs. [!ht]? In the log of the large book I edit, I get warnings that [h] has been changed to [ht] (and [!h] to [!ht], I think).

  • not including p makes floats going to the end more or less inevitable – David Carlisle Aug 20 '22 at 00:02
  • Generally, [!] makes no difference. The exceptions are if you have more than 3 (topnumber) [t] floats, or more than 1 (bottomnumber) [b] floats, or more than 8 (totalnumber) [htb] floats. – John Kormylo Aug 20 '22 at 02:22
  • @JohnKormylo it can also relax the amount of text required to be on the page with t and b floats. – Skillmon Aug 20 '22 at 07:30
  • I see no reason why you want to avoid p floats (that makes it impossible to place any float bigger than 70% of a page, and very hard to place any floats) but if you want something like H that keeps floats in order see https://tex.stackexchange.com/questions/246155/how-to-place-floats-between-paragraphs/246315#246315 – David Carlisle Aug 20 '22 at 08:28
  • @Skillmon - I stand corrected. Interestingly, the largest float allowed using [htb!] is \textheight-2\floatsep. – John Kormylo Aug 20 '22 at 14:20
  • @DavidCarlisle Thx! I was unaware of 70%. In my input (huge book), the floats are NOT hard to place in general. After some effort, they stopped going to the end. However, they are indeed hard to place where I like them to be (e.g., figures logically belonging to an example block should go between „Example 1.2.3“ and the symbol terminating it, which is ■ in my case). I guess, I might ask an extra question on this. Thanks for the link! –  Aug 20 '22 at 17:03
  • @JohnKormylo Thanks! I always thought that the exclamation mark ! applies to the symbol immediately following it and insists on the corresponding placement behavior. That's why I always wrote [!h] and [!ht] (other examples of the exaclamation mark before the placement symbols: https://de.overleaf.com/learn/latex/Errors/%60!h%27_float_specifier_changed_to_%60!ht%27 ). Now that I see your [htb!], I get puzzled: which of the three placement letters does this insist on compared to [htb]? –  Aug 20 '22 at 17:06
  • @GeekestGeek the order of letters in the option makes no difference at all, each letter is a power of 2 and they are added together as an integer – David Carlisle Aug 20 '22 at 17:10
  • @DavidCarlisle So, e.g., [ht] = [th] and [!ht]=[!th]=[h!t]=[t!h]=[ht!]=[th!] ? Or is the position of the exclamation mark treated differently from the position of an alphabetical letter? –  Aug 20 '22 at 17:13
  • not including p is a explicit instruction not to place the float on a float page, so if it is too large to go on a text page with text it can not be placed anywhere, and will drift until flushed out by \clearpage – David Carlisle Aug 20 '22 at 17:15
  • 1
    The option used by a float is stored as an integer the letters are just a bitmask, using ! anywhere adds 16 i e sets bit 4 – David Carlisle Aug 20 '22 at 17:19
  • @DavidCarlisle Got it. Thanks!!!! –  Aug 20 '22 at 17:20
  • @DavidCarlisle I extended the question, trying to use htbp instead of ht. I also tried \FloatBarrier, \afterpage{\clearpage}, or \flushhere. Do you think that using \FloatBarrier\begin{figure}[H] guarantees that the order of all the Figures is retained? If so, is there any way to improve the second example and allow Figure 3 to float a bit upwards (but not below the halmos)? –  Aug 20 '22 at 18:33
  • yes of course \FloatBarrier\begin{figure}[H] preserves the order, but you shouldn't change the question so much after answers posted, espcially after you accepted it. It makes it hard for future readers to understand – David Carlisle Aug 20 '22 at 18:44
  • @DavidCarlisle Alright, thanks! I reverted and posted a new question: https://tex.stackexchange.com/q/654666/ –  Aug 20 '22 at 19:32
  • Somewhat related: https://tex.stackexchange.com/questions/645610/find-out-if-a-figure-is-positioned-h-t-b-or-p – John Kormylo Aug 20 '22 at 20:44

1 Answers1

3

On the order question

No, that's not guaranteed, consider

\documentclass{article}

\usepackage{float} \usepackage[paperheight=6cm]{geometry}

\begin{document} \begin{figure}[b]Order.\caption{1}\end{figure} \begin{figure}[H]ORDER!\caption{2}\end{figure} \end{document}

which produces

enter image description here

Or something maybe a bit closer to some real document:

\documentclass{article}

\usepackage{float} \usepackage[paperheight=6cm]{geometry}

\usepackage{duckuments}

\begin{document} \blindduck\par \begin{figure}Order.\par Order.\par Order.\caption{1}\end{figure} \begin{figure}[H]ORDER!\caption{2}\end{figure} \end{document}

On the h vs ht question

They result in the same as LaTeX doesn't use just h, but ht if you specify just h (the ! makes no difference for this). But using only ht is a bad idea (especially with large floats) as it doesn't give LaTeX much choice where it can place the floats, I'd suggest also allowing p positioning, so better htp instead.

An example why only using ht is a bad idea

In the following example the first float is too big to be placed as either h or t, hence flushing all figures to the end of the document. If you add a p to the first floats possible placements things work out much better and floats stay close to where they are put in the source (well, closer at least).

\documentclass{article}

\usepackage{duckuments}

\begin{document} \blindduck \begin{figure}[ht]\rule{5cm}{.8\textheight}\caption{big}\end{figure} \blindduck \begin{figure}[ht]\rule{3cm}{2cm}\caption{small}\end{figure} \blindduck[full] \blindduck[full] \blindduck[full] \blindduck[full] \end{document}

Why \FloatBarrier isn't the magic answer (unfortunately)

Compile the following and see an half empty first page:

\documentclass{article}

\usepackage{float,placeins}

\usepackage{duckuments}

\begin{document} \blindduck\par \begin{figure}\rule{5cm}{.8\textheight}\caption{big}\end{figure} \blindduck \FloatBarrier \begin{figure}[H]\rule{3cm}{2cm}\caption{small}\end{figure} \blindduck[full] \blindduck[full] \blindduck[full] \blindduck[full] \end{document}

The reason is that \FloatBarrier is just a more intelligent \clearpage (it checks whether there are deferred floats, and if so does \clearpage; this simplification of it might do \FloatBarrier injustice but should suffice for highlighting why it can't solve everything).

Skillmon
  • 60,462
  • Wow! Thanks A LOT for the answer, though it's really bad news to me. Given your second example, it probably happens even if we restrict ourselves to only [h], [ht], [!h], [!ht], and [H], right? Any way to retain the order? –  Aug 19 '22 at 22:43
  • Then you might still get problems with the float order, see the code block I added (you might use ht as placement for the first float, the order will still be messed up). And if you got big floats you'll still get problems with those options (H will most likely lead to some very empty pages, and ht might flush all your floats that don't use H to the end of the current chapter or document). – Skillmon Aug 19 '22 at 22:47
  • @GeekestGeek you can retain the order by either not using H at all, or only using H, or flushing the float-queue before every time you use H (effectively destroying the purpose of floats). – Skillmon Aug 19 '22 at 22:49
  • @GeekestGeek seriously, H is a bad idea most of the time. You can also use only H or no H at all in each chapter individually (provided a \clearpage is issued by every new chapter), that way your order would still be good. – Skillmon Aug 19 '22 at 22:50
  • In a book large enough (hunderds of pages), we inevitably run into the need for floating figures in certain places, and we also run into a need for fixed figures in other places. For example, we wish to place figures logically belonging to, say, a multi-page Example 1.2.3 between “Example 1.2.3” and the halmos symbol ■ ending the example, and it doesn't matter to us how these figures float otherwise. My hope was that if we put the last figure of the example with [H] right before ■, all would be good (and all was good so far). –  Aug 19 '22 at 22:55
  • @GeekestGeek see my latest edit for an example illustrating the problem only using ht might cause. – Skillmon Aug 19 '22 at 22:56
  • Right, I saw it. Now, as for flushing the queue, how do I do that most economically? \FlushFloats\begin{figure}[H]…\end{figure}\FlushFloats ? –  Aug 19 '22 at 22:57
  • @GeekestGeek might be true, but if you resort to manual placement for a single float, you need to place every float up to the next \clearpage manually as well. Either use floats or don't, but don't mix. – Skillmon Aug 19 '22 at 22:57
  • @GeekestGeek there is a package for that, load placeins and use \FloatBarrier (or run a \clearpage for a lightweight option -- that might result in pretty empty pages as well). – Skillmon Aug 19 '22 at 22:59
  • I got your point. Unfortunately, manual placement doesn't work well with the editing process: when I change something on page 100, I shouldn't need to inspect the placement of all figures in pages 100-200, if the chapter ends at page 200. In your experience, would \FloatBarrier\begin{figure}[H]…\end{figure}\FloatBarrier work? –  Aug 19 '22 at 23:00
  • Correct, which is why I'd not recommend it :) – Skillmon Aug 19 '22 at 23:00
  • @GeekestGeek maybe take a peak at this question (if not already done): https://tex.stackexchange.com/questions/2275/keeping-tables-figures-close-to-where-they-are-mentioned – Skillmon Aug 19 '22 at 23:04
  • Ok, if I place a single float manually with \FloatBarrier\begin{figure}[H]…\end{figure}\FloatBarrier, why would I still need to place every float up to the next \clearpage (in my case, end of the chapter) manually as well? What goes wrong when saying \FloatBarrier\begin{figure}[H]…\end{figure}\FloatBarrier \begin{figure}[ht]…\end{figure}? –  Aug 19 '22 at 23:05
  • @GeekestGeek Because that might result in pretty empty pages, another example added to my answer :) – Skillmon Aug 19 '22 at 23:07
  • True. To be honest, it's difficult to get your last example both with correct order and without pretty empty pages. So, the last example will look ugly in one way or another. The best of the worst could be, I guess, putting [H] instead of [h] and removing the barrier. It's a mess :-). Anyway, knowing now that \FloatBarrier is just a clever \clearpage is sobering. Sigh. Thank you for a good lesson!!! –  Aug 19 '22 at 23:19
  • As I said, the only viable two options, imo, are all floats being floats, or all placed manually (at least chapter wise/natural \clearpage wise). There is another possibility which might be helpful if your floats and H-thingies aren't too close together: The \afterpage{\clearpage} trick flushes the deferred floats at the next natural page break. But still don't expect magic from it, LaTeX isn't omniscient and only does best guesses from what it saw so far, it doesn't look ahead and optimise page breaks. – Skillmon Aug 19 '22 at 23:22
  • @GeekestGeek simple solution for the last example: Accept that sometimes floats are not a single line away from where they are in the sources, use \refs and let floats float. Just use tbp or htbp for both floats and the example has the correct order and no half empty pages mid document :) – Skillmon Aug 19 '22 at 23:24