[Tex/LaTex] Wrapfigure affects the rest of the document in environments

wrapfigure

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.

Best Answer

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):

enter image description here

\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'
\global\advance\c@WF@wrappedlines-\prevgraf \prevgraf\z@
\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}
Related Question