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}
louis
With minipages you can also create non-nxn-grids, e.g. one on the left, and two stacked on the right:
tom
Hi Louis,
Thanks for your comment. I discussed
minipages
in an other post on that topic.Cheers, Tom
mc
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.
suvayu
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. 🙂
johre
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?
tom
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.
johre
Hey Tom,
Ah, that’s cool. But still, a little more structure would increase the readability a lot.
So instead of writing:
which in my opinion makes it a hell of a lot easier to read. Especially with the multiple \captions and \labels
johre
I just realized, that the tabs are just ignored in the comments. Is it the same with the code in the normal posts?
tom
WordPress is not a Latex editor, unfortunately :-). But I’ll see what I can do about the indentation. Thanks, Tom.
johre
Maybe uploading a .tex file as well 🙂 But as I was saying, nice Blog! Keep on doing it!
tom
Thanks! I constantly try to improve and your suggestions are most welcome.
R: Controlling size and placement (subfig) of Sweave-generated figures « LaTeX Matters
[…] 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 […]
giacobenin
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
tom
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:
That should give you the desired 2×2 arrangement.
\qquad
will 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.
การใส่รูปจำนวนมากใน Latex โดยใช้ subfig | WJ's Sandbox
[…] http://texblog.org/2011/05/24/placing-figures-side-by-side-subfig/ […]
Sper
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
tom
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]
:Hope it helps.
Best, Tom.
Sper
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
Sper
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:(
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
tom
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.
Sper
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
tom
Sure. Good luck with your paper! Tom.
marinadq
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 🙂
tom
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:
Hope it helps,
Tom.
Frith
Hi Tom,
Great article – it was just the thing I needed.
One bit of behaviour that stumped me for a second was the treatment of whitespace in the latex file. It seems that if you put an extra line at the end of the subfigures latex places them all in a vertical row, rather than anything else you might be trying to do.
e.g:
produces the expected behaviour (2×2 box), but this:
creates a 1×4 subfigure (no matter how small the boxes are).
Its just a stylistic change in the latex code (I always liked to have clear space around my figures), but it can trip you up if you don’t know about it!
tom
Hi Frith,
You are right, thanks for the comment.
The correct way to end a paragraph in LaTeX is through an empty line. Consistently, this also works within floating environments. Therefore, your 2×2 example could be written as follows:
Thanks and best,
Tom.
Hien Phan
Hi Tom,
Thanks for your great example.
Your sample is correct for three subtables, but it doesn’t work well with 2 subtables.
According to your sample code, I tried to push two subtables in a table. The code like this (just for subtable):
However, two subtables are not in the same line horizontal). They are pushed in vertical direction.
Could you please suggest some advice about this?
Thanks,
Hien Phan.
tom
Hi Hien Phan,
Thanks for your question. You may find some of the answers given here useful.
Best, Tom.
Andre Jaenisch
Hello,
your result PDF (http://texblog.files.wordpress.com/2011/05/subfig-sample-code-output.pdf) throws a Permission Denied.
Best regards,
André
tom
Many thanks. I also have problems with figures on my blog. Will try to fix this asap.
Cheers, Tom
Qiang Guo
What’s the purpose of “lofdepth” and “lotdepth” options?
tom
Hi Qiang Guo,
The counters
lofdepth
andlotdepth
are defined by the subfigure package. They control if sub-figures and sub-tables are shown in the list-of-figures and list-of-tables, respectively. They are set to 1 by default (only show main figure/table caption). If you prefer to have sub-figure/sub-table captions also, add these lines of code to your preamble:Milena
Hey Tom,
I used A very similar code to yours, but The figure number (for both figures together) is always one too high (so 2 instead of 1). However when considering the the \ref it references the ‘correct’ figure number (so 1) Do you know what could be going wrong? (I’m assuming it counts the sub-figures as figures as well, but how do i fix this?)
Thank you!
tom
Hi Milena,
Thanks for your question. Please provide a complete minimal example, so I can reproduce the problem. From your code it is not clear what sub-figure package you used.
Thanks, Tom