4

The concept I am looking for is similar to the CSS 'clear' property. I'm trying to get two paragraphs, each with an image floating left:

\begin{floatingfigure}[l]{5cm}
\fbox{
\centering
\includegraphics[scale=0.5]{myimage}
}
Some text
\end{floatingfigure}


\begin{floatingfigure}[l]{5cm}
\fbox{
\centering
\includegraphics[scale=0.5]{myimage}
}
Some text
\end{floatingfigure}

In this case, the images collide, rather than the next floatingfigure beginning after the other one.

How can I clear a floatfig?

1
  • I don't think there is any direct way to do this. A warning is printed in the log file when such a clash occurs. You can then manually add a \vspace between the offending paragraphs to provide enough space. – Andrew Swann Aug 23 '12 at 8:31
3

Here is a solution based on \pagetotal. In below example, automatic clearing of a wrapped floatingfigure is achieved both at the beginning of a new floatingfigure or a \section.

\documentclass{article}
\usepackage{mwe}

\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}

\setlength{\parindent}{0pt}

% begin preamble.tex   
\usepackage{calc}
\newlength\vimgflt
\newlength\vposflt
\newlength\vtypesetflt
\newlength\vspaceflt

\usepackage{floatflt}
\renewenvironment{description}{%
    \renewcommand{\includegraphics}[1]{\vspace{-1ex}\Oldincludegraphics[width=4cm]{##1}}
    \renewcommand{\item}[1][]{
        \settototalheight\vimgflt{##1}%
        \global\vimgflt=\vimgflt%
        ##1%
        \end{floatingfigure}%
        \setlength\vposflt\pagetotal%
        \global\vposflt=\vposflt%
        }%
    \clearflt
    \hspace{0pt}%
    \begin{floatingfigure}[l]{4cm}}%
    {}

\newcommand{\clearflt}{
    \par
    \setlength\vtypesetflt{\dimexpr\pagetotal-\vposflt}
    \ifdim \vtypesetflt<\vimgflt \setlength\vspaceflt{\dimexpr\vimgflt-\vtypesetflt+\baselineskip} \else \setlength\vspaceflt{0pt} \fi
    \vspace{\vspaceflt}
    }

\let\Oldsection\section
\renewcommand{\section}{\clearflt\Oldsection}
% end preamble.tex

\begin{document}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-a}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-b}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\section{Section title}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-c}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\blindtext
\end{document}

automatic clearing of floatingfigure

6
  • This solution still has an issue with page breaks. I am working on it. – Serge Stroobandt Aug 29 '13 at 14:06
  • The solution is not compatible (yet?) with \usepackage[nobottomtitles*]{titlesec} in the preamble. – Serge Stroobandt Aug 29 '13 at 14:33
  • A follow-up question concerning the page break problem was posted here. – Serge Stroobandt Aug 30 '13 at 9:46
  • A follow-up question about titlesec can be found here. – Serge Stroobandt Aug 30 '13 at 13:00
  • Mmm, but it jumps to a new page when part of the description flows to the next page of a two-column format. – dfshk Aug 8 '14 at 1:27
2

There is no built in way to acheive this. However, a warning is printed in the log file when such a clash occurs. This is sensible behaviour as such problems can occur in a number of different situations, and you are best placed to judge what changes should be made. One possibility in your situation would be to manually add a \vspace between the offending paragraphs to provide enough space. However there is a subtlety in that you need to add some printing command in a paragraph of its own before the new paragraph; in this case \ (backslash space) will do. Here is a working example:

\documentclass{article}

\usepackage{floatflt,graphicx}

\begin{document}
\begin{floatingfigure}[l]{5cm}
\fbox{
\centering
\includegraphics[width=4cm]{example-image-a}
}
\end{floatingfigure}
Some text.

\vspace{3.5cm}
\ 

\begin{floatingfigure}[l]{5cm}
\fbox{
\centering
\includegraphics[width=4cm]{example-image-b}
}
\end{floatingfigure}
Some text.

\end{document}

Sample output

I started by turning your code in to a minimal working example (MWE), by adding the floatflt (the newest replacement for floatfig) and graphicx packages, changing the figures to the example images included in the standrd distributions and making their widths smaller than the space you have requested for them so that they do not spill over. Now doing just that will produce no output, because there is no text outside the floating figures. Indeed the floatingfigure environment should only contain the figure and perhaps with a \caption; the paragraph text should be outside the environment. So the following minimal document displays your problem:

\documentclass{article}

\usepackage{floatflt,graphicx}
\usepackage{lipsum}

\begin{document}
\begin{floatingfigure}[l]{5cm}
\fbox{
\centering
\includegraphics[width=4cm]{example-image-a}
}
\end{floatingfigure}
Some text

\begin{floatingfigure}[l]{5cm}
\fbox{
\centering
\includegraphics[width=4cm]{example-image-b}
}
\end{floatingfigure}
Some text
\end{document} 

Sample wrong output

and its log file contains

Package floatflt Warning: Floating figures 1 and 2 colliding.

Generally the floatflt package is not happy if consecutive paragraphs contain floats. There are comments on this in the package's documentation.

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.