I'm trying to quickly add a colored background using colorbox
to all tables as shown in this answer. While I could just update all the TeX by hand, I am trying to patch the tabular
environment to keep the TeX content as "clean" as possible (and there are 200+ tables to update).
I've tried two different methods, but they are giving me the same error:
! Extra }, or forgotten \endgroup.
\color@b@x ...hbox {\kern \fboxsep {\set@color #3}
\kern \fboxsep }\dimen@ \h...
l.22 \begin{tabular}
{ll}
TeX:
\documentclass[10pt,twoside,letterpaper,openright]{scrbook}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{booktabs}
% Method One:
\usepackage{etoolbox}
\BeforeBeginEnvironment{tabular}{\colorbox{gray}\begingroup}
\AfterEndEnvironment{tabular}{\endgroup}
% Method Two
%\usepackage{xpatch}
%xpretocmd{\tabular}{\begingroup\setlength{\fboxsep}{0pt}\colorbox{gray}\begingroup}{}{\patchfailed}
%xapptocmd{\endtabular}{\endgroup\endgroup}{}{\patchfailed}
\begin{document}
\chapter{Lorem Ipsum}
\begin{table}[h]
\caption{Test Table}
\label{testtable}
\begin{tabular}{ll}
\toprule
Test 1 & Test 2 \\
Test 3 & Test 4 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
\begingroup
and\endgroup
for delimiting an argument.