[Tex/LaTex] Boxed caption and figure, with colored caption

boxescolorfloats

I'm trying to recreate a figure and caption effect from a journal. An example of it can be seen on PDF version of Figure 1 from this article:

enter image description here

It's basically a large grey bordered box that goes around the image and caption, and the bottom section, where the caption is filled in grey.

I'm currently using

\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{mystyle1}{
    backgroundcolor=white!20
}
\mdfdefinestyle{mystyle2}{
    backgroundcolor=lightgray!20
}

in the preamble, and

\begin{figure}[htb!]
\centering
    \begin{mdframed}[style=mystyle1]
        \includegraphics[width=1.0\textwidth]{foo.pdf}
        \begin{mdframed}[style=mystyle2]
            \caption[foo]{foo}
        \end{mdframed}
    \end{mdframed}
\label{fig:foo}
\end{figure}

This creates a box around the whole figure and caption, and then a subbox around the caption, but they don't lineup, and I can't figure out how to change the border colour.

Any help would be appreciated.

Best Answer

Below I present one option using tcolorbox to define a figurebox environment. This environment has an optional argument to specify placement options (just as for the standard figure environment).

Instead of using two boxes, I just used one box; the upper part is used for the image and the lower one, for the caption. Use \tcblower just before \caption.

The caption package was used to easily change the color for the caption labels. Change the settings according to your needs:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{lipsum}

\definecolor{captioncol}{RGB}{46,86,145}

\newtcolorbox{mycaptionbox}{
  freelance,
  enhanced,
  colframe=gray,
  arc=0pt,
  middle=2pt,
  outer arc=0pt,
  boxrule=0.7pt,
  enlarge left by=-5mm,
  enlarge right by=-5mm,
  text width=\textwidth-1.4pt,
  nobeforeafter,
  segmentation hidden=true,
  interior code={
  \path[draw=none,fill=gray!30]
    (interior.south west) rectangle (segmentation.north east);
  \path[draw=none,fill=white]
    (segmentation.west) rectangle (interior.north east);
  \draw[draw=gray,line width=0.7pt]
    (segmentation.west) -- (segmentation.east);
  },
}

\newenvironment{figurebox}[1][tbp]
  {\begin{figure}[#1]\begin{mycaptionbox}}
  {\end{mycaptionbox}\end{figure}}

\DeclareCaptionFormat{colorlabel}{\textcolor{captioncol}{#1#2}#3}
\captionsetup{format=colorlabel}

\begin{document}

\lipsum[4]
\begin{figurebox}
\includegraphics[height=3cm,width=.6\linewidth]{example-image-a}
\tcblower
\caption{\protect\lipsum[2]}
\end{figurebox}

\end{document}

The output:

enter image description here