# Background color for existing floats (e.g. w/o defining a new environment)?

I'm trying to make all my figures (including caption) backgrounded with a decent color.

But all these answers have in common that they define some new environments or commands that need to be put inside the figures or replace the figures completly.

Is there a way to put a background color behind existing floats without having to change the content of these existing floats?

-
You can use the link answers. Just redefine the environment figure. –  Marco Daniel Apr 4 '13 at 18:55
How about background color with adjustbox –  texenthusiast Apr 4 '13 at 18:59

\documentclass{article}

\makeatletter

\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def\@xfloat ##1[##2]{#1%
\normalcolor
\vbox \bgroup\aftergroup\egroup
#2}}
\expandafter\foo\@xfloat{#1}[#2]!!

\makeatother

\usepackage{color}
\begin{document}

\begin{figure}
a\\b\\c
\caption{yes no}
\end{figure}

one two three

\end{document}

-
Thanks, how do select the float types (figure, table, ...) for which these apply? For example if I want different colors for figures and tables. –  Foo Bar Apr 4 '13 at 19:07
\@captype will be figure table etc so you could test that or if it is just a matter of colour choice replace yellow by color-\@captype and then use \definecolor to define colors color-figure and color-table –  David Carlisle Apr 4 '13 at 19:10
Thanks, this works perfectly. :) –  Foo Bar Apr 4 '13 at 19:35
@DavidCarlisle, what is the meaning of !! on the 3rd line of the code? –  Sigur Apr 4 '13 at 23:29
@Sigur It's just a token to mark the end of the argument (the end of the expansion of \@xfloat anything could be used that is not in the expansion of the macro so long as the same marker is used 6 lines later after the expansion of \@xfloat. –  David Carlisle Apr 5 '13 at 0:09

Here is an elementary implementation using mdframed that redefines the figure environment (therefore not adapting the usage of figure in your document):

\documentclass{article}
\usepackage{lipsum,mdframed,xcolor}% http://ctan.org/pkg/{lipsum,mdframed,xcolor}
\let\oldfigure\figure% Store old figure environment start
\let\endoldfigure\endfigure% Store old figure environment end
\renewenvironment{figure}[1][htbp]% Redefine figure
{\oldfigure[#1]\mdframed[backgroundcolor=blue!15]}
{\endmdframed\endoldfigure}
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering\includegraphics{example-image-a}
\caption{This is a figure}
\end{figure}
\lipsum[2]
\end{document}


From here it would be straight forward to apply a different style to a different float (like table, for example).

-
Tried this, too. Worked well, hence the upvote, but I have accepted the other answer because it does not depend on a package (which could possbibly introduce unwanted side effects). –  Foo Bar Apr 4 '13 at 19:37