# Where does the space before wrapfigure comes from and how can it be eliminated?

The following MWE's code (taken from here and slightly modified: added minipages around) should demonstrate the details:

\documentclass{article}
\usepackage{lipsum,tikz,wrapfig}

\setlength\intextsep{0pt}%<- doesn't seem to matter

\begin{document}
%
\fbox{%
\begin{minipage}[t]{0.45\textwidth}%
\begin{wrapfigure}{r}{4cm}%
\begin{tikzpicture}%
\fill (0, 0) rectangle (4, 4);%
\end{tikzpicture}%
\end{wrapfigure}%

\lipsum[1]%
\end{minipage}%
}%
%
\fbox{%
\begin{minipage}[t]{0.45\textwidth}%
%   \begin{wrapfigure}{r}{4cm}%
%       \begin{tikzpicture}%
%       \fill (0, 0) rectangle (4, 4);%
%       \end{tikzpicture}%
%   \end{wrapfigure}%

\lipsum[1]%
\end{minipage}%
}%
\end{document}


Screenshot:

Question: How can the gap (see the red questionmark within the screenshot) be eliminated? (My build-environment: Win7-64, Texstudio, TexLive2017, LuaLatex)

Update 1:

According cfr's comments I've added fboxes around the minipages. This shows that there is no space added - instead the minipage environment seems to be influenced by wrapfig: Please see the following screenshot.

• \intextsep certainly matters. Try setting it to 100pt!
– cfr
Jun 17 '17 at 0:26
• I don't think it inserts extra space. I think it alters the baseline. Try putting \fboxes around the minipages to see what I mean. (Not sure about this, though.) wrapfig inserts a \par with an \hrule, I think, to set the width of the box to the required value. If you put a letter before the wrapfig, it is possibly a little clearer. The \par doesn't add any vertical space, but I think it changes the location of t relative to the minipage. However, I don't see why this should affect anything outside the box ....
– cfr
Jun 17 '17 at 0:36
• In case it is useful, you don't get the same problem if you use coffins rather than mini-pages. However, mixing a coffin and a wrapfig is possibly not the best idea.
– cfr
Jun 17 '17 at 0:57
• @cfr thanks for your analysis! I've addeed fboxes around and now I see: wrapfig influences the minipage. Should I update the question accordingly? Jun 17 '17 at 6:47
• Possibly a duplicate of tex.stackexchange.com/a/27699. Try \begingroup \setlength\intextsep{0pt} ... \endgroup
– Ross
Jun 17 '17 at 7:09

You are top-aligning your minipages. If a minipage (or some other box) starts with normal text then "top-aligning" means that its baseline is at the baseline of the first text line. But if it starts with some \vspace then this determines the baseline.

Inside a minipage wrapfig adds at first a zero \vspace. The easiest way to "avoid" the gap is to add a similar space to the other minipage, to adjust their baseline in a similar way:

\documentclass{article}
\usepackage{lipsum,tikz}
\usepackage{wrapfig}

\setlength\intextsep{0pt}
\begin{document}

\fbox{%
\begin{minipage}[t]{0.32\textwidth}
\begin{wrapfigure}{r}{2cm}%
\begin{tikzpicture}%
\fill (0, 0) rectangle (2, 2);%
\end{tikzpicture}%
\end{wrapfigure}%

\lipsum[1]%
\end{minipage}%
}%
%
\fbox{%
\begin{minipage}[t]{0.32\textwidth}%
\vspace{0pt}
\lipsum[1]%
\end{minipage}%
}%
\fbox{%
\begin{minipage}[t]{0.32\textwidth}%
\lipsum[1]%
\end{minipage}%
}%

\end{document}


• @UlrikeFischer thank you very much, especially for the explanation! Jun 17 '17 at 16:56
• @yo' Yes a strut is probably not a bad idea. Jun 17 '17 at 17:00

I fixed it ;)

\documentclass{article}
\usepackage{lipsum,tikz,wrapfig}

\setlength\intextsep{-1.8pt}%<- doesn't seem to matter

\begin{document}

\begin{minipage}[t]{0.45\textwidth}%
\begin{wrapfigure}{r}{4cm}%
\begin{tikzpicture}%
\fill (0, 0) rectangle (4, 4);%
\end{tikzpicture}%
\end{wrapfigure}%
\vspace*{-6.7pt}

\lipsum[1]%
\end{minipage}%
\begin{minipage}[t]{0.45\textwidth}%
%   \begin{wrapfigure}{r}{4cm}%
%       \begin{tikzpicture}%
%       \fill (0, 0) rectangle (4, 4);%
%       \end{tikzpicture}%
%   \end{wrapfigure}%

\lipsum[1]%
\end{minipage}%
\end{document}


Just uploaded for you to see if you can figure out what that numbers needed are...

• This does not really identify the problem and will, as you presumably know, be extremely fragile, breaking in all kinds of cases. I'd rather take my chances with a wrapfigure in a coffin, to be honest.
– cfr
Jun 17 '17 at 3:29
• lol... I am not so good yet... I thought you may figure out something that I can not... Hahaha... Now I can't remove it... but may be try to find an equation giving the numbers till I die... lol Jun 17 '17 at 3:54
• @koleygr Do you have any proposals for the equation? Jun 18 '17 at 7:24
• @Léo Léopold Hertz 준영 I don't think I will find it in the next two or three years.... But if later I need this option and has still the bug... I will try :) Jun 18 '17 at 7:32

Why use the boxes at all? Is there an actual function in the environment for them? If not, omit them: simple is best. Employ vspace, within the wrapfig environment, with a positive or negative value, to raise or lower the level of the figure to match the level of the text.