[Tex/LaTex] wrapfig and wraptable not obeying \FloatBarrier

placeins

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}

Best Answer

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}

Same in overleaf.

The result looks fine if those line is commented out: 1_works_OK_imcat

But the image jumps to the very end if that line is uncommented: 2_notOK_imcat 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}

Same in overleaf.

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: 3_WFclear_one_par_imcat

And uses the first suitable, if any: 4_WFclear_two_par_imcat

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}

Same in overleaf.

enter image description here

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 
    \advance\c@WF@wrappedlines\m@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}

Same in overleaf.

Now short paragraph case is not looking that bad: short_paragraph_issue_fixed

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

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 
    \advance\c@WF@wrappedlines\m@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}

Same in overleaf.

Related Question