Picture with grey box as background
You cannot enclose the figure
environment within the argument to \greybox
. Rather, enclose the contents of the figure
environment within the argument to \greybox
.
\documentclass{article}
\usepackage{color,graphicx}
\definecolor{lightgray}{gray}{0.75}
\newcommand\greybox[1]{%
\vskip\baselineskip%
\par\noindent\colorbox{lightgray}{%
\begin{minipage}{\textwidth}#1\end{minipage}%
}%
\vskip\baselineskip%
}
\begin{document}
\greybox{Content of Grey Box}
...
// but it doesn't work with picture inside:
...
\begin{figure}[ht]
\greybox{
\footnotesize
\centering
\includegraphics[width=1.0\textwidth]{example-image}
\begin{flushright}
Some Reference
\end{flushright}
\caption{Title}
\label{fig:label}
}
\end{figure}
\end{document}
You can do this easily using the adjustbox
package. It gives you a bgcolor
key for the background color. For multi-line content you need to use the minipage
key first. You can also easily select the margin around the image by using the margin
key.
If all is \textwidth
wide then you don't need to center the content, otherwise add the center
key at the very end.
Forthermore, e.g. a frame could be easily added by using e.g. the frame
key, in which case you should substract 2\fboxrule
from the either the minipage
width or the margin
.
\documentclass{article}
\usepackage{xcolor}
\usepackage{adjustbox}
\usepackage{mwe}% just for example text and images
\begin{document}
\lipsum[0-1]
\begin{figure}[ht]
\footnotesize
\begin{adjustbox}{minipage=\textwidth-6pt,margin=3pt,bgcolor=black!40}
\includegraphics[width=1.0\textwidth]{example-image}
\adjustbox{right}{Some Reference}% add `minipage=<width>` before `right` if reference has multiple lines
\caption{Title}
\label{fig:label}
\end{adjustbox}
\end{figure}
\lipsum
\end{document}