Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to put four figures in a single row. The code I am trying to use is taken from http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions.

This puts three figures in a row. When I add a fourth one, I get three images in row and the last one appears in the second row. But I want all of them in one row. Here is my code.

\documentclass[12pt,twoside]{report}% Use this line for the print version of the thesis


\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}

\begin{figure}
        \centering
        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{gull}
                \caption{A gull}
                \label{fig:gull}
        \end{subfigure}%


        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{gull}
                \caption{A gull2}
                \label{fig:gull2}
        \end{subfigure}%

        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
          %(or a blank line to force the subfigure onto a new line)
        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{tiger}
                \caption{A tiger}
                \label{fig:tiger}
        \end{subfigure}
        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
          %(or a blank line to force the subfigure onto a new line)
        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{mouse}
                \caption{A mouse}
                \label{fig:mouse}
        \end{subfigure}
        \caption{Pictures of animals}\label{fig:animals}
\end{figure}
\end{document}

As I have mentioned above, it works for three figures. But I can not use it for four figures. What should I change?

What does width=\textwidth mean?

I changed

\begin{subfigure}[b]{0.3\textwidth}

to

\begin{subfigure}[b]{0.2\textwidth}

but it didn't help.

Then, I changed

\includegraphics[width=\textwidth]{mouse}

to

\includegraphics[width=0.5\textwidth]{mouse}

Still no solution.

share|improve this question
1  
Could you please add information about the desired layout? –  egreg Sep 10 '13 at 15:03
    
@egreg added. all in single row. –  user31177 Sep 10 '13 at 15:09
    
An off-topic comment: You can drop all \centering instructions inside the subfigure environments since you're setting the graphs to occupy the full widths available. –  Mico Sep 10 '13 at 15:11
    
A blank line is a paragraph break. Don't leave blank lines between subfigures you want horizontally aligned. For four subfigures, use something like 0.22\textwidth. –  egreg Sep 10 '13 at 15:11
    
@Mico I've fixed the wikibook - thanks. –  Chris H Sep 10 '13 at 15:18

3 Answers 3

up vote 5 down vote accepted

Since all four subfigures must appear on one row, the total width for the subfigures must not exceed \textwidth; you could use .25\textwidth (at most, if they all have to have the same width) for each subfigure and make sure there's no spurious blank spaces between them (notice the % characters after the three first \end{subfigure} lines).

Below you find two possibilities using the maximum width for each subfigure; in the first case, the width of the image equals the width of its container (and the images will appear without space in between); in the second case, the width of each image is a little smaller than the width of the container, so there's some air between the images.

Notice that in the first case, the subfigures are taken all the available width and each image takes all the available width inside its container too, so there's no need to use \centering there.

\documentclass[12pt,twoside]{report}% Use this line for the print version of the thesis
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
        \begin{subfigure}[b]{0.25\textwidth}
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A gull}
                \label{fig:gull}
        \end{subfigure}%
        \begin{subfigure}[b]{0.25\textwidth}
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A gull2}
                \label{fig:gull2}
        \end{subfigure}%
        \begin{subfigure}[b]{0.25\textwidth}
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A tiger}
                \label{fig:tiger}
        \end{subfigure}%
        \begin{subfigure}[b]{0.25\textwidth}
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A mouse}
                \label{fig:mouse}
        \end{subfigure}
        \caption{Pictures of animals}\label{fig:animals}
\end{figure}

\begin{figure}
        \begin{subfigure}[b]{0.25\textwidth}
                \centering
                \includegraphics[width=.85\linewidth]{example-image-a}
                \caption{A gull}
                \label{fig:gull}
        \end{subfigure}%
        \begin{subfigure}[b]{0.25\textwidth}
                \centering
                \includegraphics[width=.85\linewidth]{example-image-a}
                \caption{A gull2}
                \label{fig:gull2}
        \end{subfigure}%
        \begin{subfigure}[b]{0.25\textwidth}
                \centering
                \includegraphics[width=.85\linewidth]{example-image-a}
                \caption{A tiger}
                \label{fig:tiger}
        \end{subfigure}%
        \begin{subfigure}[b]{0.25\textwidth}
                \centering
                \includegraphics[width=.85\linewidth]{example-image-a}
                \caption{A mouse}
                \label{fig:mouse}
        \end{subfigure}
        \caption{Pictures of animals}\label{fig:animals}
\end{figure}

\end{document}

enter image description here

Of course, you don't have to give each subfigure a width of .25\textwidth; you can use a smaller value (in this case, \centering will be needed) and you can use some space, (an explicit space, or \quad, or, even better, \hfill) to separate your figures:

\documentclass[12pt,twoside]{report}% Use this line for the print version of the thesis
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
        \begin{subfigure}[b]{0.22\textwidth}
                \centering
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A gull}
                \label{fig:gull}
        \end{subfigure}\hfill
        \begin{subfigure}[b]{0.22\textwidth}
                \centering
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A gull2}
                \label{fig:gull2}
        \end{subfigure}\hfill
        \begin{subfigure}[b]{0.22\textwidth}
                \centering
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A tiger}
                \label{fig:tiger}
        \end{subfigure}\hfill
        \begin{subfigure}[b]{0.22\textwidth}
                \centering
                \includegraphics[width=\linewidth]{example-image-a}
                \caption{A mouse}
                \label{fig:mouse}
        \end{subfigure}
        \caption{Pictures of animals}\label{fig:animals}
\end{figure}

\end{document}
share|improve this answer
1  
As the example from the wikibook has some non-breaking spaces (~) between the figures, 4 figures of .25\textwidth won't fit. This answer is a better way of achieving the space than the simple approach in the wikibook IMO, but the wikibook method is simpler to read. –  Chris H Sep 10 '13 at 15:20
    
