# How to set a background color of figure environment?

Currently I've this situation:

\newenvironment{inline}[2]{
\begin{figure}[h!]
\caption{#1}\label{#2}
\vspace{10pt}%
}{
\end{figure}
}


How can I set a background color to whole content, which is in the figure?

-
Does this question help: Rounded corner colored box around figure, or is this a duplicate? –  Peter Grill Apr 7 '12 at 19:55
Note that line breaks in the source code will be converted to spaces, if they are not following a macro (without arguments) or are commented out using %. You should therefore always write {% instead if { at the end of a line. –  Martin Scharrer Apr 7 '12 at 20:34
Also figure caption belong usually below the image, not above. You should change the values of \abovecaptionskip (usually 10pt) and \belowcaptionskip (usually 0pt) if you want to have it the other way. –  Martin Scharrer Apr 7 '12 at 20:44
Many thanks to you @MartinScharrer for your further advice! –  Sam Apr 7 '12 at 21:07

You can use the mdframed package for this. In the below MWE, I've added an option first argument to the inline environment which specifies the backgroundcolor (default is black!25 = 25% black):

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{mdframed}% http://ctan.org/pkg/mdframed
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newenvironment{inline}[3][black!25]{
\begin{figure}[h!]
\begin{mdframed}[backgroundcolor=#1]
\centering\caption{#2}\label{#3}
\vspace{10pt}%
}{%
\end{mdframed}
\end{figure}
}
\begin{document}
\lipsum[1]
\begin{inline}{An image}{image}
\rule{150pt}{100pt}
\end{inline}
\lipsum[2]
\end{document}


I've only added the backgroundcolor option to mdframed, leaving all the other settings as is (for example, it includes a border). Read the mdframed documentation on how to modify any default settings.

Having a caption above the figure, you may be interested in adjusting the lengths \abovecaptionskip and \belowcaptionskip to suit your needs - they are set with the caption-below-float in mind. The defaults are 10pt and 0pt, respectively, in the standard document classes.

In the above MWE, xcolor provides the colour specification, while lipsum provided some dummy text, Lorem Ipsum style.

-
Thanks for your help! –  Sam Apr 7 '12 at 20:28

One solution could be using the package shaded as follows:

\usepackage{shaded}
\usepackage{color}

\newenvironment{inline}[2]{
\begin{figure}[h!]
\caption{#1}\label{#2}
\vspace{10pt}%
}{
\end{figure}
}


This will produce a gray background of \textwidth width. One could also use:

\definecolor{shadecolor}{rgb}{1,0.5,0}


\definecolor{shadecolor}{gray}{.9}


if the purpose is to produce a horrible orage background. You have all the freedom you need to define the color shadecolor in terms of the color package.

-

Besides the mdframed package mentioned by Werner the bgcolor key of adjustbox can also be used to color the background color of a boxed content. Here the minipage=\linewidth key must also be used to allow multiple lines (the caption and the image).

For easier comparison (and less work for me ;-) ) I reused Werner's example code. The main difference is that mdframed allows for page breaks, but this isn't important in this case because figures are never broken across pages.

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newenvironment{inline}[3][black!25]{%
\begin{figure}[h!]
\centering
\caption{#2}\label{#3}%
\vspace{10pt}%
}{%