[Tex/LaTex] Framed floats and background colors for captions

captionscolorfloatrowfloatsframed

i wanted to edit my floats. Found the package floatrow and got the desired frames easily for them. Now i would like to add a backgroundcolor to the captions. The problem is that the \parbox, i inserted to captionsetup, is bigger than the frame of the float and doesn't end up with the frame.
I wrote an example:

\documentclass{article}
\usepackage{caption}
\usepackage{floatrow} 
\floatsetup[figure]{framestyle=fbox,
           framearound=all,rowfill=yes}
\DeclareCaptionFormat{box}{
\colorbox{red}{\parbox{\textwidth}{#1#2#3}}
 } 
\captionsetup[figure]{format=box}
%%
\begin{document}
\begin{figure}
    TEST TEST TEST
\caption{BLA}
\end{figure}
\end{document}

Best Answer

A \colorbox adds additional margins, i.e. \fboxsep, so one need to subtract it two times from the \textwidth.

The easiest way to fix this is to use the pre-defined colorbox of the caption package which code already respects \fboxsep:

\documentclass{article}
\usepackage{caption}
\usepackage{floatrow} 
\floatsetup[figure]{framestyle=fbox,
           framearound=all,rowfill=yes}
\captionsetup[figure]{box=colorbox,boxcolor=red,slc=off}
%%
\begin{document}
\begin{figure}
    TEST TEST TEST
\caption{BLA}
\end{figure}
\end{document}

enter image description here

Note that this needs at least v3.3 of the caption package (and is not documented yet since I simply did not find the time to document it so far...) If v3.3 is not available and updating is not an option, one could (of course) adapt the original code by removing extra spaces and subtracting 2\fboxsep from the \parbox width:

\documentclass{article}
\usepackage{caption}
\usepackage{floatrow} 
\floatsetup[figure]{framestyle=fbox,
           framearound=all,rowfill=yes}
\DeclareCaptionFormat{box}{%
\colorbox{red}{\parbox{\dimexpr\captionwidth-2\fboxsep}{#1#2#3}}%
 } 
\captionsetup[figure]{format=box}
%%
\begin{document}
\begin{figure}
    TEST TEST TEST
\caption{BLA}
\end{figure}
\end{document}
Related Question