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.

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?

share|improve this question
    
Does this question help: Rounded corner colored box around figure, or is this a duplicate? –  Peter Grill Apr 7 '12 at 19:55
2  
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
2  
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
add comment

3 Answers

up vote 3 down vote accepted

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):

enter image description here

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

share|improve this answer
    
Thanks for your help! –  Sam Apr 7 '12 at 20:28
add comment

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

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.

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