# Wrap a wrapfigure and text by a border?

Is it possible to wrap a wrappicture with some text nearby by a frame?

-

Not knowing how big the figure and associated text are, it's not possible to give very specific recommendations. You're probably best off (i) keeping the text and (wrap)figure inside a minipage environment and (ii) enclosing this minipage in either a framed or an mdframed environment. Be sure to "flush" all other pending figures (i.e., those not contained in these nested environments) prior to initiating the framed material, as otherwise the numbering of ordinary figures and wrap-figures will be off.

Here's a MWE that uses the wrapfigure, minipage, and mdframed environments, along with the lipsum package for filler text:

\documentclass{article}
\usepackage{wrapfig,lipsum}
\usepackage[linewidth=1.2pt,linecolor=red]{mdframed}
\begin{document}
\noindent
Some text \emph{before} the minipage environment\dots

\bigskip
\begin{mdframed}
\begin{minipage}{\textwidth}
\begin{wrapfigure}{r}{0.6\textwidth}
\centering
\rule{2in}{1in}
\caption{A black rectangle}\label{fig:blackrect}
\end{wrapfigure}
\lipsum[2]
\end{minipage}
\end{mdframed}

\bigskip\noindent
Some more text \emph{after} the minipage environment\dots
\end{document}


First Addendum, very kindly provided by @MarcoDaniel: You can use also the package cutwin. This way, you don't need to set up minipage or wrapfigure environments, and the positioning is more flexible too:

\documentclass{article}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage[linewidth=1.5pt,linecolor=blue]{mdframed}
\usepackage{cutwin}
\opencutright
\begin{document}
\noindent
Some text \emph{before} the mdframed environment\dots

\bigskip
\begin{mdframed}
\def\windowpagestuff{%
\centering\includegraphics[width=2cm]{Bildname}
\captionof{figure}{foo}
}
\begin{cutout}{2}{.55\textwidth}{0pt}{12}
\lipsum[1]
\end{cutout}
\end{mdframed}

\bigskip\noindent
Some more text \emph{after} the mdframed environment\dots
\end{document}


Second addendum: @Herbert raises the point that cutwin may not be compatible with the microtype package, thereby making the method proposed in the addendum above not as widely applicable as one might/should hope. The "official" version of microtype on the CTAN is still 2.4 (early 2010). However, beta-08 of version 2.5 (Sept 2011) is available at this webpage; it contains many enhancements and improvements over version 2.4. Version 2.5 of microtype is what I have on my system (TeXLive2011), and happily it appears to interact just fine with cutwin. :-)

-
Unfortunately minipage does not wrap the figure inside it. The figure is placed partly outside the minipage, if the minipage is too small, and so the frame around the minipage does. I would like the frame to wrap the image too. –  Skip Jan 5 '12 at 16:33
@Skip: It would be helpful if you posted a MWE (minimum working example) that provides some information regarding (i) the size of the figure to be included and (ii) the amount of text to be included, along with information as to the overall width of the text block. Separately: have you ensured, per the instructions in the user guide of the wrapfigure package, that it's not placed in some list-type environment? –  Mico Jan 5 '12 at 16:39
@Mico: In order to include the seagull.png in place of your black rectangle you need the \grapicx package, No? With \graphicx your code worked beautifully. –  Sony Jan 5 '12 at 17:45
@Sony: Thanks for this. Indeed, if the code loaded an image stored in an external file, one would have to use a command such as \includegraphics (and, obviously, omit the \rule command). As I didn't have the gull image file at hand, I chose to make do with a boring black rectangle for the MWE... –  Mico Jan 5 '12 at 17:55
@Mico: Done. Only to show the package cutout. –  Marco Daniel Jan 5 '12 at 18:06

run with xelatex

\documentclass{article}
\usepackage{pst-node,lipsum,graphicx,wrapfig}
\begin{document}

\section[Programming block type]{\rnode[lt]{Left}{Programming block type}}
\begin{wrapfigure}[13]{r}%[34pt]% optional argument for an offset into the margin
{5cm}
\includegraphics[width=5cm]{/tmp/seagull}
\caption[Close-up of a gull]{\rnode[rb]{Right}{Close-up of a gull}}
\end{wrapfigure}
\lipsum[1]

\psframe[linecolor=red,framearc=0.1]%
([nodesep=30pt,angle=160]Left)([nodesep=15pt,angle=-10]Right)

\end{document}


if you like it as a coloured rectangle then use

\psframe*[opacity=0.1,linecolor=red,framearc=0.1]%
([nodesep=30pt,angle=160]Left)([nodesep=15pt,angle=-10]Right)


-
This is great with the bonus feature of shading. –  Sony Jan 5 '12 at 17:46