[Tex/LaTex] spacing when using subfloat

spacingsubfigsubfloats

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:
enter image description here

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?

Best Answer

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.

image

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}

enter image description here

Related Question