1

When putting two floating environments consecutively inside a paragraph as in the example below, TeX seems to insert an additional space after the last floating environment.

\documentclass{article}

\begin{document}
There is an additional space before this
\begin{figure}
  \caption{A}
\end{figure}
\begin{figure}
  \caption{B}
\end{figure}% <--- gets rid of extra space
word.
\end{document}

Extra Space

Adding % after the last figure, gets rid of the extra space.

No Extra Space

What is the reason for this?

8
  • 2
    Adding floats mid paragraph is possible, but I can't recommend it. – egreg Dec 13 '20 at 14:48
  • @egreg Thank you for your comment! I have always thought floats should be put immediately after the text reference. Let‘s say the paragraph is very long and the two floats are referenced near the beginning. Should I then put the floats after the end of the paragraph? – EuklidAlexandria Dec 13 '20 at 14:51
  • 1
    The chance that floats will fall at the desired spot is very little. Two in a row even less. – egreg Dec 13 '20 at 14:52
  • @egreg So, if I understand correctly, floats should be put before/after a paragraph? – EuklidAlexandria Dec 13 '20 at 14:54
  • 1
    Yes, between paragraphs is the best choice, in my opinion. – egreg Dec 13 '20 at 14:59
1

There are two causes of the additional inter-word space between "this" and "word".

  • The first cause is the absence of %, i.e., a comment character, at the end the input line There is an additional space before this.

  • The second cause is the absence of % at the end of both \end{figure} lines.

Supplying a % character at the end of the first or third possible location suffices to generate a normal inter-word space. Inserting a % character at the end of all 3 lines gets rid of the inter-word space altogether.


Addendum to summarize some of the thoughts contained in the comments:

  • In a way, it is your coding choice that needlessly causes the complications (such as extra whitespace), which you then find necessary to fix. IMNSHO, there is no discernible advantage to inputting the floats as

    There is an additional space before this
    \begin{figure}
      \caption{A}
    \end{figure}
    \begin{figure}
      \caption{B}
    \end{figure}
    word.
    

    Instead, as @egreg has already suggested, just enter the material as

    There is an additional space before this word.
    \begin{figure}[ht!]
      \caption{A}
    \end{figure}
    \begin{figure}[ht!]
      \caption{B}
    \end{figure}
    

    if the objective is to keep the floats close to the text that precedes them.

  • As @DavidCarlisle has explained in the comments below, the extra inter-word whitespace isn't really the result of LaTeX wantonly putting it there; indeed, it's your oddball coding choice that's creating the extra whitespace issue. While TeX and LaTeX will successfully discard consecutive extra whitespace of the type word word, they weren't set up to ferret out and eliminate needless whitespace of the type word {} () word, where {} forms an empty group.

  • The upshot: Either get into the habit of adding % after each instance of \end{figure}, or start coding more sensibly so that the whitespace issue discussed in this posting never even arises.

12
  • Thank you very much for your answer! But why I do not need to add a % character if there is only one figure? – EuklidAlexandria Dec 13 '20 at 14:07
  • 1
    @EuklidAlexandria latex goes to some effort to check whether a space appears before or after a figure and just produce one space in either case. However you have space-figure-space-figure-space and it only manages to detect that it needs to remove one. Arguably it ought to try harder but it's always been this way and changing it might have compatibility issues – David Carlisle Dec 14 '20 at 1:15
  • 2
    note that you never have to add % you could not add space instead this\begin{figure}..\end{figure}\begin{figure} ..\end{figure} word would only have one space. – David Carlisle Dec 14 '20 at 1:20
  • 1
    @EuklidAlexandria hey latex didn't insert space, latex just failed to remove it after you added it :-) consecutive spaces are only combined if they really are consecutive compare one--two and one-{}-two or in your case one-{}-{}-two (where - is marking a space) you see even {} is enough to stop space combining and you have a lot more than that. an entire figure environment. – David Carlisle Dec 14 '20 at 17:22
  • 1
    @Mico Thank you! – EuklidAlexandria Dec 14 '20 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.