[Tex/LaTex] Placement of images in row using subfigure

graphicssubfloats

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.

Best Answer

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}