# Gray box around figure with a separate gray box around caption

My university requires that all figures in a thesis follow this pattern:

I'm very new to LaTeX, so far my best attempt has been this:

\begin{SCfigure}
\colorbox{darkgray}{
\begin{minipage}{14cm}
\centering
\includegraphics{inductor.jpg}
\end{minipage}
}
\caption{caption caption}
\end{SCfigure}


and the result is here:

My figure is not centered and the caption is off the page.

Trying to create another minipage around caption does not work because the colored box somehow appears in between the figure and caption.

Any help will be highly appreciated. Thank you!

• "the image http://th.upic.me/i/dy/21untitled-1.jpg cannot be displayed because it contains errors" – Roelof Spijker Nov 29 '11 at 14:30
• Welcome to TeX.sx! Please make sure that all images are uploaded using the official stackexchange interface, i.e. the image icon on top of the text field (shortcut: CTRL+G). This ensures that all images are always accessible and do not expire. (As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again.) – diabonas Nov 29 '11 at 14:33
• @wh1t3 images are fixed now – Pete Nov 29 '11 at 15:37

## 2 Answers

All problems are not resolved. You need to create a macro to automate all the little jobs and I have not used Arial and 8pt. It will be fine to cretae a macro with argument the caption and the picture. I placed a minipage inside a minipage to get margins around the caption. I think there is perhaps a better way to this. I used a box to get the height of the picture, I don't know if it's possible to avoid this.

