The float package
Adds the placement [H], which sets a figure or table exactly where it stands in the source, and defines new float types with \newfloat.
Loading the package
\usepackage{float}The placements h, t, b and p of plain LaTeX are requests. LaTeX moves a figure when it does not fit, sometimes several pages away. [H] from this package switches floating off for one figure or table: it is set at this point, and if the rest of the page is too short, it starts the next page and leaves the gap.
The second purpose is float types of your own. \newfloat{program}{tbp}{lop} creates an environment program with its own counter, caption label and list. \floatstyle chooses the look of the types defined after it: plain, plaintop (caption above), boxed or ruled.
Main commands
\begin{figure}[H] ... \end{figure} | Sets the figure exactly here. |
\newfloat{program}{tbp}{lop}[section] | New float type program: default placement tbp, list file extension lop, numbered within sections. |
\floatname{program}{Program} | The word printed before the number in captions. |
\floatstyle{ruled} | Style for the float types defined after it. |
\restylefloat{table} | Applies the current style to an existing type; with plaintop, table captions are printed above the table. |
\listof{program}{List of programs} | Prints the list of all floats of one type. |
Complete example
\documentclass{article}
\usepackage{graphicx}
\usepackage{float}
\floatstyle{ruled}
\newfloat{program}{tbp}{lop}
\floatname{program}{Program}
\begin{document}
Text before the figure.
\begin{figure}[H]
\centering
\includegraphics[width=0.4\textwidth]{image.png}
\caption{This figure stays between the two sentences.}
\end{figure}
Text after the figure.
\begin{program}[H]
\begin{verbatim}
print("Hello")
\end{verbatim}
\caption{A float type of its own with a ruled layout.}
\end{program}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
Hstands alone.[Hb]or[H!]stops the run with a fatal error.- Without the package,
[H]gives the error "Unknown float option". The lowercase[h]is a different placement, and LaTeX may change it to[ht]; see `h' float specifier changed to `ht'. - A figure with
[H]is set at once, while an earlier figure with[t]or[p]may still be waiting. The later figure then appears first, and the numbers in the document are out of order. [H]on every float produces half-empty pages. Use it for the single figure that has to follow a sentence, andplaceinsto keep floats inside their section.
Related pages
- How-to Figure placement in LaTeX
- Package
placeins - Package
caption - Error `h' float specifier changed to `ht'
- Error Too many unprocessed floats