# Using WrapFigure near an environment. (Some text inside environment is not wrapped by the wrapfig)

To count words in a document, I am using a custom environment, as suggested here on tex.stackexchange. As can be seen below, when this environment is used next to a wrapfigure, the wrapfigure fails to wrap the text correctly.

Below is the PNG file used in this MWE. In my original document, I am using the PDF file created by TikZ, but this is easier to host and has the same effect save for poor quality rasterisation.

The MWE follows.

\documentclass[a4paper]{article}
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{xesearch}
\usepackage{enumitem}

\newcounter{words}
\newenvironment{counted}{%
\setcounter{words}{0}
\SearchList!{wordcount}{\stepcounter{words}}
{a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}
\UndoBoundary{'}
\SearchOrder{p;}}{%
\StopSearching}

\begin{document}
\begin{wrapfigure}{r}{0.5\textwidth}
\centering
\includegraphics{fig1.png}
\caption{Caption}
\label{fig:1a}
\end{wrapfigure}

\begin{counted}
We assume that point $\mathrm{P}$ is far enough from the dipole such that the $\mathbf{B}$ field obeys the equation for a dipole aligned along the $z$-axis:
As can be seen by inspection, the magnetic field lines encircle the $z$-axis and therefore a circular wire loop centred at any point along the axis would have a net zero flux passing through the centre -- no signal would be detected.\\

The wire loop must be aligned in a plane normal to the magnetic flux. If the loop were aligned parallel to the flux, then no flux would pass \emph{through} the loop. In accordance with Faraday's Law, this would mean that no electric field would be found integrating along the path of the loop -- i.e. no current would be induced and therefore no signal detected. It is important to note that the direction of the flux is key and flux from opposite directions cancels out, as demonstrated in figure~\ref{fig:1a}.
\end{counted}
\end{document}


At the moment I am having to choose between correctly wrapped text and a word count. I would much rather not have to choose between the two!

-

The problem disappears as soon as you don't leave blank lines between the wrapfigure environment and the text and add a \par command just before end{counted}:

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{xesearch}
\usepackage{enumitem}

\newcounter{words}
\newenvironment{counted}{%
\setcounter{words}{0}
\SearchList!{wordcount}{\stepcounter{words}}
{a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}
\UndoBoundary{'}
\SearchOrder{p;}}{%
\StopSearching}

\begin{document}
\begin{wrapfigure}{r}{0.5\textwidth}
\centering
\includegraphics{fig1.png}
\caption{Caption}
\label{fig:1a}
\end{wrapfigure}
\begin{counted}
We assume that point $\mathrm{P}$ is far enough from the dipole such that the $\mathbf{B}$ field obeys the equation for a dipole aligned along the $z$-axis:
As can be seen by inspection, the magnetic field lines encircle the $z$-axis and therefore a circular wire loop centred at any point along the axis would have a net zero flux passing through the centre -- no signal would be detected.

The wire loop must be aligned in a plane normal to the magnetic flux. If the loop were aligned parallel to the flux, then no flux would pass \emph{through} the loop. In accordance with Faraday's Law, this would mean that no electric field would be found integrating along the path of the loop -- i.e. no current would be induced and therefore no signal detected. It is important to note that the direction of the flux is key and flux from opposite directions cancels out, as demonstrated in figure~\ref{fig:1a}.\par
\end{counted}

\end{document}


By the way, never use \\ followed by a blank line to increase separation between paragraphs (this produces an underfull hbox warnings); search this site for alternatives.

-
Thanks - I had played around with spacing before, so I think it's the \par right at the bottom that's key. I don't understand how this works, but at least it does! I'll have a look for spacing options other than \\. –  Adam James Hawkins Sep 5 '12 at 14:30