# why subfigure doesn't occupy the full textwidth

I have 5 subfigures with width set to be 0.2 \textwidth, I want them to be in one row, but I have to set their width smaller than 0.2 to do that. Does anyone know the reason? Thank you!

• Hi and welcome, you might have spaces in between them. But it is really hard to tell, without any code. Can you present a minimal working example? – Johannes_B May 2 '15 at 22:49
• With standard paper and margins, you only have 6.5in of textwidth. So you have have 6.5/5 - 4*(gap distance desired) between each picture for the max width of each picture or some calculation of this nature to determine max size. You can adjust the margins with the geometry. – dustin May 2 '15 at 22:53
• Which package do you use to create subfigures: subfigure (obsolete/deprecated), subfig, or subcaption? – Mico May 2 '15 at 23:02

## 1 Answer

You didn't state which package you use to help create the subfigure environments: subfigure -- which is deprecated and ought not to be used anymore -- subfig, or subcaption. I'll assume you're using subcaption.

One needs to remember that TeX converts single line breaks into space tokens. Thus, if each subfigure environment is terminated with a newline directive, the total width is 5*0.2\textwidth + 4*(width of space token), which exceeds \textwidth.

You have two choices: insert a % (comment character) at the end of the first four subfigure environments (to suppress the implicit insertion of a space character) or choose a width for each subfigure that's slightly less than 0.2\textwidth. I actually prefer the second approach.

\documentclass{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx}
\begin{document}

\hrule  %% just to illustrate width of text block

%% Five subfigures, width 0.2\textwidth,
%% use "%" to assure no gap,
%% set image widths to 0.95\textwidth
\begin{figure}[h!]
\begin{subfigure}{0.2\textwidth}
\centering
\includegraphics[width=0.95\textwidth]{figa}
\caption{First}
\end{subfigure}%
\begin{subfigure}{0.2\textwidth}
\centering
\includegraphics[width=0.95\textwidth]{figb}
\caption{Second}
\end{subfigure}%
\begin{subfigure}{0.2\textwidth}
\centering
\includegraphics[width=0.95\textwidth]{figc}
\caption{Third}
\end{subfigure}%
\begin{subfigure}{0.2\textwidth}
\centering
\includegraphics[width=0.95\textwidth]{figd}
\caption{Fourth}
\end{subfigure}%
\begin{subfigure}{0.2\textwidth}
\centering
\includegraphics[width=0.95\textwidth]{fige}
\caption{Fifth}
\end{subfigure}
\end{figure}

%% Five subfigures, width 0.18\textwidth,
%% use "\hspace{\fill}" to maximize gaps,
%% set image widths to 1\textwidth
\begin{figure}[h!]
\begin{subfigure}{0.18\textwidth}
\includegraphics[width=1\textwidth]{figa}
\caption{First}
\end{subfigure}\hspace{\fill}
\begin{subfigure}{0.18\textwidth}
\includegraphics[width=1\textwidth]{figb}
\caption{Second}
\end{subfigure}\hspace{\fill}
\begin{subfigure}{0.18\textwidth}
\includegraphics[width=1\textwidth]{figc}
\caption{Third}
\end{subfigure}\hspace{\fill}
\begin{subfigure}{0.18\textwidth}
\includegraphics[width=1\textwidth]{figd}
\caption{Fourth}
\end{subfigure}\hspace{\fill}
\begin{subfigure}{0.18\textwidth}
\includegraphics[width=1\textwidth]{fige}
\caption{Fifth}
\end{subfigure}
\end{figure}

\end{document}

• Amazing! I was thinking that there is something in between. – godblessfq May 4 '15 at 3:37