Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

I already found these questions with some nice answers:

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?

share|improve this question
    
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
add comment

2 Answers

up vote 6 down vote accepted

enter image description here

\documentclass{article}

\makeatletter

\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def\@xfloat ##1[##2]{#1%
 \normalcolor
      \hbox\bgroup{\color{yellow}\leaders\vrule\hskip\columnwidth\hskip-\columnwidth}%
      \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}
share|improve this answer
    
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
1  
@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
add comment

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

enter image description here

\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).

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.