# Wrap text around graphic without changing its size

I created a graphic with specific dimensions and text in it so I don't want to scale it.

I want the text to float around it so I use \wrapfigure which needs a specified width, which I want to get from the inserted image (\includegraphics).

Box the image and then use the width of the box in wrapfigure:

\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}

\newsavebox\mybox
\savebox\mybox{\includegraphics[height=5cm]{example-image-a}}

\begin{document}

\begin{wrapfigure}{r}{\wd\mybox}
\usebox\mybox
\caption{a test figure}
\end{wrapfigure}
\lipsum[1-2]

\end{document}


The result:

If you are going to use this many times, perhaps you could define an environment to simplify the code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{xparse}
\usepackage{lipsum}

\newsavebox\mybox

%Syntax:
%\begin{Wrapfigure}[<lines>]{<position>}[<overhang>]{<image file>}[<options for image>]
%\caption{description}
%\label{...}
%\end{Wrapfigure}

\NewDocumentEnvironment{Wrapfigure}{O{}mO{0pt}mO{}}
{%
\savebox\mybox{\includegraphics[#5]{#4}}
\wrapfigure[#1]{#2}[#3]{\wd\mybox}
\usebox\mybox
}
{\endwrapfigure}

\begin{document}

\begin{Wrapfigure}{r}{example-image-a}[height=4cm]
\caption{a test figure}
\end{Wrapfigure}
\lipsum[1]

\begin{Wrapfigure}[11]{l}[30pt]{example-image-a}[height=3cm]
\caption{a test figure}
\end{Wrapfigure}
\lipsum[1]

\end{document}


• Thank your for your solution. Can you merge these two steps the definition in the preamble and the insertion of the graphic in one command?
– jhg
Aug 21 '15 at 14:31
• @jhg You're welcome. Yes, one could define that command, but it would end up having three mandatory and three optional arguments, so I don't think it's practical to define it. Aug 21 '15 at 14:36
• @jhg Perhaps more arguments, since one would also need two more for the caption and one more for the label. No; it's not practical, in my opinion, to define such a command. Perhaps an environment. Aug 21 '15 at 14:41
• @jhg Please see my updated answer with the definition of an environment. Aug 21 '15 at 23:42
• Thank you very much for your effort it works like a charm! I never have used the overhang option but I do use a \vspace{-_pt} before inserting the picture to compensate the displacement of images when wrapfigure is directly after a \section. So I'll use #3 for that.
– jhg
Aug 23 '15 at 11:34