# Do I really have to specify the width of my wrapfigure twice?

I'm trying to add a figure to my document. Because I want text to wrap around it, I use the wrapfigure environment:

\begin{wrapfigure}{r}{1cm}
\begin{center}
\includegraphics[width=1cm]{"My figure"}
\end{center}
\caption{A caption of my figure}
\end{wrapfigure}


As you can see, I want my figure to be shrunk down (the file is quite large) so I set the width to 1 cm. But now I must also tell the wrapfigure to be 1 cm wide. Why can't wrapfigure just automatically be as wide as the figure it contains?

-
With this you can put white space around the figure. –  Sigur May 17 at 0:57
@Sigur Yes, I realize that. However, I have many large documents with many figures, where I don't want any white space. Isn't there a way of saving myself the trouble of having to enter the width twice? It can be quite annoying when I'm trying out a few different sizes for a figure. –  Superbest May 17 at 1:01
You could define a custom environment using \newenvironment tweaked as you wish? –  cfr May 17 at 1:05

wrapfig actually doesn't care what it contains. As such, there is no connection between the image and the wrapfigure environment width. You can, however, set the width to \linewidth so that it fills the container it's in:

\documentclass{article}
\usepackage{wrapfig,graphicx}
\begin{document}

\begin{wrapfigure}{r}{3cm}
\includegraphics[width=\linewidth]{example-image}
\caption{A caption of my figure}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas luctus id tellus semper mattis. Integer quis neque
sem. Etiam semper, tortor in lacinia feugiat, nisi mi cursus
metus, et molestie felis nisl quis sapien. Fusce vel ligula
eget nunc gravida malesuada ac sit amet risus. Duis et urna
vestibulum, iaculis metus nec, placerat sem. Fusce suscipit
quis velit mattis fermentum. Curabitur odio urna, tempor ac
orci a, euismod elementum est. Mauris id mollis est, a consequat
purus. Mauris quis risus non ipsum accumsan elementum ut in
quam. Proin suscipit non augue dapibus tempus. Mauris
scelerisque, turpis et adipiscing interdum, neque nunc
tristique ipsum, a sollicitudin libero eros eget tellus. Donec
fringilla cursus diam, eget malesuada erat dapibus in. Aliquam
pellentesque vitae enim ut rutrum. In auctor nunc ac laoreet
molestie. Proin scelerisque convallis tortor, sit amet egestas
nulla accumsan ac. Donec urna justo, fringilla et vulputate eu,

\end{document}


Try also setting the default in the preamble:

\setkeys{Gin}{width = \linewidth}

-
Why not \usepackage{etoolbox} \AtBeginEnvironment{wrapfigure}{\setkeys{Gin}{width = \linewidth}} so inside wrapfigure there's no need to manually specify \linewidth as the width? –  Gonzalo Medina May 17 at 2:00

Or this, for example, defines a custom command which wraps your graphic in the necessary environments. It requires 3 arguments with a fourth optional.

\documentclass{article}
\usepackage{wrapfig,graphicx,kantlipsum}
\newcommand{\mywrapfigure}[4][r]{%
\begin{wrapfigure}{#1}{#2}
\centering
\includegraphics[width=#2]{#3}
\caption{#4}
\end{wrapfigure}}

\begin{document}
\mywrapfigure{25mm}{example-image-a}{A caption of my figure}
\kant[1-2]

\mywrapfigure[l]{30mm}{example-image-b}{A caption of my other figure}
\kant[3]
\end{document}


-
You are loosing two arguments for wrapfigure. –  Gonzalo Medina May 17 at 1:34
@GonzaloMedina It was only supposed to be an example of how to set up a custom command based on the example in the question. Usually when I write custom macros, I just define them to accommodate the arguments I need - I don't typically accommodate everything the commands/environments I use in the definition support. Perhaps this is bad policy but I prefer to think of it as a virtue of customisation! (It keeps things simpler and focuses on the particular features I need without requiring me to worry about the rest.) I never use wrapfig, though, so maybe I've missed an essential? –  cfr May 17 at 1:56
For that matter, I'm neglecting almost every feature \includegraphics offers... –  cfr May 17 at 1:57
+1 for the code, but I'll accept the other question since it is the most direct answer to my question as written. –  Superbest May 17 at 4:34