# wrapfig and wraptable not obeying \FloatBarrier

I've been using this forum for a year, and it's very helpful. I have a question about floating figures and tables. It seems to have already been addressed here: Floats - how to restrict floating to subsection only in one section of the document?

However, I find a lot of cases where \FloatBarrier does not work with wrapfig and wraptable. Is there something I'm obviously missing? Below is my minimum working example. The wraptable will not go into its designated section, even with an explicit \FloatBarrier command. Any thoughts? Thank you!

\documentclass[12pt]{report}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{float}
\usepackage[section]{placeins}

\begin{document}

\section{Characterization of Porosity}

\Blindtext

\begin{wraptable}{L}{0.4\textwidth}
\caption{Comparison of porosity parameters.}
\begin{center}
\begin{tabular}{lccc}
Fabric &                       $K_{n}$&$K_{c}$   \\ \hline
A                              & 0.53 & 0.772      \\
B                              & 0.56 & 0.533       \\
B, speckled                    & 0.56 & 0.485       \\
\end{tabular}
\end{center}
\label{t:porosity_label}
\end{wraptable}

\FloatBarrier
\section{Strain Effect on Porosity}

\Blindtext

\end{document}

• Welcome to TeX - LaTeX!
– Cfun
Jun 30 '16 at 22:38
• You can't use wraptable in connection with list environments (such as center). Jun 30 '16 at 23:32
• These aren't floats so \FloatBarrier is irrelevant. I'm not sure about @egreg 's point. You can't use them when the surrounding stuff is a list. I don't know if that prohibits using a list within the wrapfig or wraptable. However, \centering is probably a better option anyway.
– cfr
Jun 30 '16 at 23:58
• Thank you cfr. OK, I'm really dumb. So, if I like having text wrapped around figures and tables but need to control which sections they stay in, is there anything you would recommend?
– Ken
Jul 1 '16 at 1:45
• what do you want it to be wrapped with? you have wraptable but then the section ends and you supply no text to wrap around the table. it seems that you just want to use a standard table here, Sep 23 '17 at 20:52

## 1) A slightly different MWE

AFAIU, it demonstrates the same sort of behavior you're talking about:

\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}

\begin{document}
\section{First section}
% \lipsum[1]  % uncommenting this make image jump to after section 2
\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[1]

\section{Second section}
\lipsum[2]
\end{document}


The result looks fine if those line is commented out:

But the image jumps to the very end if that line is uncommented: Sometimes, this "jump" is triggered by adding just one character. And, personally, jumping that far is not the behavior I expect.

## 2) \WFclear

Donald Arseneau (wrapfig dev) suggested to use built-in undocumented \WFclear command for that.

\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}

\begin{document}
\section{First section}

\lipsum[1]

\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]

% \lipsum[3]  % if uncommented, the image would be next-to-this paragraph

\WFclear

\section{Second section}
\lipsum[2]
\end{document}


And at the first glance, this is all you need. It just puts the image separately from the text, if there's no suitable paragraph:

And uses the first suitable, if any:

However, there's still a caveat, associated with too short paragraphs:

\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}

\begin{document}
\section{First section}

\lipsum[1]

\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]

% \lipsum[3]  % if uncommented, the image would be next-to-this paragraph
This is a very short paragraph.

\WFclear

\section{Second section}
\lipsum[2]
\end{document}


## 3) \WFfill

The same Donald's post proposes a fix for this:

\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}

%%% Adds empty text lines next to the wrapfigure,
%   if the paragraph is shorter than needed.
\makeatletter
\def\WFfill{\par
\ifx\parshape\WF@fudgeparshape
\nobreak
\ifnum\c@WF@wrappedlines>\@ne
\vskip\c@WF@wrappedlines\baselineskip
\global\c@WF@wrappedlines\z@
\fi
\allowbreak
\WF@finale
\fi
}
\makeatother

\begin{document}
\section{First section}

\lipsum[1]

\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]

% \lipsum[3]  % if uncommented, the image would be next-to-this paragraph (works OK)
This is a very short paragraph.

% \lipsum[3]  % if uncommented, this paragraph would wrap the figure as well (works OK)

\WFfill
\WFclear

\section{Second section}
\lipsum[4]
\end{document}


Now short paragraph case is not looking that bad:

And if the first-suitable, but too-short paragraph is followed by another one, it also perfectly works:

## 4) Put it inside \section

Just like FloatBarrier, these commands could be embedded in \section:

\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}

%% Adds empty text lines next to the wrapfigure, if the paragraph is shorter than needed.
\makeatletter
\def\WFfill{\par
\ifx\parshape\WF@fudgeparshape
\nobreak
\ifnum\c@WF@wrappedlines>\@ne
\vskip\c@WF@wrappedlines\baselineskip
\global\c@WF@wrappedlines\z@
\fi
\allowbreak
\WF@finale
\fi
}
\makeatother

%% Modify \section definition to include all the barriers inside.
\usepackage{placeins}
\let\Oldsection\section
\renewcommand{\section}{\WFfill\WFclear\FloatBarrier\Oldsection}

\begin{document}
\section{First section}

\lipsum[1]

\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]

% \lipsum[3]  % if uncommented, the image would be next-to-this paragraph (works OK)
This is a very short paragraph.

% \lipsum[3]  % if uncommented, this paragraph would wrap the figure as well (works OK)

\section{Second section}
\lipsum[4]
\end{document}