7

Does anyone know how I can set up a grey box as background for my images like in this example in the picture below?

Picture with grey box as bg

I had found a good example in Grey box code and lists but it doesn't work with a picture inside.

\documentclass{article}

\usepackage{color}
\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:

...
    \greybox{
       \begin{figure}[ht]
          \footnotesize
          \centering
          \includegraphics[width=1.0\textwidth]{illustrationen/mygraphic.png}
          \begin{flushright}
             Some Reference
          \end{flushright}
          \caption{Title}
          \label{fig:label}
       \end{figure}
    }

\end{document}

Anyone a simple idea for that?

5

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}

enter image description here

3

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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