Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
2  
You can't use \begingroup and \endgroup for delimiting an argument. –  egreg Feb 15 '13 at 22:41
    
Makes sense, I'm still trying... clearly I am lost. –  h0tw1r3 Feb 15 '13 at 22:49
add comment

2 Answers 2

up vote 7 down vote accepted
+50

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}

enter image description here

share|improve this answer
    
This answer explains a lot to me. Wish I could +1k. –  h0tw1r3 Feb 16 '13 at 5:53
    
@JeffreyClark You can add a bounty to the question and retroactively award it to egreg. –  lockstep Feb 16 '13 at 11:31
add comment

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}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.