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?
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?
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.
One solution could be using the package shaded as follows:
\usepackage{shaded}
\usepackage{color}
\definecolor{shadecolor}{gray}{.9}
\newenvironment{inline}[2]{
\begin{figure}[h!]
\begin{shaded}
\caption{#1}\label{#2}
\vspace{10pt}%
}{
\end{shaded}
\end{figure}
}
This will produce a gray background of \textwidth width. One could also use:
\definecolor{shadecolor}{rgb}{1,0.5,0}
instead of:
\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{adjustbox}% http://ctan.org/pkg/adjustbox
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newenvironment{inline}[3][black!25]{%
\begin{figure}[h!]
\begin{adjustbox}{minipage=\linewidth,bgcolor={#1}}% maybe also: margin=x y
\centering
\caption{#2}\label{#3}%
\vspace{10pt}%
}{%
\end{adjustbox}%
\end{figure}%
}
\begin{document}
\lipsum[1]
\begin{inline}{An image}{image}
\rule{150pt}{100pt}
\end{inline}
\lipsum[2]
\end{document}
%. You should therefore always write{%instead if{at the end of a line. – Martin Scharrer Apr 07 '12 at 20:34\abovecaptionskip(usually 10pt) and\belowcaptionskip(usually 0pt) if you want to have it the other way. – Martin Scharrer Apr 07 '12 at 20:44