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:

enter image description here

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.

Thanks in advance!



Related Questions


Justification of the caption of a wrapfigure

Updated September 22, 2017 18:23 PM

How to remove the word "Figure" from a caption?

Updated April 13, 2019 13:23 PM

Wrapfigure with caption and floatfoot problem

Updated February 18, 2017 18:23 PM

Caption changes when using wrapfigures

Updated July 20, 2017 14:23 PM

Caption on tables with wrapfigure

Updated May 22, 2017 20:23 PM