# Miktex/XeLaTeX - non-working wrapped images (wrapfigure)

I've searched hundereds forums of the same question, but without any working solution.

I'd like to insert images to my document to the right side of the page and my text shall wrap them from the left.

After many hours I've built below piece of code, but it still doesn't work properly although most issues are solved now:

\begin{wrapfigure}{r}{.3/textwidth}
\centering
\vspace{-11pt} % Kill unwanted space above
\includegraphics[height=4cm]{name.jpg}
\caption{Some description}
\end{wrapfigure}
\paragraph{}\vspace*{-\parskip}
Some text which should wrap the image.


Unwanted behaviour

• width of the image area is based on page width, but it's unacceptable

• I need to set image area width based on image width, not page width
• Reason: images are imported to .tex file using external script and image width is variable - I don't know what is their width. I need only to specify their height and width must be calculated. It works for image itself, but not for the wrapfigure area.
• wrapfigure doesn't work at all if an image (or its caption) overflows the page break

• if I use {r} settings for wrapfigure, image is displayed under the text out of page and dummy space is generated at the top of next page
• if I use {R} settings which is recommended in lots of forums, output after first overflow is absolutely nonsense - although there
is no overflow below text area as described for {r}, starting from
the first occurence of intersection image and page break all further images are displayed at nonsense positions at below pages - not only vertically, but also horizontally - in some cases the image is
centered, in other cases it's aligned to left side
• required behaviour is page break before both image and begin of wrapping text, I don't want to split image and above mentioned "Some text which should wrap the image." in any way (for example starting
text on previous page and then display image on next page is
unwanted. Text and image must start at the same line, of course empty space of approximately height of image at previous page is expected).

First essential questions:

• is wrapfigure the correct method how to display images wrapped by text?
• if any of above requirements can't be fulfilled (I need essentially both - width detection and proper page breaks), what should I use instead of wrapfigure?

If wrapfigure can fulfill all above requrements, how to solve wrapfigure width detection based on included image width and non-working page breaks?

• you can use a width of 0pt and wrapfig will calculate the width, see the documentation. But when using a caption this can be problematic, then it would be better to first measure the graphic. The empty \paragraph looks odd. – Ulrike Fischer Dec 20 '18 at 22:19
• Width 0pt works fine for detection, I haven't observed any issues even for captions (I tried also very long caption and it hasn't overflowed the image width). But 0pt has a side effect: now the vspace to remove top (or bottom) padding doesn't work. Is there anothew way how to remove image border (especially on the top - I need the image to be aligned to first line of my text)? Regarding empty paragraph: to be honest, I don't know the purpose - I've just somewhere read that wrapfigure MUST be followed by a paragraph and above workaround was recommended :-) – Joseph Dec 21 '18 at 5:40

You can measure the object to be placed in the wrapfigure environment. Here I add 2em of padding.

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

\newsavebox{\wrapboxitem}
\newenvironment{xwrapfigure}[2][]
{%
\sbox{\wrapboxitem}{\includegraphics[#1]{#2}}%
\wrapfloat{figure}{r}{\dimexpr\wd\wrapboxitem+2em}
\centering
\usebox{\wrapboxitem}
}
{\endwrapfloat}

\begin{document}

some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text

\begin{xwrapfigure}[height=4cm]{example-image}
\caption{Some description}
\end{xwrapfigure}

some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some text

\end{document}


• This solution works very similarly to Ulrike's 0pt advice, additionally it allows to set the horizontal border. Unfortunately it has the same side effect as 0pt - vspace{-11pt} doesn't work, so the image is always aligned to second line (as visible at your screenshot) whereas I need to align it to first line of text. Is there some other possibility how to remove vertical top border? – Joseph Dec 21 '18 at 21:28