# Wrapfigure caption on the margin

I have run into a curious problem using wrapfigure with the tufte layout, in that the caption does not stay on the margin as expected but instead spans the whole width of the caption, as per default behaviour. I have loaded the caption package to no avail. What do have to change to get the tufte behaviour? MWE

\documentclass[a4paper,justified]{tufte-handout}
\usepackage{caption}
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{wrapfigure}[20]{r}[\marginparwidth]{10cm}
\vspace{9cm}
\captionsetup{width=\marginparwidth}
\caption{Very long caption that spans several lines and son and I have no idea what to write anymore and oh boy is my keyboard noisy}
\end{wrapfigure}
\lipsum[2-3]
\end{document}


What I get looks like this, caption below the figure instead of on the side.

• Why do you want to have a wrapfigure and not a marginfigure? – TeXnician May 21 '17 at 7:56
• The figure width is wider than a marginfigure and smaller than a figure. A secondary reason is to provide variation in the page. – Huang_d May 21 '17 at 8:29

Is this what you are looking for?

I aligned the bottom of the caption to the bottom of the image (default). If you want to align to the middle or top, you should used the appropriate option for the minipage and use \raisebox{-0.5\height} or \raisebox{\dimexpr\topskip-\height} to the image.

It should be noted that using the caption package with KOMA (or TUFTE) documents is an exercise in futility. See this answer

\documentclass[a4paper,justified]{tufte-handout}
%\usepackage{caption}% not compativle with KOMA
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
\begin{wrapfigure}[20]{r}[\dimexpr \marginparsep+\marginparwidth]{10cm}
\rule{\dimexpr 10cm-\marginparwidth-\marginparsep}{9cm}%
\hspace{\marginparsep}%
\begin{minipage}[b]{\marginparwidth}
\caption{Very long caption that spans several lines and son and I have no idea what to write anymore and oh boy is my keyboard noisy}
\end{minipage}
\end{wrapfigure}
\lipsum[2-3]
\end{document}


• It works, I can adopt it to my needs. (The figure spans the margin too with the caption below on the margin. I adjust by moving the minipage to the right.) I didn't know \caption was incompatible with tufte-style layouts, thanks for the heads-up. – Huang_d May 21 '17 at 17:46