How do I get an entire table to get a background color. I dont mean the indivual cells but the entire table to get a gray shade. Something like this:
3 Answers
I'd simply put the tabular
inside a \colorbox
:
\documentclass{article}
\usepackage{booktabs,xcolor,siunitx}
\definecolor{lightgray}{gray}{0.9}
\begin{document}
\begingroup\setlength{\fboxsep}{0pt}
\colorbox{lightgray}{%
\begin{tabular}{l*{4}{S[table-format=3.2]}@{}}
\toprule
Method & \multicolumn{4}{c}{Recognition rate (\%) vs.\ illumination} \\
\cmidrule{2-5}
& \multicolumn{1}{c}{Subset 2} &
\multicolumn{1}{c}{Subset 3} &
\multicolumn{1}{c}{Subset 4} &
\multicolumn{1}{c}{Subset 5} \\
\midrule
Linear subspace [9] & 100.00 & 100.00 & 85.00 & {n/a} \\
Cones-attached [9] & 100.00 & 100.00 & 91.40 & {n/a} \\
Cones-cast [9] & 100.00 & 100.00 & 100.00 & {n/a} \\
PCA & 98.33 & 79.17 & 30.00 & 15.79 \\
LTV${}+{}$PCA & 100.00 & 99.17 & 96.43 & 92.11 \\
Our method${}+{}$PCA & 100.00 & 100.00 & 100.00 & 100.00 \\
\bottomrule
\end{tabular}%
}\endgroup
\end{document}
The local setting of \fboxsep
is needed to avoid padding.
-
would it be doable to embed the background color into a new tabular environment?– plutonCommented Apr 2, 2013 at 2:27
-
@pluton You need to know the precise dimensions of the foreground.– egregCommented Apr 2, 2013 at 8:23
-
@Dan egreg's answer does not seem to work when the table is inside a
\newtcbtheorem
but Dan's answer does. Commented May 27, 2016 at 3:01
I guess you can use the xcolor
package as explained here, but instead of set up alternate table row colors, force to color odd and even rows using the same color:
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\rowcolors{1}{gray}{gray}
Here is you have a full working example:
\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\begin{document}
\begin{table}[ht]
\caption{default}
\begin{center}
\rowcolors{1}{lightgray}{lightgray}
\begin{tabular}{r|rrrrr}
\hline
& 1 & 2 & 3 & 4 & 5 \\
\hline
1 & 2.36 & 1.08 & -0.49 & -0.82 & -0.65 \\
2 & -0.68 & -1.13 & -0.42 & -0.72 & 1.51 \\
3 & -1.00 & 0.02 & -0.54 & 0.31 & 1.28 \\
4 & -0.99 & -0.54 & 0.97 & -1.12 & 0.59 \\
5 & -2.35 & -0.29 & -0.53 & 0.30 & -0.30 \\
6 & -0.10 & 0.06 & -0.85 & 0.10 & -0.60 \\
7 & 1.28 & -0.46 & 1.33 & -0.66 & -1.80 \\
8 & 0.80 & 0.46 & 1.37 & 1.73 & 1.93 \\
9 & -0.75 & 0.28 & 0.51 & 0.19 & 0.58 \\
10 & -1.64 & -0.12 & -1.17 & -0.10 & -0.04 \\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}
And the result:
EDIT: Following Mico's comment, there are two limitations:
- The space around lines created by the
booktabs
package's\toprule
,\midrule
,\cmidrule
, and\bottomrule
commands and - Any intercolumn whitespace. E.g., if one specifies
@{\extracolsep{\fill}}
in thetabular*
environment's second argument -- to force the overall width of the table to be equal to the environment's width argument (usually, but not necessarily,\textwidth
) -- the extra intercolumn whitespace
won't be affected by the \rowcolor statement.
-
You may want to add a note mentioning two limitations: coloring won't be made for (i) the space around lines created by the
booktabs
package's\toprule
,\midrule
,\cmidrule
, and\bottomrule
commands or (ii) any intercolumn whitespace. E.g., if one specifies@{\extracolsep{\fill}}
in thetabular*
environment's second argument -- to force the overall width of the table to be equal to the environment's width argument (usually, but not necessarily,\textwidth
) -- the extra intercolumn whitespace won't be affected by the\rowcolor
statement. :-(– MicoCommented Jul 19, 2012 at 11:03
The environment {NiceTabular}
of nicematrix
has a tool to color the whole array (and tools to color rows, columns, cells and block). We keep the ability to use verbatim in the your tabular.
\documentclass{article}
\usepackage{booktabs,xcolor,siunitx}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{l*{4}{S[table-format=3.2]}@{}}
\CodeBefore
\arraycolor[gray]{0.9}
\Body
\toprule
Method & \multicolumn{4}{c}{Recognition rate (\%) vs.\ illumination} \\
\cmidrule{2-5}
& \multicolumn{1}{c}{Subset 2} &
\multicolumn{1}{c}{Subset 3} &
\multicolumn{1}{c}{Subset 4} &
\multicolumn{1}{c}{Subset 5} \\
\midrule
Linear subspace [9] & 100.00 & 100.00 & 85.00 & {n/a} \\
Cones-attached [9] & 100.00 & 100.00 & 91.40 & {n/a} \\
Cones-cast [9] & 100.00 & 100.00 & 100.00 & {n/a} \\
PCA & \verb|&#&&| & 79.17 & 30.00 & 15.79 \\
LTV${}+{}$PCA & 100.00 & 99.17 & 96.43 & 92.11 \\
Our method${}+{}$PCA & 100.00 & 100.00 & 100.00 & 100.00 \\
\bottomrule
\end{NiceTabular}
\end{document}
You need several compilations (because nicematrix
uses PGF/Tikz nodes under the hood).