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.
The following is a complete code example. You will find a link to the expected result further below.
\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 PDF result can be viewed here.
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}

May 24th, 2011 at 10:39 pm
With minipages you can also create non-nxn-grids, e.g. one on the left, and two stacked on the right:
\begin{figure}[h]
\centering
\begin{minipage}{0.4\textwidth}%
\subfloat[Subfigure 1 list of figures text][Subfigure 1 caption]{
\includegraphics[width=0.4\textwidth]{figure1.jpg}
\label{fig:subfig1}}
\end{minipage}%
\qquad
\begin{minipage}{0.4\textwidth}%
\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}}
\end{minipage}
\caption{This is a figure containing several subfigures.}
\label{fig:globfig}
\end{figure}
May 25th, 2011 at 4:05 am
Hi Louis,
Thanks for your comment. I discussed
minipagesin an other post on that topic.Cheers, Tom
June 7th, 2011 at 2:35 am
There is a bug in subfig (not present in subfigure) which makes it essentially unusable in some cases. When you turn it on your ALL figure captions get centered by default and there is NO way to make them justified as they were before (with revtex4 at least, I didn’t check article but seen some similar posts on the web) — raggedleft/right works but justified doesn’t. Very irritating & forced me to switch back subfigure which works perfect, at least in this aspect.
July 11th, 2011 at 10:52 am
For similar sized figures so far I had used tabular inside a figure environment. I’m not sure whether that lets you specify separate references for the figures though. Thanks for this post, I’ll try this in the future.
September 30th, 2011 at 12:54 am
Great work. Thanks for that. But is there any way, you can change the style of your posts a little bit, so that they are more convenient. You see, first of all, it would be great if you would give your code a little bit more structure by using indent lines. And it would be great if you could offer a .tex file, since if I like to copy you code right now, I have to remove all the line numberings by hand. Or am I missing something and I am just doing the whole thing wrong?
September 30th, 2011 at 4:00 am
Hi Johre,
Thanks for your comment. You can copy the code to the clipboard or display the code only. Just place your mouse inside the code box and you’ll see an option box appear. I assume it works with most browsers.
Tom.
September 30th, 2011 at 4:16 am
Hey Tom,
Ah, that’s cool. But still, a little more structure would increase the readability a lot.
So instead of writing:
\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}}
\caption{This is a figure containing several subfigures.}
\label{fig:globfig}
\end{figure}
writing it this way:
\begin{figure}[h]
\centering
\subfloat[Subfigure 1 list of figures text][Subfigure 1 caption]
{
\includegraphics[width=0.4\textwidth]{figure1.jpg}
\label{fig:subfig1}
}
\subfloat[Subfigure 2 list of figures text][Subfigure 2 caption]
{
\includegraphics[width=0.4\textwidth]{figure2.jpg}
\label{fig:subfig2}
}
\caption{This is a figure containing several subfigures.}
\label{fig:globfig}
\end{figure}
which in my opinion makes it a hell of a lot easier to read. Especially with the multiple \captions and \labels
September 30th, 2011 at 4:17 am
I just realized, that the tabs are just ignored in the comments. Is it the same with the code in the normal posts?
September 30th, 2011 at 4:29 am
WordPress is not a Latex editor, unfortunately
. But I’ll see what I can do about the indentation. Thanks, Tom.
September 30th, 2011 at 4:30 am
Maybe uploading a .tex file as well
But as I was saying, nice Blog! Keep on doing it!
September 30th, 2011 at 4:34 am
Thanks! I constantly try to improve and your suggestions are most welcome.
December 1st, 2011 at 8:48 am
[...] we manually add our plots using the standard LaTeX includegraphics-command and the subfig-package (see this post for more details). In case you prefer png and/or eps figures, use the pdf, png, eps [...]
December 4th, 2011 at 9:59 pm
Hello Tom,
I’m sort of a newbie with latex…
I copied and pasted you code but the generated pdf file turned out different from the sample that you posted.
The sub-figuring seems to be working, however the tables are one on top of each other and the figures have a weird “cross” configuration (figure 1 is by itself on the first first line, figures 2 and 3 side-by-side on the second line, and figure 4 on a third line).
May it be an issue with my latex version (latex –version says “This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)”, I’m on a mac)? Or do you have any idea of what the issue may be?
Thanks in advance,
GB
December 5th, 2011 at 6:33 am
Hi GB,
Concerning the tables, you need to make sure that the width of both tables together doesn’t exceed
\textwidth, the maximum space available for content on the page. Since it’s a floating environment, LaTeX will automatically add a line-break when the second table reaches into the margin. Just reduce their size a little, for example by reducing the font size or using fixed-width columns.I have seen the cross-like arrangement of figures before. What you can do is the following:
\begin{figure}[h] \centering \subfloat[][]{ \rule{4cm}{3cm} } \qquad \subfloat[][]{ \rule{4cm}{3cm} }\\ \subfloat[][]{ \rule{4cm}{3cm} } \qquad \subfloat[][]{ \rule{4cm}{3cm} } \end{figure}That should give you the desired 2×2 arrangement.
\qquadwill add some vertical space between the figures, it’s optional. More important is the line-break (\\) after the second figure.Hope it works!
Best, Tom.
January 12th, 2012 at 5:51 am
[...] http://texblog.org/2011/05/24/placing-figures-side-by-side-subfig/ [...]
February 9th, 2012 at 1:57 am
Hi Tom,
I’m having a hard time with the following situation.
I’m using the IEEEtran documentclass, with option journal.
The formatting of the document is two columns, and I want to span a double float figure (two subfigures) across the two columns, using the subfigure package.
For some reason, when I compile the document, although I place the figure on the *first* page of my paper, it shows up on the second. I am also using the stfloats and overpic packages for this figure; the figure placer is [bp].
Also, the figure is not centered properly on the page….
Any ideas what may cause the problems? Have I given you sufficient detail?
Thanks a lot!
Sper
February 13th, 2012 at 4:04 pm
Hi Sper,
Yep, I guess you gave sufficient detail, but a minimal example would be even better
. Here is one that seems to work using
[h!t]:\documentclass[journal]{IEEEtran} \usepackage[english]{babel} \usepackage{blindtext, stfloats, overpic, subfig} \begin{document} \begin{figure}[h!t] \centering \subfloat[][]{ \rule{0.45\textwidth}{3cm} } \subfloat[][]{ \rule{0.45\textwidth}{3cm} } \end{figure} \blindtext \end{document}Hope it helps.
Best, Tom.
February 13th, 2012 at 9:56 pm
Thanks a lot for taking the time, Tom!
As a continually developing newbie in LaTeX (aren’t we all?! well, some more than others, I suppose), I learned a lot from your reply and after having played a bit with your example I have managed to make it work in the paper I am writing.
I’ll try to provide a minimal example next time I post a question:)
Thank you, again, for your help!
Sper
February 13th, 2012 at 11:45 pm
On a second take, it doesn’t work quite as I expected; I tried filling the page with text and the second figure overlaps on the text:(
\documentclass[journal]{IEEEtran} \usepackage[english]{babel} \usepackage{amsmath} \usepackage{blindtext, stfloats, overpic, subfig} \begin{document} \begin{figure}[bp] \centering \subfloat[][]{ \rule{0.45\textwidth}{3cm} } \subfloat[][]{ \rule{0.45\textwidth}{3cm} } \end{figure} \blindtext[13] \end{document}Maybe one of the issues is that in my paper I would need this figure on the bottom of the first page (which is why I used the bp placer and the stfloats package), and it keeps either overlapping on the second column, or moving to the next page, overlapping with both.
Hmm…
Anyway, thank you again for taking the time, Tom.
Sper
February 14th, 2012 at 3:29 am
Hi Sper,
After reading this and this, unfortunately, I believe that placing the figure at the bottom of your first page is not possible. You may either place it at the top/bottom of the second page or try the multicol package.
Sorry for that,
Tom.
February 16th, 2012 at 6:38 pm
Thank you very much, Tom, for looking into this.
I did some more thinking and came to the conclusion that there is a logical reason why this is not allowed/ possible: the mandatory content of the first page in a two column IEEE journal paper. I think I’ll place my figure on the second page.
Again, thank you for taking the time.
Sper
February 17th, 2012 at 7:04 pm
Sure. Good luck with your paper! Tom.
February 22nd, 2012 at 11:18 am
Hi Tom, your help with my last question solved my problem and now my list of figures looks great! I was wondering if it is possible placing the caption of a figure side instead of below. That because if you have single figures in a document full of subfigs and you want the figures to be the most uniform size possible, placing the caption in a side may help you get rid of the blanks at the sides of a centred figure and even optimise space in your page when captions are long
February 22nd, 2012 at 2:38 pm
Hey, placing caption next to a figure probably only makes sense, if there is enough space. Here is a minimal example that illustrates how to do it:
\documentclass{article} \usepackage[english]{babel} \usepackage{sidecap, graphicx, blindtext} \begin{document} \begin{SCfigure} \centering \rule{6cm}{8cm} \caption{\blindtext} \end{SCfigure} \end{document}Hope it helps,
Tom.