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!