In think your second example has a potential problem, in that the first and final subfigure aren't set flush with the edges of the text block. It's probably preferable to set each subfigure's width to something like 0.23\textwidth and then set the space between the subfigures to \fill. –  Mico Sep 10 '13 at 15:32
    
@Mico I don't see it as a problem. It all depends on the desired final layout. –  Gonzalo Medina Sep 10 '13 at 16:06

To place all four subfigure environments in one row (and each occupy the same width), you must set their widths to something less than 0.25\textwidth. In the example below, I use 0.24\textwidth, and I set the spacing between the subfigures to \hspace{\fill}, i.e., the maximum available amount. If you need more space between the individual subfigures, you should reduce their overall width (say, to 0.21\textwidth).

By the way, there's no need for any \centering instructions inside the individual subfigure environments, as the graphs occupy the maximum available space. Likewise, there's no need for a \centering instruction after \begin{figure} if the full width of the textblock is to be used.

enter image description here

\documentclass[12pt,twoside]{report}
\usepackage[demo]{graphicx} % remove demo option for your production document
\usepackage{subcaption}     % subcaption automaticallz loads caption package
\begin{document}
\begin{figure}
        \begin{subfigure}[b]{0.24\textwidth}
                \includegraphics[width=\linewidth]{gull}
                \caption{A gull}
                \label{fig:gull}
        \end{subfigure}%
        \hspace{\fill}
        \begin{subfigure}[b]{0.24\textwidth}
                \includegraphics[width=\linewidth]{gull}
                \caption{A gull2}
                \label{fig:gull2}
        \end{subfigure}%
        \hspace{\fill}
        \begin{subfigure}[b]{0.24\textwidth}
                \includegraphics[width=\linewidth]{tiger}
                \caption{A tiger}
                \label{fig:tiger}
        \end{subfigure}%
        \hspace{\fill}
        \begin{subfigure}[b]{0.24\textwidth}
                \includegraphics[width=\linewidth]{mouse}
                \caption{A mouse}
                \label{fig:mouse}
        \end{subfigure}
        \caption{Pictures of animals}\label{fig:animals}
\end{figure}
\end{document} 
share|improve this answer

The first thing you should note that your total width, 0.3\textwidth X 4 1.2\textwidth is more than \textwidth. So, it would be physically impossible to accommodate the images in one row.

Moreover, even if you reduce the widths to some value like 0.2\textwidth, still they fail to appear in a row. This is due the carriage returns you have inserted between images.

So, taking in to account all the above issues, your code becomes,

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.20\textwidth}
    \centering
    \includegraphics[width=\textwidth]{gull}
    \caption{A gull}
    \label{fig:gull}
  \end{subfigure}
  \begin{subfigure}[b]{0.20\textwidth}
    \centering
    \includegraphics[width=\textwidth]{gull}
    \caption{A gull2}
    \label{fig:gull2}
  \end{subfigure}
  \begin{subfigure}[b]{0.20\textwidth}
    \centering
    \includegraphics[width=\textwidth]{tiger}
    \caption{A tiger}
    \label{fig:tiger}
  \end{subfigure}
  \begin{subfigure}[b]{0.20\textwidth}
    \centering
    \includegraphics[width=\textwidth]{mouse}
    \caption{A mouse}
    \label{fig:mouse}
  \end{subfigure}
  \caption{Pictures of animals}\label{fig:animals}
\end{figure}

And the images are nicely displayed in one row.

enter image description here

For my case, I sometimes put the images for a row in one tabular row. This is just to have more control in the placements. But whether you decide to go for this depends on your personal choice. Should you decide to use this, the code is,

\begin{figure}
  \centering
  \begin{tabular}[c]{cccc}
    \begin{subfigure}[b]{0.20\textwidth}
      \includegraphics[width=\textwidth]{gull}
      \caption{A gull}
      \label{fig:gull}
    \end{subfigure}&
    \begin{subfigure}[b]{0.20\textwidth}
      \includegraphics[width=\textwidth]{gull}
      \caption{A gull2}
      \label{fig:gull2}
    \end{subfigure}&
    \begin{subfigure}[b]{0.20\textwidth}
      \includegraphics[width=\textwidth]{tiger}
      \caption{A tiger}
      \label{fig:tiger}
    \end{subfigure}&
    \begin{subfigure}[b]{0.20\textwidth}
      \includegraphics[width=\textwidth]{mouse}
      \caption{A mouse}
      \label{fig:mouse}
    \end{subfigure}
  \end{tabular}
  \caption{Pictures of animals}\label{fig:animals}
\end{figure}

And the images placements will be somewhat better. This is will allow more control which you may want to have as time passes.

enter image description here

share|improve this answer
    
In your second example, all \centering instructions inside the subfigure environments are redundant since the cell contents will be set centered because of the {cccc} argument of the tabular environment. The same holds for your first example too, because the graphs are set to occupy the full width of the subfigure environments. –  Mico Sep 10 '13 at 15:39
    
Got it, an inadvertent mistake. Updated my code for the second one. For the first one, putting a \centering still helps to evenly distribute the extra horizontal space, should there be any. –  Masroor Sep 10 '13 at 15:41
    
Re your first example: There's no "extra horizontal space" to be distributed inside the subfigure environments since the graphs inside each subfigure environment are set to take up the full width available. –  Mico Sep 10 '13 at 15:53
1  
Agreed, of course what you are saying is correct. I said, "should there be any". Just as a general rule for the user when he tries to apply the same code for other situations. –  Masroor Sep 10 '13 at 15:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.