# Extra Space Inserted After Consecutive Floating Environments

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}


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

What is the reason for this?

• Adding floats mid paragraph is possible, but I can't recommend it. Dec 13, 2020 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? Dec 13, 2020 at 14:51
• The chance that floats will fall at the desired spot is very little. Two in a row even less. Dec 13, 2020 at 14:52
• @egreg So, if I understand correctly, floats should be put before/after a paragraph? Dec 13, 2020 at 14:54
• Yes, between paragraphs is the best choice, in my opinion. Dec 13, 2020 at 14:59

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.

• 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.


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.

• Thank you very much for your answer! But why I do not need to add a % character if there is only one figure? Dec 13, 2020 at 14:07
• @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 Dec 14, 2020 at 1:15
• 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. Dec 14, 2020 at 1:20
• @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. Dec 14, 2020 at 17:22
• @Mico Thank you! Dec 14, 2020 at 22:38