[Tex/LaTex] wrapfig vs intextsep

wrapfigure

This is a follow up to https://tex.stackexchange.com/a/266431/3929

Consider the MWE below. For now, ignore that we cannot provide arguments to \wrapfigure, what I'm trying to understand is this:

Why is it that if I set \intextsep globally to zero, then no extra vertical padding is added to the wrapped figure. But it is not enough to set it to zero inside it wrapper env. Ulrike has already answered by it does disappear in the very first one in the example. But try adding the section and the image will be shifted down.

Why?

\documentclass{article}

\usepackage{wrapfig}
\usepackage{kantlipsum}

\newcommand\fig{\rule{3cm}{15mm}}

%\setlength\intextsep{0pt}

\newenvironment{mw}{%
  \setlength\intextsep{0pt}
  \wrapfigure[4]{l}{3cm}
}{\endwrapfigure}

\begin{document}

%\section{Test}

\begin{mw}
  \fig%
\end{mw}
\kant[1]

\begin{mw}
  \fig%
\end{mw}
\kant[1]
\end{document}

The result of the MWE:

enter image description here

The result of adding a section

enter image description here

The result of globally setting \intextsep to zero

enter image description here

Best Answer

The documentation says

If you put a wrapfigure in a parbox or a minipage, or any other type of grouping, the text wrapping should end before the group does.

So if you put \wrapfig in your environment group you have to put your (Kant) text in there as well.