This section will give an overview of floats and figures. It will cover importing external graphics and positioning. It will also cover sub-figures and captions.
We have already seen in the last section that it is often useful to enclose a tabular environment in a table environment. The table environment is an example of a float. Floats are blocks of content that "float" around the page in the sense that LaTeX chooses where to place them based on certain algorithms.
In general, it is a good idea to let LaTeX place the float where it wants, at least at first, when you have the entire document written you can then go back and change it if you wish. If you use the float package, one can override the LaTeX placement with custom options.
The code below on the right right gives an example of a typical float environment with special placement. Here the specifier is H. The table below on the left lists some of the possible specifiers (you can of course define your own).
Specifier | Action |
---|---|
H | Place exactly at spot in source text |
h | Place approximately at spot in source test |
t | Place at top of page |
b | Place at bottom of page |
p | Place on page for floats only |
! | Override internal LaTeX parameters for determining float position |
\documentclass{article}
\usepackage{float}
\begin{document}
\begin{figure}[H]
%figure content goes here
%Can be:
%graphic
%table
%tikz drawing
%etc.
\end{figure}
\end{document}
Note: The float package is not necessary if you wish to let LaTeX place the figure for you.
Often it is useful, if not necessary, to include graphics inside a LaTeX document. For the remainder of this section, we are going to use the image to the right in our code. You can of course use any image you wish, but in order to provide proper consistency, you can right click and download this picture of an atom from wikipedia (I like atoms).
The code on the right imports a figure and places it into a document while scaling it by 1.5x.
View PDF ››\documentclass{article}
\usepackage{float}
\usepackage{graphicx}
\begin{document}
\begin{figure}[H]
\centering
\includegraphics[scale=1.5]{atom.png}
\end{figure}
\end{document}
Warning! Notice that this picture looks pixelated, especially at this scale. One must be careful to use high enough quality images. Preferable are vectorized graphics which will scale without pixelation.
This syntax is fairly self-explanatory, however, one thing to note is the use of the \centering command. There are other ways to center a figure, but this is the correct way to do it inside a float environment. You will almost always be using this command when using floats.
The \includegraphics command is part of the graphicx package and can import all the usual file formats including pdf, jpeg, png, etc. The command also accepts other options instead of scale such as rotation, height, width, bounding box, etc.
Lets take the figure we had before and add a caption to it. Where the caption goes in relation to the \includegraphics command will determine where it is placed on the page. Play with this to see what happens.
View PDF ››\documentclass{article}
\usepackage{float}
\usepackage{graphicx}
\begin{document}
\begin{figure}[H]
\centering
\includegraphics[scale=1.5]{atom.png}
\caption{This is an atom}
\end{figure}
\end{document}
When one wants to put multiple subfigures inside a subfigure, one must use two packages, caption and subcaption. There are other packages such as subfigure and subfig, however, these are no longer considered standard.
Try the code below to see three subfigures within a figure, each with a separate caption in addition to a global caption.
\documentclass{article}
\usepackage{float}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}[H]
\centering
\begin{subfigure}{.3\textwidth}
\includegraphics[width=\textwidth]{atom.png}
\caption{Atom 1}
\end{subfigure}
%%%%%%%%%%%%%%
\begin{subfigure}{.3\textwidth}
\includegraphics[width=\textwidth]{atom.png}
\caption{Atom 2}
\end{subfigure}
%%%%%%%%%%%%%%
\begin{subfigure}{.3\textwidth}
\includegraphics[width=\textwidth]{atom.png}
\caption{Atom 3}
\end{subfigure}
\caption{Atoms are fun!}
\end{figure}
\end{document}
Each subfigure is treated like a normal figure in relation to the containing float. The \textwidth simply tells the compiler to make the width of the subfigure .3 times the width of the allowed width of the text (the page width minus the margins).
However, inside the \includegraphics command, we set width=\textwidth. This is because inside the subfigure enviornment, the \textwidth command has been redefined to the width of the subfigure.
Created by Zachary Glassman. Contact at zach.glassman@gmail.com