# Seamless background of subfigure bodies, but not captions

I have 3 sub-figures:

\begin{figure}
\subfloat[Apple]{
\includegraphics{apple}
}
\subfloat[Orange]{
\includegraphics{orange}
}
\subfloat[Cherry]{
\includegraphics{cherry}
}
\end{figure}


I want the background behind the row of images to be a solid black rectangle, with no gaps between the images. How can I achieve this?

I have used colorbox around the separate images, but this leaves gaps between them. If I wrap the whole thing in a colorbox then the captions also have the background color, which I do not want.

I have also tried putting a tikz node in the top left and bottom right, but using overlay puts a black box on top of the images, not below them.

-

Remove the spaces from your code:

\documentclass[]{scrreprt}
\usepackage[demo]{graphicx}
\usepackage{subfig,xcolor}
\begin{document}

\begin{figure}
\subfloat[Apple]{%<---
\colorbox{red}{\includegraphics{apple}}%<...
}
\subfloat[Orange]{%<---
\colorbox{red}{\includegraphics{orange}}%<---
}
\subfloat[Cherry]{%<---
\colorbox{red}{\includegraphics{cherry}}%
}
\end{figure}

\end{document}

-
+1 This is a good suggestion. I missed a key point in my question---the captions come too close to each other if I take such an approach. Perhaps I can tweak the caption width.... I didn't think of that last night. –  Geoff Sep 2 '10 at 14:04
\captionsetup{margin=1ex}. You can also enlarge the borders of the colorbox by changing \fboxsep. –  Ulrike Fischer Sep 2 '10 at 15:00

This one does not use minipages, makebox or vphantom at all:


\documentclass{article}
\usepackage{caption,subcaption,xcolor}

%\captionsetup[subfigure]{margin=5pt}

\begin{document}

\begin{figure}
\captionsetup[subfigure]{margin=5pt}
\centering
\subcaptionbox
{A long caption about cherries that spans two lines}
[0.32\linewidth]
{\colorbox{black}{
{\color{red}\rule{0.3\linewidth}{90pt}}
}
}
\subcaptionbox
{A long caption about oranges that spans two lines}
[0.32\linewidth]
{\colorbox{black}{
{\color{orange}\rule{0.3\linewidth}{90pt}}
}
}
\subcaptionbox
{A long caption about apples that spans two lines}
[0.32\linewidth]
{\colorbox{black}{
{\color{green}\rule{0.3\linewidth}{90pt}}
}
}

\caption{Some fruits, with a black background to help with contrast.}
\end{figure}

\end{document}

-

I can only offer a bit of a hack with tikz

The background library fits the whole figure including captions. By adding a white line on the bottom of the background rectangle the captions appear again.

A much easier solution is probably not to use the \subfloat command at all and to arrange the pictures in tikz nodes and then simply use the fit-library (or again the background library) to draw the rectangle around the nodes. In this approach caption could be added with the \captionof command from the caption or the capt-of package.

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{figure}
\tikzset{background rectangle/.style={fill=black},
background bottom/.style={draw=white,line width=8ex}}
\begin{tikzpicture}[framed,tight background,show background bottom,outer frame xsep=4pt]
\node {
\subfloat[Apple]{
{\color{red}
\rule{0.3\textwidth}{90pt}}
}
\subfloat[Orange]{%
{\color{orange}\rule{0.3\textwidth}{90pt}}
}
\subfloat[Cherry]{%
{\color{red!90!green}%
\rule{0.3\textwidth}{90pt}}
}
};
\end{tikzpicture}
\end{figure}

\end{document}

-
+1 Thanks. This is a good work-around. –  Geoff Sep 2 '10 at 19:32

## Minipages and captionsetup to the rescue

@Ulrike Fischer's answer is a good start. By placing the sub-figures exactly next to each-other you can get each colorbox to touch the next one.

This, however, results in captions which may be too close to each other. To address this, I've wrapped each subfloat in a minipage and added \captionsetup{width=0.95\textwidth} to constrain the width of the sub-captions. It's also important to be sure that the contents of each colorbox is fully as wide as the space needed, so I've used yet another minipage.

For example:

\begin{figure}
\centering
\begin{minipage}{0.32\linewidth}
\captionsetup{width=0.95\textwidth}
\subfloat[A long caption about cherries that spans two lines]{
\colorbox{black}{
\begin{minipage}{\linewidth}
\centering
{\color{red}\rule{0.8\textwidth}{90pt}}
\end{minipage}
}
}
\end{minipage}
\begin{minipage}{0.32\linewidth}
\captionsetup{width=0.95\textwidth}
\subfloat[A long caption about oranges that spans two lines]{
\colorbox{black}{
\begin{minipage}{\linewidth}
\centering
{\color{orange}\rule{0.8\textwidth}{90pt}}
\end{minipage}
}
}
\end{minipage}
\begin{minipage}{0.32\linewidth}
\captionsetup{width=0.95\textwidth}
\subfloat[A long caption about apples that spans two lines]{
\colorbox{black}{
\begin{minipage}{\linewidth}
\centering
{\color{green}\rule{0.8\textwidth}{90pt}}
\end{minipage}
}
}
\end{minipage}
\caption{Some fruits, with a black background to help with contrast.}
\end{figure}


Results in:

Improve this!

I tend to overuse minipages. Please post a better answer and I'll accept it.

-
I've never found a use for minipages. I'm sure one exists, but in most cases, a \parbox works just as well. For example, here, you seem to be using minipage just to constrain the width of the box. –  TH. Sep 3 '10 at 4:24

Well you are really using a lot of minipages. As I already said in my comment I would use \captionsetup{margin=...}. You can also put a black rule between the floats. (I'm using the makebox to get a bit overhang, this will close small gaps due to pixel rounding).

\documentclass[]{scrreprt}
\usepackage[demo]{graphicx}
\usepackage{subfig,xcolor}
\begin{document}

\begin{figure}\captionsetup{margin=2ex}\fboxsep=1ex
\subfloat[A long caption about apples that spans two lines]{%
\colorbox{black}{{\color{red}\rule{0.3\linewidth}{90pt}}}%<...
}%
\makebox[1cm]{\colorbox{black}{\vphantom{\rule{0.3\linewidth}{90pt}}\hspace{1cm}}}%
\subfloat[A long caption about apples that spans two lines]{%
\colorbox{black}{{\color{orange}\rule{0.3\linewidth}{90pt}}}%<---
}
\subfloat[Cherry]{%<---
\colorbox{black}{{\color{green}\rule{0.3\linewidth}{90pt}}}%
}
\end{figure}
\end{document}

-