2

In my report, I am using a lot of figures that wrap around text using the package wrapfig.

\begin{wrapfigure}{L}{0.30\textwidth} 
    \centering
    \includegraphics{Image.pdf}
\end{wrapfigure}

For all figures that I want to produce, the intended picture already has the perfect size (I might need a scaling factor for all of them but that can be included with [scale=0.X] in the \includegraphics line). For context, the images depict chemical structures that I drew using ChemDraw and saved as PDF. I want to use their size as is, because if I for example use "width=0.8\linewidth" the structures will not be "uniform" anymore (a nitrogen in one structure may end up looking 20% bigger than in another structure which I do not want).

Therefore, the image-width keeps changing depending on the chemical structure (which is good). However, the wrapfigure-width is very stiff and needs to be adjusted manually to accommodate each image the best (which is bad). Since I have a lot of figures, I am wondering if there is a way to make this more dynamic? What do I need to replace the line

\begin{wrapfigure}{L}{0.30\textwidth} 

with to make the wrapfigure automatically adjust to the width of each image? Any help is much appreciated!

2
  • 1
    Put the image into a savebox first, measure its width and use that as the width for the wrapfigure environment. Then simply use the contents of the savebox inside the wrapfigure env.
    – Raven
    Commented Nov 24, 2023 at 15:20
  • @Raven Wow, thanks for the quick answer! Could you maybe demonstrate this using code? I've never used savebox and I'm not sure if I'll be doing it correctly.
    – Chiron
    Commented Nov 24, 2023 at 15:23

1 Answer 1

5

You can make use of a savebox and put the desired content (in your case the image) into it. Subsequently, you can measure the box's width and use that in the width argument for the wrapfigure environment. Finally, inside the wrapfigure environment, you can then display the actual contents of your box - aka: your image.

Here I have wrapped all of that into a new command \wrapimage that takes the same kind of arguments as \includegraphics, but displays the image as a wrapfigure:

\documentclass{scrartcl}

\usepackage{wrapfig}
\usepackage{duckuments}
\usepackage{xparse}
\usepackage{graphicx}

% Before usage, the savebox has to be declared/created
\newsavebox{\imageHolder}

% Use as \wrapimage{image} or e.g. \wrapimage[scale=2]{image}
\NewDocumentCommand{\wrapimage}{ O{} m }{
    % Typeset the image into the savebox
    \sbox{\imageHolder}{\includegraphics[#1]{#2}}

    % Pass the box's width to wrapfigure
    \begin{wrapfigure}{L}{\wd\imageHolder}
        % Dump the contents of the box into the document as
        % if the box's content had been added here all along
        \usebox{\imageHolder}
    \end{wrapfigure}
}

\begin{document}
    \wrapimage{example-image-duck}

    \blindduck[full=true]
\end{document}

Image of resulting document

2
  • Thank you kind stranger! This works like a charm, you've made my life a lot easier. The only thing I wanted to remark is that using "\wrapimage[scale=2]{image}" sadly does not work properly for me. But that's completely fine, since that wasn't necessarily part of the question. (I just tried replacing the "#2" in \includegraphics with "scale=2" instead and it worked!) Anyways, thanks again, there's no chance I would've come up with this myself so quickly :)
    – Chiron
    Commented Nov 28, 2023 at 14:07
  • @Chiron you're welcome :) See my latest edit to my answer to get the optional arguments working as expected. I had the ordering of the arguments wrong. The original argument order would have allowed for \wrapfigure{...}[scale=2] instead :)
    – Raven
    Commented Nov 28, 2023 at 15:11

You must log in to answer this question.

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