[Tex/LaTex] possible bug with wrapfig (just before section starts)

wrapfigure

I appear to have come across a bug using wrapfig. If I use a wrapfig environment immediately before a \section{...} command, then I get some bad behaviour, which appears to go away if I add some text after the wrapfig environment.

\documentclass[12pt,a4paper]{article}
\usepackage{todonotes}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{fullpage}

\begin{document}

\lipsum[1]

\begin{wrapfigure}{r}{0.6\textwidth}
\missingfigure{}
\caption{a figure}
\end{wrapfigure}

%This text fixes it.
\section*{Lorem}


\lipsum[2-3]

\end{document}

This gives enter image description here

Note the extra space in the final paragraph. If however I uncomment "This text fixes it", I get

enter image description here

I did a (brief) search and found nothing about this. What's going on?

Best Answer

The documentation of wrapfig tells you not to place a wrapfigure immediately before a list environment. The problem with \section is, however, the same: both wrapfigure and \section set \everypar, which means “fail”.

Just add wrapfigure after the section title.

\documentclass[12pt,a4paper]{article}
\usepackage{todonotes}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{fullpage}

\begin{document}

\lipsum[1]

\section*{Lorem}

\begin{wrapfigure}{r}{0.6\textwidth}
\missingfigure{}
\caption{a figure}
\end{wrapfigure}

\lipsum[2-3]

\end{document}

enter image description here