1

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!

  • Have you considered \linewidth i stead of \textwidth, text width never changes, linewidth does – daleif Apr 15 at 11:07
  • Thanks very much! This solved the issue for me. Do you want to submit it as a solution so I can accept it? – Noah Sprent Apr 15 at 12:38
0

In your configuration, use \linewidth instead of \textwidth. In most latex constructions, \linewidth is changed according to the current configuration. On the other hand \textwidth is constant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.