# Set \minipage within \Declarecaptionformat width to match figure

by Noah Sprent   Last Updated April 15, 2019 11:23 AM - source

I'm trying to set up captions in my document to all have a certain format, as can be seen in the screenshot:

I'm doing this through the caption package, as can be seen in this MWE:

\documentclass[12pt]{report}

\usepackage{graphicx}
\usepackage{blindtext}

\usepackage[margin=10pt,font={footnotesize, sf},labelfont=bf, labelsep=space]{caption}
\usepackage{xcolor}
\definecolor{grey}{HTML}{EDEDEF}
\DeclareCaptionFormat{plain}{%
\fcolorbox{white}{grey}{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}#1#2#3\end{minipage}}}
\usepackage{wrapfig}

\begin{document}

\begin{wrapfigure}{R}
\centering
\includegraphics[width=0.4\linewidth]{sections/figs/intro/why.pdf}
\caption[paragraph]{\textbf{This is my test caption} \\[5pt]
Look, it is always the width of the page, \\ rather than the width of the figure, can \\ I change this?
}
\label{why}
\end{wrapfigure}

\blindtext

\end{document}


The issue is, that the width of the minipage in the caption, and therefore the width of the whole caption, is set by the linewidth rather than by the figurewidth. Is there some way that I can fix this? Am I going to have to do the caption formatting individually for each figure rather than using caption?

I'm sorry if this is something that's been asked before, if it has I couldn't find it.