12

In this MWE, I have five elements on a line: three figures with 32% each, and two spaces with 2% each. 3*32+2*2=100. So I expect these items to fit on one line:

\documentclass{article}
%\usepackage[showframe]{geometry}
\usepackage{mwe}
\begin{document}%
    $32+2+32+2+32=100$, right?
    \begin{figure}%
        \includegraphics[width=0.32\linewidth]{example-image-a}%
        \hspace{0.02\linewidth}%
        \includegraphics[width=0.32\linewidth]{example-image-a}%
        \hspace{0.02\linewidth}%
        \includegraphics[width=0.32\linewidth]{example-image-a}%
    \end{figure}%
\end{document}%

Why don't they?

enter image description here

Bonus question: What influence does the geometry package have?

enter image description here

0

1 Answer 1

22

You're losing sight of the fact that sums of real (double-entendre intended) calculations with non-integers may introduce rounding error issues.

With TeX, all internal length calculations are done in terms of multiples of sp units, where 1pt=65536sp. Depending on the exact value of \linewidth, 3(0.32\linewidth)+2(0.02\linewidth) may work out to be either slightly more or slightly less than 1\linewidth. In one case, i.e., with the document class-defined default value of \linewidth, the rounded sum is slightly more than 1\linewidth; hence, the third graph gets kicked to the next line. If geometry is loaded, the default value of \linewidth is larger. It also so happens that with the modified default, the rounded sum is slightly less than 1\linewidth -- and all three graphs happily fit on one line.

At any rate, the issue wouldn't arise with proper coding technique: Use \hfill instead of \hspace{0.02\linewidth} to separate the graphs.

\begin{figure}
    \includegraphics[width=0.32\linewidth]{example-image-a}%
    \hfill
    \includegraphics[width=0.32\linewidth]{example-image-a}%
    \hfill
    \includegraphics[width=0.32\linewidth]{example-image-a}%
\end{figure}
6
  • 2
    I would like to upvote twice: once for the explanation, and once for the workaround with \hfill!
    – bers
    Commented Dec 7, 2016 at 22:50
  • 2
    you need just \hfill (hence the %) as there is not room for two word spaces) Commented Dec 7, 2016 at 22:52
  • 1
    For anyone interested: One simple example to try, e.g., in MATLAB, is 0.1+0.1+0.1-0.3, which equals 5.5511e-17 (from stackoverflow.com/questions/249467/…)
    – bers
    Commented Dec 7, 2016 at 23:13
  • Another comment for those who would like to use different spacings, such as \hspace{0.02\linewidth} and \hspace{0.035\linewidth}: use \hfill\hfill\hfill\hfill and \hfill\hfill\hfill\hfill\hfill\hfill\hfill.
    – bers
    Commented Dec 7, 2016 at 23:20
  • 1
    @bers: … or rather, \hspace{\stretch{.2}} and \hspace{\stretch{.35}}.
    – GuM
    Commented Dec 7, 2016 at 23:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .