34 Comments

  1. louis

    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}
  2. 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.

  3. 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. 🙂

  4. 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?

    • 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.

      • 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

  5. 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

    • 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. \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.

  6. 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

    • 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.

  7. 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

  8. 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:(

    \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

    • 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.

  9. 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

  10. 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 🙂

    • 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.

  11. 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:

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

    produces the expected behaviour (2×2 box), but this:

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

    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!

    • 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:

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

      Thanks and best,
      Tom.

  12. 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):

    \begin{table}[ht]
    \centering
    \subfloat[Subtable 1 list of tables text][Subtable 1 caption]{
    \begin{tabular}{ccc}
     0 & 1 & 2\\
     1 & 2 & 0\\
     2 & 0 & 1\\
    \end{tabular}}
    \qquad
    \subfloat[Subtable 2 list of tables text][Subtable 2 caption]{
    \begin{tabular}{ccc}
     0 & 0 & 0\\
     0 & 1 & 1\\
     0 & 2 & 2\\
     1 & 0 & 1\\
     1 & 1 & 2\\
     1 & 2 & 0\\
     2 & 0 & 2\\
     2 & 1 & 0\\
     2 & 2 & 1\\
    \end{tabular}}
    \caption{This is a table containing several subtables.}
    \end{table}

    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 Qiang Guo,

      The counters lofdepth and lotdepth 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:

      \usepackage{subfigure}
      \setcounter{lofdepth}{2}
      \setcounter{lotdepth}{2}
  13. 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!

    \begin{figure}
        \begin{subfigure}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=1.0\textwidth]{image1.png}
            \caption{(a)}\label{fig:dataA}
        \end{subfigure}
        \begin{subfigure}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=1.0\textwidth]{image2.png}
            \caption{(b)}\label{fig:dataB}
        \end{subfigure}    
    \caption{Data Statistics}\label{fig:data}
    \end{figure}
    • 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

Leave a Reply to johreCancel reply