[Tex/LaTex] Textwidth wrong after wrapfigure

mdframedwrapfigure

I have included a picture with wrapfigure and everything is fine – the problem is that in my next formula environment, the formula is totally wrong! It seems to me, that the width which I set in the wrapfigure environment also effects the formula. How can I set the width back again to standard?

Here is my code:

\documentclass{book}
\usepackage{graphicx}
\usepackage{amsthm}
\usepackage{wrapfig}
\usepackage{mdframed}

\newtheoremstyle{mystyle}{}{}{}{}{}{}{0.5em}{}
\theoremstyle{mystyle}
\newmdtheoremenv{formel}{Formel}

\begin{document}
\begin{wrapfigure}{l}{8cm}
  \includegraphics[scale=0.48]{example-image-a}
\end{wrapfigure}
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
\begin{formel}[Formel]
$1+1=2$
\end{formel}
\end{document}

So the text wrapping around the figure is OK, but then width of the formula is wrong. It seems to me, that textwidth is set to the wrapfigure's textwidth, what results in a wrong display.

Best Answer

To cite the manual of the wrapfig package, page 1:

You must not specify a wrapfigure in any type of list environment or or immediately before or immediately after one. It is OK to follow a list if there is a blank line (\par) in between.

[...]

\linewidth is now adjusted within the wrapped text, but since it can only be set for whole paragraphs at a time, it will persist with the wrong value after the wrapping, until the paragraph is finished.

And at page 2:

For esthetic reasons, only plain text should wrap around the figure. Section titles and big equations look bad; lists are bad if the figure is on the left. (All these function properly, they just don’t look very good.) Small equations look fine.

Thus: Put a \par before \begin{formel}:

\documentclass{book}
\usepackage{graphicx}
\usepackage{amsthm}
\usepackage{wrapfig}
\usepackage{mdframed}

\newtheoremstyle{mystyle}{}{}{}{}{}{}{0.5em}{}
\theoremstyle{mystyle}
\newmdtheoremenv{formel}{Formel}

%\usepackage{picins}

\begin{document}
\begin{wrapfigure}{l}{8cm}
  \includegraphics[scale=0.48]{example-image-a}
\end{wrapfigure}
text 
\par
\makeatletter
\loop
\ifnum\c@WF@wrappedlines>3
\hspace*{1sp}\newline
\advance\c@WF@wrappedlines by -1
\repeat
\makeatother
\par
\begin{formel}[Formel]
$1+1=2$
\end{formel}
\end{document}

If the text to be wrapped is too short, the formula would be placed in the wrapped text, where it has the smaller width, of course. (And an additional \usepackage{picins} is needed, otherwise the formula is printed "over" the picture.) Using \c@WF@wrappedlines of the wrapfig package and automatically inserting the appropriate number of empty lines fixes this.