10

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:

enter image description here

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.

enter image description here

lAtExFaN
  • 1,131
  • 2
    \intextsep certainly matters. Try setting it to 100pt! – cfr Jun 17 '17 at 00:26
  • 2
    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 00:36
  • 2
    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 00: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? – lAtExFaN Jun 17 '17 at 06:47
  • 1
    Possibly a duplicate of https://tex.stackexchange.com/a/27699. Try \begingroup \setlength\intextsep{0pt} ... \endgroup – Ross Jun 17 '17 at 07:09
  • @Ross I already tried this, without success. The "possible duplicate" seems to be a duplicate of the question I've linked in my question. – lAtExFaN Jun 17 '17 at 07:24
  • You could do, yes. It might mean something to somebody. My default diagnostics in these kinds of cases is drawing around boxes. However, in this case, I still can't understand what's happening. I don't understand why wrapfig would be affecting stuff outside the minipage which contains it :(. – cfr Jun 17 '17 at 12:42
  • @cfr I just found a similar (I think not exactly duplicate) question. Adapting the solution/workaround provided in the answer (https://tex.stackexchange.com/a/309452/112503) using adjustbox makes the gap smaller (but negative) but does not eliminate it. – lAtExFaN Jun 17 '17 at 13:02

3 Answers3

10

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}

enter image description here

Ulrike Fischer
  • 327,261
2

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

enter image description here

koleygr
  • 20,105
  • 2
    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 03:29
  • 1
    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 – koleygr Jun 17 '17 at 03:54
  • @koleygr Do you have any proposals for the equation? – Léo Léopold Hertz 준영 Jun 18 '17 at 07:24
  • 1
    @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 :) – koleygr Jun 18 '17 at 07:32
0

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.