update 4: I added a macro like Herbert and I adapted the code to odd and even pages but you need to make some calculations to replace \hspace*{-6em} and \hspace*{-4em} by something like \hspace*{\dimexpr -\marginparwidth-\marginparsep\relax} (Herbert's code). I don't know exactly what are the macros for a twoside document to calculate the left and right margin. I corrected 2 bugs. I added some extra space around the picture

\documentclass[11pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier,xcolor,graphicx}
\usepackage[font={sf}]{caption}
\usepackage{ifthen,changepage,lipsum}

\newsavebox{\mybox}

\DeclareCaptionLabelSeparator{period-newline}{.\newline\newline}
\captionsetup{aboveskip=3pt,singlelinecheck=false,
labelsep=period-newline,labelfont={small,bf}}
\newcommand\Image[4][width=\textwidth]{%
\savebox{\mybox}{\includegraphics[#1]{#2}}
\fboxsep=0pt
\checkoddpage
\ifthenelse{\boolean{oddpage}}{%
\hspace*{-25mm}
\hbox{\colorbox{black!10}{\rule{0pt}{\dimexpr \ht\mybox+4mm}%
\begin{minipage}[b]{38mm}\center
\begin{minipage}[b]{34mm}
\caption[courte]{#3}
\label{#4}
\end{minipage}%
\end{minipage}}%
\hspace*{2mm}
\colorbox{black!25}{%
\begin{minipage}[b]{145mm}\center
\vspace*{2mm}
\colorbox{white}{\usebox\mybox}%
\vspace*{2mm}
\end{minipage}}}%
}{%
\hspace*{-40mm}%
\hbox{\colorbox{black!25}{%
\begin{minipage}[b]{145mm}\center
\vspace*{2mm}
\colorbox{white}{\usebox\mybox}%
\vspace*{2mm}
\end{minipage}}%
\hspace*{2mm}
\colorbox{black!10}{\rule{0pt}{\dimexpr \ht\mybox+4mm}%
\begin{minipage}[b]{38mm}\center
\begin{minipage}[b]{34mm}
\caption[courte]{#3}
\label{#4}
\end{minipage}%
\end{minipage}}}%
}
}
\begin{document}

\section {Figures/Capturing}

Place Tables/Figures/lmages in text as close to the reterence as possible. (see Figure 1). The table, frgure or image has to put in the area which is width 14.5 cm., filled with 70\% black colm. You should leave 2 mm. tram margin. In a side oox (width 3.8 cm., filled with 15\% Black), a short caption could be in the bottom.

\begin{figure}[h!]
\Image[width=8cm]{elephant.pdf}{Long texte de légende avec quelques explications sur une figure importante}{elephant}
\end{figure}

ln tables, pictures, images or captures. use 10pt Arial regular to describe. Each f<gure (group) must include a caption set in 8-points Arial regular. The caption is to be on left or right depends on odd or even page. Figure numbering and referencing should be done sequentially, e.g. Figure. 1, Figure. 2, Table 1., Table 2 .. etc. for single figure and Figure l(a), Figure 1(b)., etc., for figures with multiple parts.

\lipsum [1]
\section {Figures/Capturing}

\begin{figure}[h!]
\Image[width=8cm]{elephant.pdf}{Long texte de légende avec quelques explications sur une figure importante}{elephant bis}
\end{figure}
Compare with  the first picture \ref{elephant}
\end{document}


• @Pete Sorry I have not enough time to cretae a macro but an expert on this site will complete this beginning of solution. – Alain Matthes Nov 29 '11 at 20:11
• I tried your macro on twoside documenclass and it's not centered. Also, the caption has to be on the inside margin. Help a little bit more please! – Pete Nov 29 '11 at 23:17
• I forgot this condition. You need to adapt my code for the exact calculation of the left margin. – Alain Matthes Nov 30 '11 at 5:35
• I prefer the method tabularx of Herbert but I can't adapt some little things like \DeclareCaptionLabelSeparator to get exactly the same picture. – Alain Matthes Nov 30 '11 at 5:46
• Yes you can use \hspace*{-2.5cm} and \hspace*{-1.5cm} but a better approach is to make calculations like Herbert but you have to know all the parameters of the layout – Alain Matthes Nov 30 '11 at 7:39

It needs two LaTeX runs to get the left/right pages correct

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}
\usepackage{graphicx,tabularx,ifthen}
\usepackage{lipsum}
\newcounter{ImageCounter}
\usepackage[font={small,sf},skip=-\normalbaselineskip]{caption}
\renewcommand\tabularxcolumn[1]{b{#1}}
\newcommand\Image[3][width=\textwidth]{%
\stepcounter{ImageCounter}\label{SpecialImage-\theImageCounter}%
\ifthenelse{\isodd{\pageref{SpecialImage-\theImageCounter}}}%
{\hspace*{-1cm}%\dimexpr -\marginparwidth-\marginparsep\relax}%
\begin{tabularx}{\dimexpr \marginparwidth+\marginparsep+\textwidth+1cm\relax}%
{@{}>{\columncolor{black!30}\centering} X @{\kern3pt} l
>{\columncolor{black!15}}b{1.3\marginparwidth} @{}}
\includegraphics[#1]{#2} & & \caption{#3}
\end{tabularx}}%
{\hspace*{\dimexpr -\marginparwidth-\marginparsep\relax}%
\begin{tabularx}{\dimexpr \marginparwidth+\marginparsep+\textwidth+1cm\relax}%
{@{}>{\columncolor{black!15}}b{1.3\marginparwidth} @{\kern3pt} l
>{\columncolor{black!30}\centering} X @{}}
\caption{#3}  & & \includegraphics[#1]{#2}
\end{tabularx}}%
}
\begin{document}

\section {Figures/Capturing}
\lipsum[1]

\begin{figure}[!htb]
\Image[width=8cm,height=6cm]{tiger}{Some nonsense text for the also superfluos
caption of the tiger for the also superfluos caption of the tiger}
\end{figure}

\lipsum[1]

\begin{figure}[!htb]
\Image[width=8cm,height=6cm]{tiger}{Some nonsense text for the also superfluos
caption of the tiger for the also superfluos caption of the tiger}
\end{figure}

\lipsum[1]
\end{document}


• Why all these calculations ? The specifications are 14.5 cm for the picture, 38 mm for the caption and 2mm for the interspace, but good idea for talularx, no need to calculate the height of the boxes. – Alain Matthes Nov 29 '11 at 21:19
• I have two problems with your code. There is a blank space at the top of the columns and the caption is not at the bottom of the first column. – Alain Matthes Nov 29 '11 at 21:33
• +1 Can this be adapted to behave differently on odd and even pages (as requested by the OP) – cmhughes Nov 29 '11 at 22:59
• @Herbert I tried your code on a twoside document and it's not centered. The caption also needs to be on the inside margin as well. Please take a look. Thanks! – Pete Nov 29 '11 at 23:22
• Is it possible to use \DeclareCaptionLabelSeparator with your method. I think you chose the best method with tabularx. – Alain Matthes Nov 30 '11 at 5:49