4

When defining a wrapfigure (or a minipage), I need to specify the width of the figure. Is there a possibility that this width is determined by the picture inside?

For sake of an MWE, I'll use a tikzpicture. I don't want to chage the scale of the image since my image is used multiple times in the document, but in different variations and it should have the same scale all the time.

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage[justification=centering]{caption}
\begin{document}
\begin{wrapfigure}{r}{5cm}
    \centering
    \begin{tikzpicture}
        \draw (0,0)circle (2);
    \end{tikzpicture}
    \captionof{figure}{A circle}
\end{wrapfigure}
\lipsum[1]
\end{document}

This looks bad: first attempt

Adding \centering improves a bit, but then I still have too much spacing left and right.

I want to have an output like this:

good output

(This was created using 4cm as width, but I don't want to guess the size of the image - easy for the circle, harder if you have a more complicated image)

To sum it up, is there an option to fit the width of a wrapfigure (or any environment, like a minipage) to an image without rescaling the image and without guessing the size?

Edit: Not a duplicate of Do I really have to specify the width of my wrapfigure twice?, since in this case the author knows the width of the image and therefore can specify it, I don't.

2
  • Have you had a look at the possible duplicate? Sep 18, 2017 at 18:50
  • I had, and edited my question.
    – jaytar
    Sep 18, 2017 at 18:51

2 Answers 2

5

enter image description here

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage[justification=centering]{caption}
\begin{document}
\sbox{0}{%
\begin{tikzpicture}
        \draw (0,0)circle (2);
    \end{tikzpicture}%
}
\begin{wrapfigure}{r}{\wd0}
    \centering
     \usebox{0}%
    \captionof{figure}{A circle}
\end{wrapfigure}
\lipsum[1]
\end{document}
4
  • I really like this since it works for more than wrapfigures.
    – jaytar
    Sep 18, 2017 at 18:57
  • 1
    +1 -- although the wrapfig manual suggestion is very convenient, it does have a recognized downside, which this doesn't. (at least not the same one.) and the caption package nicely wraps and centers the caption if it's wider than the drawing; looks a bit funny if a line is very short, but it's automatic. Sep 18, 2017 at 19:46
  • Is the box essential? I somehow get the feeling the box is really needed in this example, but is it? Sep 18, 2017 at 19:53
  • 2
    @OlegLobachev you need a box to put the content in so you can measure it Sep 18, 2017 at 20:17
7

this is addressed in section 2 of the wrapfig documentation, where it says

if you specify a width of zero (0pt), the actual width of the figure will determine the wrapping width. A following \caption should have the same width as the figure, but it might fail badly; it is safer to specify a width when you use a caption.

advice: rtfm.

1
  • Great, but I do use captions. I might give it a shot though.
    – jaytar
    Sep 18, 2017 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .