Patch tabular environment with colorbox
I would be careful to redefine the tabular
environment altogether, as it may be used in different contexts than a table (for example to typeset the author name and address). Since probably the tables to which you want to add the background color are in table
environments, we'll patch only those.
\documentclass[10pt,twoside,letterpaper,openright]{scrbook}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{booktabs}
\usepackage{xpatch,letltxmacro}
%% A storage bin
\newsavebox{\tabularbox}
%% We patch the tabular environments only inside table
\AtBeginEnvironment{table}{%
\LetLtxMacro\tabular\colortabular
\LetLtxMacro\endtabular\endcolortabular
}
%% Start: save a copy of \tabular and \endtabular
\LetLtxMacro\colortabular\tabular
\LetLtxMacro\endcolortabular\endtabular
%% Patch the copies
\xpretocmd{\colortabular}
{\setbox\tabularbox=\hbox\bgroup} % start building a box
{}{}
\xapptocmd{\endcolortabular}
{%
\egroup % finish the box
\begingroup
\fboxsep=0pt % no padding
\colorbox{black!30}{\box\tabularbox}% typeset the box on a background
\endgroup
}
{}{}
\begin{document}
\chapter{Lorem Ipsum}
\begin{table}[h]
\centering
\caption{Test Table}\label{testtable}
\begin{tabular}{ll}
\toprule
Test 1 & Test 2 \\
Test 3 & Test 4 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
With a new environment Tabular
with the same parameter settings
\documentclass[parskip=half*]{scrbook}
\usepackage{xcolor,varwidth}
\usepackage{booktabs}
\newsavebox\TBox
\newenvironment{Tabular}
{\begin{lrbox}{\TBox}\varwidth{\linewidth}\tabular}
{\endtabular\endvarwidth\end{lrbox}%
\fboxsep=1pt\colorbox{black!20}{\usebox\TBox}}
\begin{document}
\chapter{Lorem Ipsum}
\begin{Tabular}{ll}\toprule
Test 1 & Test 2 \\
Test 3 & Test 4 \\\bottomrule
\end{Tabular}
\begin{tabular}{ll}\toprule
Test 1 & Test 2 \\
Test 3 & Test 4 \\\bottomrule
\end{tabular}
\end{document}