# Why is my custom wrapfig environment messed up?

I want to create a wrapper environment for wrapfigure in order to locally set \initextsep to 0. I know, it could be done differently, but this code is exhibiting strange behaviour. It moves the figure to a place after the paragraph it is defined in.

\documentclass{article}
\usepackage{lipsum}
\usepackage{wrapfig}
\begin{document}
\lipsum[1]
\newenvironment{mywrapfigure}[2]{
\begin{wrapfigure}{#1}{#2}
}
{
\end{wrapfigure}
}

\begin{mywrapfigure}{r}{.3\textwidth}
\rule{5cm}{5cm}
\end{mywrapfigure}
\lipsum[1]
\end{document}


Note that all I am doing is creating a dummy environment around wrapfigure.

How do I fix this?

• Try \wrapfigure and \endwrapfigure instead of \begin{wrapfigure} and \end{wrapfigure} wrapfigure is no real environment in the sense of \newenvironment. – user31729 Sep 8 '15 at 9:56
• Other question: Why do you want a wrapper? You're losing the important 1st and 3rd optional arguments here, which may be used for fine-control – user31729 Sep 8 '15 at 10:01
• @ChristianHupfer to set \initexsep  locally. I also find it annoying that that setting is not automatically zero inside wrapfigure – daleif Sep 8 '15 at 10:16
• @daleif: \initextsep is completely unknown to me. Is it a TeX primitive? Do you mean \intextsep? – user31729 Sep 8 '15 at 10:19
• @ChristianHupfer i don't remember. There is a description of what it's used for in the memoir manual (Peter Wilson explained most of the mechanics behind floats). It controls (AFAIK) the distance from the top of the wrapfigure cavity to the top if the inserted material. Often you want that to be zero. But is has to be set before the env is used. I haven't tried, but perhaps one could make an interface keeping wrapper using xparse – daleif Sep 8 '15 at 10:24

Perhaps this is the wrapper wanted: I added an optional 5th argument holding the \intexsep value, defaulting here to 0pt.

The wrapfig environment isn't a real environment defined with \newenvironment. In this case, it's better to use the fake environment starter and end commands \wrapfigure and \endwrapfigure.

\documentclass{article}
\usepackage{blindtext}
\usepackage{wrapfig}

\usepackage{xparse}

\NewDocumentEnvironment{mywrapfigure}{O{}mO{\wrapoverhang}mO{0pt}}{%
\setlength{\intextsep}{#5}%  Defaults to 0pt \plus 0pt \minus 0pt
\wrapfigure[#1]{#2}[#3]{#4}%
}{%
\endwrapfigure%
}

\begin{document}

\begin{mywrapfigure}[8]{r}[20pt]{.4\textwidth}
\rule{5cm}{2cm}
\end{mywrapfigure}
\blindtext[1]

\hrule
\vskip\baselineskip

• The default value for the first optional argument is empty; for the second one it is \wrapoverhang. – egreg Sep 8 '15 at 10:55