The article explains how to use the correct LaTeX commands to insert a figure in text. The desired placement of figures throughout the text is demonstrated using four different types of techniques with the help of illustrations.
Float
Floats are used to contain elements that cannot be broken across pages, such as figures and tables. If there is not enough room on the current page for a figure, the float will place the figure at the top of the next page. The figure environment is used to generate floats that contain figures. The placement specifier parameter allows us to have control over the location of the figure in text.
Specifier | Placement |
---|---|
t | Place the figure at the top of a text page |
b | Place the figure at the bottom of a text page |
h | Place the figure at the position in the text where the figure environment is (since it cannot be used alone, it automatically adds the t option) |
p | Place the figure on a separate float page |
! | Used in addition to the other parameters, it overrides the restrictions of LaTeX over the floats (such as the maximum number of floats in a page) and helps to enforce the chosen parameter. |
H | Comes with the float package, it absolutely forces the figure to appear at the position in the text where the figure environment is |
The default placement parameter is tbp, which means LaTeX will try to place the figure to the top or the bottom part of the page, or it will place it on a separate float page. The order of the parameters is not important, it only means that LaTeX will try to place the figure in one of the positions that are included in the parameter, and will not try the excluded ones.
\usepackage{graphicx,float} % ... \begin{figure}[H] \centering \includegraphics[width = .5\textwidth]{latex.png} \caption{Figure in a float} \label{fig:float} \end{figure}
Floats are a helpful environment to arrange the positions of figures in a text body. However, in some cases, its options may lack the control needed to make the figure appear in an exact part of a text. In those cases, one of the options below can help position the figure.
Minipage
Minipage is an alternative to float, it does not require the figure environment. It creates a small page environment with a given width. Since this is not a figure environment, caption
command is not enough to elaborate on the element we are creating the caption for. For this purpose, we can include caption package at the beginning of our document, and use captionof
command with figure
parameter to add a caption to the figure. Additionally, to center the figure, \centering
command is used. This environment might need a vertical spacing command to keep consistency throughout the text.
\usepackage{graphicx, caption} % ... \begin{minipage}{\linewidth} \centering \includegraphics[width = .5\textwidth]{latex.png} \captionof{figure}{Figure in a minipage} \label{fig:minipage} \end{minipage}
Center
Center is an alternative to float environment and minipage
, it only centers the elements in its scope. Since this is still the same document environment, spacing will be similar to other elements in the text, such as paragraphs.
\usepackage{graphicx, caption} % ... \begin{center} \includegraphics[width = .5\textwidth]{latex.png} \captionof{figure}{Figure in a center scope} \label{fig:center} \end{center}
AdjustBox
Adjustbox package is used to make a non-floating float substitute for your packaged products. In an adjustbox environment, alignment, caption and label are attributes that can be declared as an argument. We also need to declare that we are adding a figure which is not going to be evaluated as a float with nofloat = figure
option. Additionally, to separate the caption from the regular text, we can use the vspace
option with any length value. In this case, we used \bigskipamount
to make it the same size as a \bigskip
.
\usepackage{graphicx, adjustbox} % ... \begin{adjustbox}{center, nofloat=figure, vspace=\bigskipamount, caption={Figure in an adjustbox}, label={fig:adjustbox}} \includegraphics[width=\textwidth]{latex.png} \end{adjustbox}
FAQ
Question 1: How can I tell LaTeX I really want the figure in that specific place, no matter how much whitespace will be left?
As mentioned in the Float section, the placement specifier parameters can be used. The best way to precisely position the figure is the H placement specifier, which is a part of the float package.
Question 2: Is H from float the same as !h?
No, !h is not the same as H. !h only positions the picture at the desired location, if possible, but it’s not an absolute command.
Question 3: Which other methods can be used for forced figure placement in text other than float?
Minipage, center and adjustbox can be used as an alternative to float if the desired figure location is the same as its place in the LaTeX code.