[Tex/LaTex] Understanding the difference between \hspace and \hspace* when placing subfigures side-by-side

spacingsubcaption

Why is there a difference in the output when using \hspace* instead of \hspace despite the fact that the subfigures widths and horizontal spacing add to \textwidth?

\documentclass{article}
\usepackage{subcaption,mwe}
\begin{document}
    \begin{figure}
        \centering
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-a}
        \end{subfigure}%
    \hspace{0.05\textwidth}
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-b}
        \end{subfigure}%
    \hspace{0.05\textwidth}
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-c}
        \end{subfigure}
    \end{figure}

    \begin{figure}
        \centering
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-a}
        \end{subfigure}%
    \hspace*{0.05\textwidth}
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-b}
        \end{subfigure}%
    \hspace*{0.05\textwidth}
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-c}
        \end{subfigure}
    \end{figure}
\end{document}

enter image description here

Best Answer

The row of images is too wide for the line. \hspace introduces a breakpoint so the line breaks after the B and the space is discarded at the start of the next line.

with \hspace* no line break is allowed and so they stay on one line producing an overfull line

Overfull \hbox (4.44969pt too wide) in paragraph at lines 21--32

It is overfull due to the word spaces added after the \hspace.

enter image description here

\documentclass{article}
\usepackage{subcaption,mwe}
\begin{document}
    \begin{figure}
        \centering
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-a}
        \end{subfigure}%
    \hspace{0.049\textwidth}%
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-b}
        \end{subfigure}%
    \hspace{0.049\textwidth}%
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-c}
        \end{subfigure}
    \end{figure}

    \begin{figure}
        \centering
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-a}
        \end{subfigure}%
    \hspace*{0.05\textwidth}%
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-b}
        \end{subfigure}%
    \hspace*{0.05\textwidth}%
        \begin{subfigure}[t]{0.3\textwidth}
            \includegraphics[width=\linewidth]{example-image-c}
        \end{subfigure}
    \end{figure}
\end{document}

Note with \hspace{0.05\textwidth} rounding error makes it slightly over full so I had to reduce it a bit. In practice it is best not to use such fixed lengths and just use \hfill between the images, this will expand to space out the images without needing to worry about rounding error.