# How to justify subfig subfloats?

I have three subfloats and I want them to be aligned over the full \textwidth, no matter what the sizes are (smaller than a third of the page of course).

I have the following setup:

\begin{figure} %
\centering
\subfloat[subcaption 1]{%
\includegraphics[width=0.2\textwidth]{image1}} %
%
\subfloat[subcaption 2]{%
\includegraphics[width=0.2\textwidth]{image2}} %
%
\subfloat[subcaption 3]{%
\includegraphics[width=0.2\textwidth]{image3}} %

\caption{%
\label{fig:trans_actions} %
Some caption text here. This is the full textwidth.}
\end{figure}


How do I get the images justified over the full width of the text?

-
While code snippets are useful for explaining things, t is always best to compose a compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Nov 25 '11 at 17:39
Ok sure, I'll do that next time. I am always afraid that I'm making my examples too large and people can't read what the actual problem is anymore. –  Marnix Nov 25 '11 at 17:45

Just add \hfill to spread out the figures, which adds a rubber length that is allowed to stretch and shrink horizontally as needed.

The [showframe] option for the geometry pacakge was used to show the placement of the figures and the margins.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[demo]{graphicx}% Remove demo option in real document
\usepackage{caption}
\usepackage{subfig}

\begin{document}
\begin{figure}
\centering
\subfloat[subcaption 1]{\includegraphics[width=0.2\textwidth]{image1}}
\hfill
\subfloat[subcaption 2]{\includegraphics[width=0.2\textwidth]{image2}}
\hfill
\subfloat[subcaption 3]{\includegraphics[width=0.2\textwidth]{image3}}
\caption{\label{fig:trans_actions}
Some caption text here. This is the full textwidth.}
\end{figure}
\end{document}


As Werner pointed out, the above solution aligns the edges of the figures with the margins. If you also want spacing between the figures and margins as well you add \null\hfill on the left, and \hfill\null on the right.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[demo]{graphicx}% Remove demo option in real document
\usepackage{caption}
\usepackage{subfig}

\begin{document}
\begin{figure}
\centering
\null\hfill
\subfloat[subcaption 1]{\includegraphics[width=0.2\textwidth]{image1}}
\hfill
\subfloat[subcaption 2]{\includegraphics[width=0.2\textwidth]{image2}}
\hfill
\subfloat[subcaption 3]{\includegraphics[width=0.2\textwidth]{image3}}
\hfill\null
\caption{\label{fig:trans_actions}
Some caption text here. This is the full textwidth.}
\end{figure}
\end{document}

-
Ok that was too easy for you... I'll remember \hfill from now on. –  Marnix Nov 25 '11 at 17:40
Note that this will push the outer \subfloats against the left/right margin. If you want them to be evenly spaced from the margins as well, add \null\hfill on the left, and \hfill\null on the right. –  Werner Nov 25 '11 at 17:45
@Werner: Thanks. I have updated the solution to show that as well. –  Peter Grill Nov 25 '11 at 18:42