# Wrapfigure affects the rest of the document in environments

I have encountered a strange behaviour of wrapfigure, that I have isolated in the following example. I don't know if it's a bug or not.

I have this latex source:

\documentclass[a4paper]{article}

\usepackage[czech]{babel}

\usepackage{graphicx}
\usepackage{wrapfig}

\newenvironment{myenvironment}{}{}

\def\poisonousparagraph{

\begin{myenvironment}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at rhoncus libero. Nunc auctor neque ut urna sodales vestibulum.

\end{myenvironment}

}

\begin{document}

\begin{wrapfigure}{r}{0.5\textwidth}
\begin{center}
\includegraphics[width=0.48\textwidth]{some_picture}
\end{center}

\caption{Lorem ipsum}
\end{wrapfigure}

\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph

\end{document}


Instead of the paragraphs with Lorem ipsums wrapping nicely, they are only a half-page wide in the entire document, exactly like this (link to my server with the PDF).

What, however, "fixes" the problem is making the text in poisonousparagraph longer. Or, if I delete the myenvironment altogether. However, in real example, I do need to use custom environments and I do need to use short text like this.

What is going on exactly?

edit: as I have tried, it has nothing to do with the picture itself, if I change it to a .png one, it still does the same thing. Also, it behaves the same when using both latex and pdflatex.

-
This behavior is mentioned in the documentation (which is at the end of the file wrapfig.sty)‌​: "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". –  Philippe Goutet Aug 19 '11 at 6:43
Ah, thank you, I haven't probably read that. –  Karel Bílek Aug 19 '11 at 13:27

Putting at the end of \poisonousparagraph what the content of the internal wrapfig command \WF@mypar does seems to do the trick (I've replaced the image by a rule of about the same size):

\documentclass[a4paper]{article}

\usepackage[czech]{babel}

\usepackage{graphicx}
\usepackage{wrapfig}

\newenvironment{myenvironment}{}{}

\makeatletter
\def\poisonousparagraph{

\begin{myenvironment}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at rhoncus libero. Nunc auctor neque ut urna sodales vestibulum.

\end{myenvironment}
\@@par
\ifnum\@@parshape=\z@ \let\WF@pspars\@empty \fi % reset parshape'
\ifnum\c@WF@wrappedlines<\tw@ \WF@finale \fi

}
\makeatother

\begin{document}

\begin{wrapfigure}{r}{0.5\textwidth}
\begin{center}
\rule{0.48\textwidth}{2cm}
\end{center}

\caption{Lorem ipsum}
\end{wrapfigure}

\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph
\poisonousparagraph

\end{document}
`
-
I absolutely don't understand why that works, but it does. Thanks! –  Karel Bílek Aug 19 '11 at 13:41