[Tex/LaTex] wrap text around a figure inside a floating box

floatrowwrapfigure

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

Best Answer

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

Related Question