6

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

Diaa
  • 9,599
  • 1
    In addition to what David says, you need % following the \hspace and \hspace* invocations. To see this, employ \usepackage[pass,showframe]{geometry}. Other than that, it is round-off that is getting you. If you add the % I speak of and reduce the \hspace to 0.0499\textwidth, it will fit just fine. – Steven B. Segletes Apr 26 '17 at 18:51
  • @StevenB.Segletes I am sorry, but it is frequently said that there is some sort of round-off I couldn't see. What I input or assume is 3 * 0.32\textwidth + 2 * \hspace{0.02\textwidth} = \textwidth, could you explain a bit to me how Latex understands or interprets my input? – Diaa Apr 26 '17 at 22:58
  • 2
    @DiaaAbidou your sum does not relate to what you put on the line. A B C is two word spaces wider than ABC and your line is two word spaces wider than your calculation suggests, for the same reason. (Rounding error means that it is not quite equal in any case but the 4.5pt overshoot is due to the additional space not the rounding error) – David Carlisle Apr 26 '17 at 23:13

1 Answers1

9

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.

David Carlisle
  • 757,742
  • Many thanks for your answer. I appreciate your explanation. Another question if you don't mind, I always don't look at the overfull warnings, is it OK as long as the final document is fine to me? – Diaa Apr 26 '17 at 19:06
  • 1
    @DiaaAbidou Just to show how rounding can lead to spurious overfull or underfull boxes, consider \setbox0=\hbox to 1in{}\setbox2=\hbox to 2in{\copy0\copy0}, that should be OK, but results in an underfull box (by 1sp, where 65536sp are 1pt). – egreg Apr 26 '17 at 20:25
  • @egreg I appreciate your response, but I would lie if I said that I can understand or imagine what you technically mean. I will try to read about it to better understand. – Diaa Apr 26 '17 at 22:52
  • 1
    @DiaaAbidou you should care about the warnings as TeX only warns if the boxes are worse than user-set limits, so if it warns then they are worse than values you have set (or could have set) see https://tex.stackexchange.com/questions/50830/do-i-have-to-care-about-bad-boxes/50850#50850 – David Carlisle Apr 26 '17 at 23:08
  • 1
    @DiaaAbidou in this case the right hand edge of C image is 4.5pt into the right hand margin which is an appreciable amount and would be very noticeable if there was surrounding text justified to the margins to make the correct margin line clear. – David Carlisle Apr 26 '17 at 23:11