# Restrict background color to varwidth's narrower width with adjustbox?

I want to wrap a frame, with background color, around each table, with its caption included within the frame. For the frame, I've adapted Martin Scharrer's answer to "Adding border around figure and caption," which uses an adjustbox environment with the varwidth key. The frame itself is perfect, particularly that the width of the box shrinks (from a max of \textwidth) to fit the table and caption.

However, when I add a bgcolor key to set a background color, the width of the background appears to be the entire \textwidth, but I want the color to remain within the bounds of the frame. I.e., I want the background to respect the narrower width that varwidth determined. (MWE and output follows.)

How do I do this?

\documentclass{article}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|cc|}
\hline
x   &   y   \\
a   &   b   \\
\hline
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}


UPDATE

Following up on @jon's observation in a comment: It turns out that the scope of the background color's coverage depends on where in the list of key=value pairs bgcolor falls.

In the below, new graphic, I present three outcomes:

• bgcolor is in position #1 or #2 (bgcolor=pink,varwidth=\textwidth,padding=1em,frame=0.4pt,center) or {varwidth=\textwidth,bgcolor=pink,padding=1em,frame=0.4pt,center}
• bgcolor is in position #3 or #4 {varwidth=\textwidth,padding=1em,bgcolor=pink,frame=0.4pt,center} or {varwidth=\textwidth,padding=1em,frame=0.4pt,bgcolor=pink,center}
• bgcolor is in the last position {varwidth=\textwidth,padding=1em,frame=0.4pt,center,bgcolor=pink}

I couldn't find anything in the adaptbox documentation that explains that the order of bgcolor would matter. But the story one could tell that rationalizes the pattern above is:

• When bgcolor falls before padding, it applies to the innermost (pre-padding) content;
• When bgcolor falls after padding, it applies to the "padded" content;
• When bgcolor falls after center, it applies to the widest area, i.e., after the line has been filled to the margins on the right and left.

(It would be great if Martin Scharrer could weigh in here about exactly what's going on. 😉)

• Surprisingly, the order in which you put bgcolor seems to matter. Feature or bug? – jon Oct 4 '18 at 4:07
• @jon, astute observation! I get three different results depending on where I put bgcolor. The order that gives me what I want is: varwidth=\textwidth,padding=1em,bgcolor=pink,frame=1pt,center – Jim Ratliff Oct 4 '18 at 4:44
• I've updated the question to show explicitly the dependence on the order in which bgcolor appears of the scope of coverage of the background color. – Jim Ratliff Oct 4 '18 at 16:45

This solution uses varwidth and \fcolorbox.

\documentclass{article}
\usepackage{varwidth,xcolor}
\begin{document}
\begin{table}
\centering
\fcolorbox{black}{pink}{\begin{varwidth}{\textwidth}
\centering
\begin{tabular}{|cc|}
\hline
x   &   y   \\
a   &   b   \\
\hline
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{varwidth}}
\end{table}
\end{document}


• interesting solution that clearly works. I'm going to see what other solutions might come forward (including see new comment to original question, where just moving the location of bgcolor fixes the problem (for unknown reasons). I've upvoted you in the meantime. – Jim Ratliff Oct 4 '18 at 4:48

I'm answering my own question just in order to bring this question to closure. As commenter @jon astutely observed: It matters what the order of the key values is; specifically, where bgcolor falls in the list.

In my case, I needed the scope of the coverage of bgcolor to (a) include the region after the padding is applied but (b) before center is applied. Thus, either of the following two key=value orders works:

• bgcolor is in position #3 or #4 {varwidth=\textwidth,padding=1em,bgcolor=pink,frame=0.4pt,center} or {varwidth=\textwidth,padding=1em,frame=0.4pt,bgcolor=pink,center}

Although I didn't find the significance of the order of bgcolor to be documented in the adjustbox documentation, the author of adjustbox, Martin Scharrer, has separately confirmed to me that this is a feature (not a bug):

Hi, that is a feature. You can add other keys after the background color, like adding another frame or margin or rotation and then add another background if you want. This way you have the maximum freedom.