8

Not sure why but in my long document \begin{algorithm}[h] leads to the algorithm and all subsequent algorithms to being placed at the very end but \begin{algorithm}[] leads to the algorithm being placed at the correct location.

What is [h] doing? and what does [] do instead?

Also, can anyone explain why [h] would move the algorithm to the end of the document? I can't seem to construct a small example of this happening or one not using a bunch of stuff I'd like to keep private for now.

1
  • It means ‘place the figure here, if possible’.
    – Bernard
    Jul 17, 2021 at 19:47

2 Answers 2

13

A float may be placed

h (here, at the point the environment is in the source)

t (top of a page)

b (bottom of a page)

p (on a float page that only has floats)

The default position is set by the document class but is usually tbp so [h] allows "here" floats but prevents the float being placed in any of the other places. As it is often not possible to place a float in the h position, LaTeX will change this to [ht] and gives a warning.

LaTeX Warning: `h' float specifier changed to `ht'.

[] means that the float is not allowed anywhere so this is really a syntax error. LaTeX warns about this and substitutes the default option

LaTeX Warning: No positions in optional float specifier.
               Default added (so using `tbp') on input line 25.

The intended way to get the default positions is to not use the option, not to use []

1
  • 4
    You might want to mention that, if the float forced to the end of the document is not the last float, any later floats are also dragged off to the end. Jul 18, 2021 at 0:59
8

(this answer addresses solely the OP's follow-up question)

You asked:

Also, can anyone explain why [h] would move the [float] to the end of the document? I can't seem to construct a small example of this happening ...

First, please be sure to study @DavidCarlisle's earlier answer. Second, for an extremely thorough discussion of how LaTeX places floats, please be sure to read Frank Mittelbach's answer to the question How to influence the position of float environments like figure and table in LaTeX?

If you want, you can think of the following answer as an extremely condensed and selective summary of Frank's essay, focusing on the role of the h and t placement specifiers and the important role played by the little-known parameter called \topfraction.

You wrote,

I can't seem to construct a small example

Let's consider the following small example:

\documentclass{article}
\usepackage{lipsum} % filler text
\begin{document}

% Uncomment the next line to get the float placed at the top of page 2:
%\renewcommand\topfraction{0.8} % default value: 0.7

\lipsum[1-3]

\begin{figure}[h] % <-- note: "h"
\rule{1\textwidth}{0.7\textheight} % print a black rectangle
\caption{Hello World}
\end{figure}

\lipsum[4-6]
\end{document}

Please compile this example on your system to verify that the float will be placed at the end of the test document, i.e., on page 3, and not on either page 1 or page 2.

What's going on? First, some preliminaries.

  • As David has noted in his answer, LaTeX actually automatically changes [h] to [ht] if the float cannot be placed right away.

  • By design, the total height of the float slightly exceeds 0.7\textheight.

  • By specifying [ht], one rules out either b ("bottom") or p (on a page by itself) as potential locations for the float.

  • In the article document class (and, likely, in many other document classes too), the default values of the parameters \topfraction and \bottomfraction are 0.7 and 0.3, respectively. Their unit of measurement is \textheight. This means that a float (or, possibly, several floats) must not take up more than 0.7\textheight if placed at the top of a page, and 0.3\textheight if placed -- you guessed it! -- at the bottom of a page. The parameter choices sure give the impression that LaTeX should not place larger floats in the middle or at the bottom of a page, don't they?

Now to the document contents:

  • LaTeX begins by typesetting three paragraphs of filler text. This takes up about two thirds of the page.

  • LaTeX next encounters a figure environment (a "float" in LaTeX jargon) and takes note that [h] is specified. LaTeX must decide if it can place the float right away. Given that the figure's total height is slightly more than 0.7\textheight, it turns out that the figure can not be placed "here", since doing so would result in a massively overfull page. Hence, LaTeX changes the positioning specifier from [h] to [ht] and places the float on a special stack, and its potential suitability for placement will next be considered at the start of the next page.

  • LaTeX next encounters the instruction \lipsum[4-6] and starts typesetting more filler text. But before it can fully typeset all three paragraphs of filler text, a page break occurs. At the top of each page, LaTeX checks if the stack of accumulated floats is empty or not. If it's not, i.e., if there are floats waiting be placed, LaTeX next checks if it is allowed to place the float. Now the [t] location specifier kicks in. But because \topfraction (0.7) is less than the height of the figure, LaTeX determines that it cannot typeset the figure at this location either, and the float gets placed back on the stack, to be considered at the top of next page.

  • Meanwhile, LaTeX gets to \end{document} and realizes that a final task remains: to place the float. In the text document at hand, the only possible/permissible location happens to be the very end of the document, literally on the final page, all by itself.

I would like to encourage you to recompile the test document, but now with \topfraction set to 0.8: Lo and behold, the float now gets typeset at the top of page 2. Life is good.

2
  • 2
    actually h only gets changed to ht if it can't be placed here (so [h] never goes to the top of the current page Jul 17, 2021 at 23:22
  • 1
    @DavidCarlisle - Many thanks for pointing out this inaccuracy. I'll fix it right away.
    – Mico
    Jul 17, 2021 at 23:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .