texblog

Placing figures/tables side-by-side (\subfig)

The subfigure package was replace by the subfig package quite a while ago. I therefore decided to replace my old post on that topic (Placing figures/tables side-by-side with subfigure) with an introduction to the subfig package. The package simplifies the positioning, captioning (I wonder if that’s a word) and labeling of small “sub” figures and tables within a single figure or table environment.

So much for the theory, let’s have a look at the usage. First we load the package, the options tell LaTeX to include the subs in the listoffigures and listoftables. Most probably you will also need the graphicx package, e.g. to set the figure width.

\usepackage[lofdepth,lotdepth]{subfig}
%\usepackage{graphicx}

The subfloat command has a body, for the content, being most commonly a figure/table plus a label. The two optional arguments define the list-of-figures text and the caption. If only one is provided, the text will be used for both, somewhat similar to the caption command (\caption[⟨list entry⟩]{⟨caption⟩}).

\subfloat[⟨list entry⟩][⟨sub-caption⟩]{⟨body⟩}

We need the figure (or table) floating environment as a container for the actual subfigure/subtables. This leads us to a complete subfigure example with two subfloats:

\begin{figure}[h]
\centering
 \subfloat[][]{
   \rule{4cm}{3cm}
 }
 \subfloat[][]{
   \rule{4cm}{3cm}
 }
\end{figure}

Referencing a subfloat is straight forward. The first code line will produce the complete reference number, e.g. 4a, whereas the second line will only produce the sub-index, e.g. (a).

\ref{⟨label⟩}
\subref{⟨label⟩}

By default, the package will place the figures/tables side-by-side. Now in case you want to arrange 4 subfloats 2×2, you make use of the standard linebreak by leaving an empty line.

 
Complete code example with output

\documentclass[11pt, a4paper, draft]{article}
\usepackage{graphicx}
\usepackage[lofdepth,lotdepth]{subfig}
\begin{document}
\listoftables
\listoffigures
\section{Example with 3 sub-tables}
\begin{table}[ht]
\centering
\subfloat[Subtable 1 list of tables text][Subtable 1 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\qquad
\subfloat[Subtable 2 list of tables text][Subtable 2 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\qquad
\subfloat[Subtable 3 list of tables text][Subtable 3 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\caption{This is a table containing several subtables.}
\end{table}
\clearpage
\section{Example with 4 sub-figures 2x2}
\begin{figure}[h]
\centering
\subfloat[Subfigure 1 list of figures text][Subfigure 1 caption]{
\includegraphics[width=0.4\textwidth]{figure1.jpg}
\label{fig:subfig1}}
\qquad
\subfloat[Subfigure 2 list of figures text][Subfigure 2 caption]{
\includegraphics[width=0.4\textwidth]{figure2.jpg}
\label{fig:subfig2}}
\subfloat[Subfigure 3 list of figures text][Subfigure 3 caption]{
\includegraphics[width=0.4\textwidth]{figure3.jpg}
\label{fig:subfig3}}
\qquad
\subfloat[Subfigure 4 list of figures text][Subfigure 4 caption]{
\includegraphics[width=0.4\textwidth]{figure4.jpg}
\label{fig:subfig4}}
\caption{This is a figure containing several subfigures.}
\label{fig:globfig}
\end{figure}
In the text, you can refer to subfigures of figure \ref{fig:globfig} as \ref{fig:subfig1}, \ref{fig:subfig2}, \ref{fig:subfig3} and \ref{fig:subfig4} and to the sub-index as \subref{fig:subfig1}, \subref{fig:subfig2}, \subref{fig:subfig3} and \subref{fig:subfig4}.
\end{document}

 
The complete documentation of this very comprehensive package is available here.

A final remark:
In order to simulate figures for testing purposes, you may either use the draft option of the documentclass command which lets you load existing or non-existing figure files and draws an empty box as placeholder. Or you may make use of the rule command that by default draws a black line of a certain length and thickness.

\documentclass[draft]{article}
...
\includegraphics[width=0.3\textwidth]{virtualfigure.jpg}
\rule{4cm}{3cm}
Exit mobile version