# Clearing floats in LaTeX

I'm making a document using the memoir theme. In my document, I wish to have images floated to the left or the right at times.

I've got this working reasonably well:

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{
\begin{wrapfigure}{#3}{0.25\textwidth}%
\vspace{-20pt}%
\begin{center}%
\includegraphics[width=0.25\textwidth]{#1}%
\end{center}%
\vspace{-15pt}%
\legend{#4}%
\vspace{-15pt}%
\end{wrapfigure}%
}

% Remove 'Figure 1' text
\patchcmd{\legend}%Cmd
{\normalsize}%Search
{\small\bfseries\raggedright}%Replace
{}%Success
{}%Fail

\newcommand{\beginsection}[1]{%
\let\@section@title@\relax
\normalfont\large\bfseries #1
\normalfont\normalsize
\par
\vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

\gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{800placeholder}{800}{R}{Figure A}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\floatingimage{150placeholder}{150}{L}{Figure B}
\lipsum[3-3]

\beginsection{Section 3}
\lipsum[4-4]

\end{document}


The only thing is, I don't like the way the 'Section 3' heading is pushed inwards. I'd like instead for headings to appear after the floated element, at the start of a line. Ideally I would like for this behaviour to only apply when the float is on the left, as floats on the right don't push the titles.

In CSS I could accomplish this using h3 { clear: left; }. What's the LaTeX equivalent?

I did some searching and found \FloatBarrier in the placeins package, but this didn't change the output at all. Here's how I tried to use it.

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{placeins}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{
\begin{wrapfigure}{#3}{0.25\textwidth}%
\vspace{-20pt}%
\begin{center}%
\includegraphics[width=0.25\textwidth]{#1}%
\end{center}%
\vspace{-15pt}%
\legend{#4}%
\vspace{-15pt}%
\end{wrapfigure}%
}

% Remove 'Figure 1' text
\patchcmd{\legend}%Cmd
{\normalsize}%Search
{\small\bfseries\raggedright}%Replace
{}%Success
{}%Fail

\newcommand{\beginsection}[1]{%
\let\@section@title@\relax
\FloatBarrier
\normalfont\large\bfseries #1
\normalfont\normalsize
\par
\vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

\gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{800placeholder}{800}{R}{Figure A}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\floatingimage{150placeholder}{150}{L}{Figure B}
\lipsum[3-3]

\beginsection{Section 3}
\lipsum[4-4]
\end{document}


(Not going to attach a screenshot as it looks identical to the one above)

How can I achieve the effect I'm after?

EDIT: I found this related question but I can't get their solution working for me.

• What should happen if the figure is taller than a section? – Hugh Oct 9 '16 at 6:15
• If a figure extends below the end of a section, the next section should not start until after the end of the figure. Super dodgy Paint mockup: i.imgur.com/QZ34345.png (Ignore the stretching) – Joshua Walsh Oct 9 '16 at 6:24
• you have added % to lots of lines where not needed but \newcommand\floatingimage[4]{ needs one – David Carlisle Oct 9 '16 at 11:24
• don't use negative space and center just use \centering, which adds no vertical space – David Carlisle Oct 9 '16 at 11:43

With some care you can clear wrapfig's settings so the later text is not indented, although you need to add the extra space to compensate. (If you were doing this often you could automate the measurement of the image and the size of the paragraph, but not really worth it for a hand written docuemnt, only for an automated workflow)

Note wrapfig, even with L rather than l isn't really a latex float so the question title is a bit misleading. I used \section as the custom definition seemed to miss all the important code for a section heading (controlling page breaks after the heading for example)

Also added some missing % and used \centering (which adds no vertical space) rather than center with negative space adjustments.

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage[nopar]{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{placeins}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{%
\begin{wrapfigure}{#3}{0.25\textwidth}%
\centering
\includegraphics[width=0.25\textwidth]{#1}%

\legend{#4}%
\end{wrapfigure}%
}

% Remove 'Figure 1' text

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{example-image-a}{800}{R}{example-image-a}
\section{Section 1}
\lipsum[1-1]

\section{Section 2}
\lipsum[2-2]

\floatingimage{example-image-b}{150}{l}{example-image-b}
\lipsum[3-3]

\makeatletter
\WF@@everypar{}
\vspace{\baselineskip}
\makeatother

\section{Section 3}
\lipsum[4-4]
\end{document}

• Ah, I should've mentioned that my document will be produced automatically. Thanks though! – Joshua Walsh Oct 9 '16 at 20:32
• @YM_Industries just change the the vspace to vspace{\numexpr6-\c@WF@wrappedlines\relax\baselineskip} which makes it adjust to the number of lines wrapfig would have wrapped. – David Carlisle Oct 9 '16 at 21:02
• I've adapted this to my needs and posted my own answer, I'd love any feedback you could give me about my approach. Thanks for your help, I was beginning to think that this wasn't possible in LaTeX. – Joshua Walsh Oct 10 '16 at 9:25

This is more of a comment than an answer, as it is only a marginal improvement.

Essentially, I treat left images as bullets in an itemize list. This ensures the images is cleared before the next paragraph. As a result, paragraphs shorter than the image will be followed by a large parskip. Packages adjustbox and enumitem are to get the image aligned properly and the paragraph to the right in-line with the document margins.

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{
\begin{wrapfigure}{#3}{0.25\textwidth}%
\vspace{-20pt}%
\begin{center}%
\includegraphics[width=0.25\textwidth]{#1}%
\end{center}%
\vspace{-15pt}%
\legend{#4}%
\vspace{-15pt}%
\end{wrapfigure}%
}

% Remove 'Figure 1' text
\patchcmd{\legend}%Cmd
{\normalsize}%Search
{\small\bfseries\raggedright}%Replace
{}%Success
{}%Fail

\newcommand{\beginsection}[1]{%
\let\@section@title@\relax
\normalfont\large\bfseries #1
\normalfont\normalsize
\par
\vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

\gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip
\usepackage{enumitem}

\newcommand{\leftimage}[3]{
\begin{itemize}[labelwidth=0.25\textwidth, labelindent=0pt, leftmargin=!, itemindent=0.05\textwidth, labelsep*=0.025\textwidth]
\end{itemize}
}

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{Example-image}{800}{R}{Figure A}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\leftimage{Example-image}{\lipsum[4-4]}

\lipsum[4-4]

\beginsection{Section 3}
\lipsum[4-4]

\end{document}

• I appreciate the effort, and this certainly does get closer to what I want to achieve, but it's won't quite work for my purposes. Thanks though! – Joshua Walsh Oct 9 '16 at 9:12

Here's the approach I ended up using, based on David Carlisle's answer. The changes I have made are to make it automatically applied wherever I've used \beginsection and \floatingimage. (But only if the image is on the left side)

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage[nopar]{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{placeins}
\graphicspath{ {images/} }

\makeatletter

\let\shouldwrap\relax

\providetoggle{floattoclear}

\newcommand\floatingimage[4]{%
\ifnum l=#3
\settoggle{floattoclear}{true}%
\else
\settoggle{floattoclear}{false}%
\fi
\begin{wrapfigure}{#3}{0.25\textwidth}%
\centering
\includegraphics[width=0.25\textwidth]{#1}%
\vspace{-5pt}%
\legend{#4}%
\end{wrapfigure}%
}

\newcommand\beginsection[1]{%
\let\@section@title@\relax
\iftoggle{floattoclear}{
\WF@@everypar{}%
\vspace{\numexpr6-\c@WF@wrappedlines\relax\baselineskip}%
\settoggle{floattoclear}{false}
}
\normalfont\normalsize
\par
\vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

\gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{example-image-a}{800}{r}{example-image-a}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\floatingimage{example-image-b}{150}{l}{example-image-b}
\lipsum[3-3]

\beginsection{Section 3}
\lipsum[4-4]
\end{document}

• you asked for comments so... (1) you are still missing several % at ends of lines (or equivalently you have a space before the % which is the same problem) (2) the magic number 6 can't be right (I know I suggested it:-) works for a quick answer but there must be a more sensible relationship between the wrapfig counter and the amount of space to be left. (3) as I commented in my answer I didn't check everything \beginsection is doing, but it doesn't seem to be using any of the latex section heading code, so will not prevent a page break immediately after the heading (for example) – David Carlisle Oct 10 '16 at 11:07
• (4) \ifx l#3\relax probably less obscure than \ifnuml#3 – David Carlisle Oct 10 '16 at 11:10