0

I'm trying to put a small figure in a floating box; the picture itself doesn't need to float, it should only float with the box. I've been doing some adjusting of minipages, but that requires some trial and error. I've been thinking that the wrapfig package would be able to do this, but it seems to interfere strangly with the floatrow package that I use to make the boxes.

MWE:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{floatrow}
\DeclareNewFloatType{Box}{
  name=Box,
  placement=htbp,
  fileext=lob}
\floatsetup[Box]{
  style=BOXED,
  capposition=top,
  justification=justified}


\newenvironment{BOX}[2][htbp]{%
  \begin{Box}[#1]
    \ifx\relax#2\relax\else\caption{\textbf{#2}}\fi
}{\end{Box}}

\newenvironment{BOX*}[2][htbp]{%
  \begin{Box*}[#1]
    \ifx\relax#2\relax\else\caption{\textbf{#2}}\fi
}{\end{Box*}}


\begin{document}

\title{Title}

\maketitle

\begin{BOX}{First box}

\begin{minipage}[t][1\totalheight][c]{0.3\columnwidth}%
\vspace{0pt}

\includegraphics[width=1\textwidth,height=0.2\textheight]{foo}%
\end{minipage}\hspace{0.03\textwidth}%
\begin{minipage}[t]{0.67\columnwidth}%
\vspace{0pt}
\lipsum[1]
\end{minipage}
\lipsum[3]
\end{BOX}

\begin{BOX}{Second box}

\begin{wrapfigure}{l}{0.4\textwidth}
\includegraphics[width=1\textwidth,height=0.2\textheight]{foo}%
\end{wrapfigure}
\lipsum[2]
\end{BOX}

\end{document}

enter image description here enter image description here

1

Here is a poor man's wrapfig using \hangindent. The image is drawn in vmode before the indentation starts. Adding \leavevmode will cause it to be indented as well.

Note: \hangindent is quite versatile. As you can see, a negative value for \hangafter changes it from hanging indentation to regular indentation. A negative value for \hangindent cause the right margin to be hung/indented.

\documentclass{article}
\usepackage{mwe}

\begin{document}

\begin{figure}
\fbox{\begin{minipage}{\dimexpr \textwidth-2\fboxsep-2\fboxrule}
\abovecaptionskip=0pt
\caption{Floating Box}%
\sbox0{\includegraphics[scale=0.25]{example-image}}% get height and width
\hangafter \numexpr -\ht0/\baselineskip -2\relax% adds two blank lines at bottom
\hangindent \dimexpr \wd0 + 1em\relax% adds 1em margin on right
\vbox{\raisebox{-\height}[0pt][0pt]{\box0}}\vspace{-1ex}% overlap image
\lipsum[1]
\end{minipage}}
\end{figure}

\end{document}

floating box

2
  • Great answer! I have to add, though, that this doesn't work if the text that needs to be wrapped contains multiple paragraphs, but I can live with that. Is there a way to increase the space at the bottom ? When the text that needs to be wrapped is \small, the vbox seems too small and the text goes back to the left margin too early.
    – Jos
    Dec 4 '15 at 9:36
  • You need to switch to \small before all the calculations are done, like right after begin{figure} or \begin{minipage}. If/when you make this a macro/environment, you could make the number of lines indented an optional parameter. Dec 4 '15 at 16:29

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.