# spacing when using subfloat

Consider this example:

\documentclass{report}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{lipsum}
\begin{document}
\begin{figure}[htp]
\centering
\def\twidth{0.45}
\subfloat[{\lipsum[1]}]{
\includegraphics[width=\twidth\textwidth]{example-image-a}
}
\subfloat[{\lipsum[2]}]{
\includegraphics[width=\twidth\textwidth]{example-image-b}
}
\end{figure}
\end{document}


In the output:

the spacing between the subcaptions blurs. I could manually fix by inserting \hspace{0.2cm} after the first subfloat.

Is there a better, automatic way of inserting a spacing between the subfloats?

There are two problems with your code.

Each image has a normal interword space on either side, generated by the endlines inside \subfloat and you should watch out for them.

Second, the working of \subfloat ends with \ignorespaces, so the endline after the closing brace of the first \subfloat doesn't generate space between the two parts.

You can insert instead \hfill so the two images will be pushed to the margins leaving 0.1\textwidth space in the middle. Or you can use \hfil and you will have three equal spaces, left, center and right.

With the help of showframe we can see the boundaries of the text block. Choose a style and stick to it.

\documentclass{report}
\usepackage{subfig}
\usepackage{graphicx,showframe}
\usepackage{lipsum}
\begin{document}

\begin{figure}[htp]
\centering
\def\twidth{0.45}
\subfloat[{\lipsum[1][1-3]}]{%
\includegraphics[width=\twidth\textwidth]{example-image-a}%
}\hfil
\subfloat[{\lipsum[2][1-3]}]{%
\includegraphics[width=\twidth\textwidth]{example-image-b}%
}
\end{figure}

\begin{figure}[htp]
\centering
\def\twidth{0.45}
\subfloat[{\lipsum[1][1-3]}]{%
\includegraphics[width=\twidth\textwidth]{example-image-a}%
}\hfill
\subfloat[{\lipsum[2][1-3]}]{%
\includegraphics[width=\twidth\textwidth]{example-image-b}%
}
\end{figure}

\end{document}


Don't forget the % bits in the places I added them.

There is a third possibility: setting some margin for the subcaptions.

\documentclass{report}
\usepackage{subfig}
\usepackage{graphicx,showframe}
\usepackage{lipsum}

\captionsetup[subfloat]{margin=1em}

\begin{document}

\begin{figure}[htp]
\centering
\def\twidth{0.45}
\subfloat[{\lipsum[1][1-3]}]{%
\includegraphics[width=\twidth\textwidth]{example-image-a}%
}
\subfloat[{\lipsum[2][1-3]}]{%
\includegraphics[width=\twidth\textwidth]{example-image-b}%
}
\end{figure}

\end{document}


• Should \twidth be adjusted manually in order that after the extra spaces are added a newline is not started or is there some recipe? – Viesturs Jan 20 at 11:37
• @Viesturs To start a new row of subfloats, leave a blank line. – egreg Jan 20 at 12:04
• in case I use also \caption{} and \label{} do they also need a trailing %? – Viesturs Jan 23 at 16:56
• @Viesturs No, why? – egreg Jan 23 at 17:02
• @Viesturs % should be added when the endline would give an unwanted space. – egreg Jan 26 at 21:28

A solution with floatrow:

\documentclass{report}
\usepackage[showframe]{geometry}
\usepackage{subfig}
\usepackage{floatrow}
\usepackage{graphicx}
\usepackage{lipsum}

\DeclareFloatSeparators{Fil}{\hspace*{0.1\textwidth}}

\begin{document}

\begin{figure}[htp]
\floatsetup[figure]{floatrowsep=Fil, objectset=centering, margins=centering}%
\centering
\begin{subfloatrow}
\ffigbox[\FBwidth]{\includegraphics[width=0.45\textwidth]{example-image-a}}{\caption{\lipsum[1]}}
\ffigbox[\FBwidth]{\includegraphics[width=0.45\textwidth]{example-image-b}}{\caption{\lipsum[2]}}
\end{subfloatrow}
\end{figure}

\end{document}


With the method you used, the caption text will be misaligned with the figure. For your particular case, it is not noticeable, but for other cases, it could be.

You can use \hfill which will 'horizontally' fill a space between the two figures in such a way that each one will be on one end of the text width

A more advanced way can be useful if you have multiple figures. Use \hspace{\stretch{<factor>}} to specify a share for each given spacing. For instance, if you use \hspace{\stretch{2}} and \hspace{\stretch{5}}, then the second spacing will be 5/2 = 2.5 times larger than the first one like this

\documentclass{report}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{float}
\usepackage{lipsum}
\usepackage[showframe=true]{geometry}

\begin{document}

\begin{figure}[htp]

\centering

\def\twidth{0.45}
\subfloat[{\lipsum[1]}]{
\includegraphics[width=\twidth\textwidth]{example-image-a}
}
%
\hfill
\subfloat[{\lipsum[2]}]{
\includegraphics[width=\twidth\textwidth]{example-image-b}
}
\end{figure}

\begin{figure}

\centering

\subfloat[{\lipsum[1]}]{
\includegraphics[width=0.25\textwidth]{example-image-a}
}
%
\hspace{\stretch{2}}
\subfloat[{\lipsum[2]}]{
\includegraphics[width=0.25\textwidth]{example-image-b}
}
%
\hspace{\stretch{5}}
\subfloat[{\lipsum[2]}]{
\includegraphics[width=0.25\textwidth]{example-image-c}
}
\end{figure}

\end{document}