# Theorem-like environment for wrapfigure without trivlist

I would like to be able to define a theorem-like environment that has the same spacing as the normal theorems, but does not use trivlist to get the spacing. The reason is that I would like to place a wrapfigure to the right of such theorems (which I would use for examples, exercises etc.), which is not possible with list(-like) environments.

\documentclass[a4paper]{article}
\usepackage{graphicx, wrapfig, lipsum}

\newcounter{exercise}[section]
\renewcommand{\theexercise}{\thesection.\arabic{exercise}}
\newenvironment{exercise}[1][]{\refstepcounter{exercise}\par\medskip

\begin{document}
\section{Section One}
\begin{wrapfigure}{r}{0pt}
\includegraphics[scale=0.5]{example-image-c}
\end{wrapfigure}
\begin{exercise}
Vivamus vehicula leo a justo. Quisque nec augue.
Morbi mauris wisi, aliquet vitae. Vivamus vehicula leo a justo.
Quisque nec augue. Morbi mauris wisi, aliquet vitae.
\end{exercise}

\clearpage
\section{Section Two}
\begin{wrapfigure}{r}{0pt}
\includegraphics[scale=0.5]{example-image-c}
\end{wrapfigure}
\begin{exercise}
\lipsum[66-67]
\end{exercise}

\end{document}


Strangely, the wrapping does not work my example text, but it does for lipsum paragraphs.

Is it possible (with medium knowledge of LaTeX) to define such an environment with equally robust spacing above and below, as well as correct paragraph wrapping? – Could you show me how?

I really need this and appreciate any help.

Wrapfig is implemented using \everypar and \parshape. Adding a terminating \par inside the exercise helps, but that might be peculiar to that environment.

The undocumented macro \WFclear stops wrapfig from continuing when it gets confused.

\documentclass[a4paper]{article}
\usepackage{graphicx, wrapfig, lipsum}

\newcounter{exercise}[section]
\renewcommand{\theexercise}{\thesection.\arabic{exercise}}
\newenvironment{exercise}[1][]{\refstepcounter{exercise}\par

\intextsep=0pt% easy solution

\begin{document}
\section{Section One}
\begin{wrapfigure}{r}{0pt}
\raisebox{-\intextsep}[\dimexpr \height-2\intextsep][\depth]{% alternate solution
\includegraphics[scale=0.5]{example-image-c}}%
\end{wrapfigure}
\begin{exercise}
Vivamus vehicula leo a justo. Quisque nec augue.
Morbi mauris wisi, aliquet vitae. Vivamus vehicula leo a justo.
Quisque nec augue. Morbi mauris wisi, aliquet vitae.
\end{exercise}
\WFclear
\clearpage

\section{Section Two}
\begin{wrapfigure}{r}{0pt}
\raisebox{-\intextsep}[\dimexpr \height-2\intextsep][\depth]{% negative depth not useful
\includegraphics[scale=0.5]{example-image-c}}%
\end{wrapfigure}
\begin{exercise}
Vivamus vehicula leo a justo. Quisque nec augue.
Morbi mauris wisi, aliquet vitae. Vivamus vehicula leo a justo.
Quisque nec augue. Morbi mauris wisi, aliquet vitae.
\end{exercise}
\sloppy
\lipsum[1]
\WFclear
\lipsum[2]

\end{document}

• The problem with just sticking a \medskip before and after the environment is that the spacing is wrong when it is immediately followed or preceded by another similar (theorem, lemma, etc) environment. Is there a way of emulating \trivlist's spacing more closely? – Martin Hairer Oct 8 '18 at 17:21
• Are you talking about the \intextsep gap above and below wrapfig? You also might try putting the exercise inside a minipage. (There is a \if@minipage flag which is used by \trivlist.) – John Kormylo Oct 9 '18 at 1:42
• What I mean is that with this definition of the exercise environment, you end up with two \medskip's if you have one exercise after the other. The theorem environments based on trivlist on the other hand seem to give much more consistent spacing. – Martin Hairer Oct 9 '18 at 7:36
• I got rid of the extra \medskips. You might experiment with using \hrule height0pt instead of \par. – John Kormylo Oct 9 '18 at 14:39