6

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}
5
  • Welcome to TeX - LaTeX!
    – Cfun
    Jun 30 '16 at 22:38
  • You can't use wraptable in connection with list environments (such as center).
    – egreg
    Jun 30 '16 at 23:32
  • 1
    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
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.