1

I would like to have an entire table (including the caption) with a background color. I already found solution for cells and rows (which also did not work perfectly), but not for the whole table.

Can I build an whole environment around it? If yes, how?

Thank you very much in advance

Paul

1
  • 1
    Please give the code you've already written. This will serve as a starting point for users wishing to respond.
    – AndréC
    Feb 19, 2020 at 19:09

2 Answers 2

2

You can use simply \colorbox{your color}{your table}

MWE:

\documentclass{article}
\usepackage{booktabs,xcolor}
\begin{document}
\begin{table}
\caption{Foo}\centering 
\colorbox{green!20}{\begin{tabular}{cc}
foo & baz\\
\midrule
11 & 12 \\
13 & 14 \\
\end{tabular}}
\end{table}
\end{document}

It could look better with some settings before of the table (but inside the float if you want limit the setting to this table only). For instance:

\setlength\fboxsep{1ex}
\setlength\belowcaptionskip{1ex}
\setlength\tabcolsep{1ex}

mwe

5
  • How do you get the caption into the green area with this solution?
    – Paul
    Feb 20, 2020 at 8:36
  • Put the caption and the tabular inside a minipage (some like ` \begin{minipage}{7em} ... \end{minipage}`. Then apply the colorbox to the minipage instead that to the tabular environment only.
    – Fran
    Feb 20, 2020 at 10:18
  • \begin{minipage}{7em} \colorbox{green!20}{ \begin{table} \caption{Foo}\centering \begin{tabular}{cc} foo & baz\\ \midrule 11 & 12 \\ 13 & 14 \\ \end{tabular} \end{table}} \end{minipage} , does not work for me :-(
    – Paul
    Feb 20, 2020 at 12:02
  • @user34047 No, no, no ... the tabular environment and the caption inside the minipage, the minipage inside the color box, the color box inside the table. The table environment is a float and this mean that it cannot be inside anything (except the document environment, of course).
    – Fran
    Feb 20, 2020 at 12:11
  • Thx a lot. Now it works
    – Paul
    Feb 20, 2020 at 12:49
2

The following MWE based on tcolorbox might serve as a starting point:

enter image description here

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{booktabs}

\newtcolorbox[blend into=tables]{mytable}[2][]{float=htb, 
                                               halign=center,  
                                               title={#2}, 
                                               every float=\centering, 
                                               sharp corners,
                                               coltitle=black,
                                               colback=yellow,
                                               colbacktitle=yellow,
                                               boxrule=0pt,
                                               frame hidden,
                                               #1}

\begin{document}

\begin{mytable}[float=t, label=mykey]{This is a floating box}
\begin{tabular}{cc}
\toprule
column header 1 & column header 2\\
\midrule
1 & 2 \\
3 & 4 \\
\bottomrule
\end{tabular}
\end{mytable}

\end{document}
3
  • Thank you so much leandriis! One quick follow ups: 1) How can I get the caption/title to the bottom?
    – Paul
    Feb 19, 2020 at 21:06
  • By the way: Would "\newtcolorbox[blend into=tikzfigures]{mytikzfig}[2][]{float=htb, halign=center, title={#2}, every float=\centering, sharp corners, coltitle=black, colback=yellow, colbacktitle=yellow, boxrule=0pt, frame hidden, #1}" work the same way?
    – Paul
    Feb 19, 2020 at 21:46
  • Regarding your second question: According to the manual, you can only choose from tables, figures or listings for blend into=.
    – leandriis
    Feb 20, 2020 at 18:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .