Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
1  
You could define a custom environment using \newenvironment tweaked as you wish? –  cfr May 17 at 1:05

2 Answers 2

up vote 4 down vote accepted

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:

enter image description here

\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, 
adipiscing sed augue.

\end{document}

Try also setting the default in the preamble:

\setkeys{Gin}{width = \linewidth}
share|improve this answer
    
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}

\mywrapfigure

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.