From: tvz@zandtwerk.Princeton.EDU (Timothy Van Zandt)
Subject: Re: Shading boxes in \tabular (Latex)
Organization: Princeton University
Date: Fri, 10 Apr 1992 19:56:32 GMT

In article <dobo.702921675@swalwell> dobo@cs.UAlberta.CA (W. Dobosiewicz) writes:
>I have a table with numeric entries. I would like to shade a couple of the
>entries in the table. I have seen publications that have it, so it must be
>feasible. Alas, I have no idea how. Any help?
>
>Wlodek Dobosiewicz

A while ago I posted the following experimental macros asking
if anyone knew of a better way. I didn't get any response,
and I haven't done anything with
these, and in particular, there is no LaTeX interface (not
much point until the primitives work right), but here they
are again.
          -- Tim

%% colortab.tex :: Exploratory macros for adding color to alignments.
%% Run with Plain TeX.
%% Cells are colored using \hrules and \vrules. The problem is in
%% figuring out how large to make the rules.
%% tvz@Princeton.EDU

%% Colors: 
\def\psVerb#1{\special{ps: #1}}   % For dvips
\def\endcolor{\psVerb{grestore}}
\def\color#1{\psVerb{gsave #1}\aftergroup\endcolor}
\def\yellow{\color{1 1 0 setrgbcolor}}
\def\gray{\color{.75 setgray}}
\def\blue{\color{0 0 1 setrgbcolor}}
\def\red{\color{1 0 0 setrgbcolor}}
\def\white{\color{1 setgray}}

%% Make lines thicker, vertical spacing wider, to get better view:
\setbox\strutbox=\hbox{\vrule height11.5pt depth6.5pt width0pt}
\def\Hrule{\hrule height 1.5pt}
\def\Vrule{\vrule width 1.5pt}

%% An alignment structure without color:
Plain:
\bigskip
\vbox{\offinterlineskip
\halign{\Vrule\strut\quad\hfil#\hfil\quad
       &\Vrule\hbox to 1in{\quad#\hfil\quad}%
       &\Vrule\quad\hfil#\quad\Vrule\cr
  \noalign{\Hrule}%
  Broil, grill&dog&went home\cr\noalign{\Hrule}%
  This is&another&john\cr\noalign{\Hrule}%
  and here&is another&mary jones\cr\noalign{\Hrule}}}
\bigskip\bigskip

%% Coloring the lines is easy:
Lines:
\bigskip
\vbox{\offinterlineskip
\halign{{\blue\Vrule}\strut\quad\hfil#\hfil\quad
       &\Vrule\hbox to 1in{\quad#\hfil\quad}%
       &{\gray\Vrule}\quad\hfil#\quad\Vrule\cr
  \noalign{\Hrule}%
  Broil, grill&dog&went home\cr\noalign{{\red\Hrule}}%
  This is&another&john\cr\noalign{\Hrule}%
  and here&is another&mary jones\cr\noalign{{\blue\Hrule}}}}
\bigskip\bigskip

%% So is coloring a column with fixed width:
%% "\gray\Vrule width 1in\hskip -1in" puts a gray blob down that is the size
%% of the cell and then backs up.
Color column of fixed width:
\bigskip
\vbox{\offinterlineskip
\halign{\Vrule\strut\quad\hfil#\hfil\quad
       &\Vrule{\gray\Vrule width 1in\hskip -1in}%
         \hbox to 1in{\quad#\hfil\quad}%
       &\Vrule\quad\hfil#\quad\Vrule\cr
  \noalign{\Hrule}%
  Broil, grill&dog&went home\cr\noalign{\Hrule}%
  This is&another&john\cr\noalign{\Hrule}%
  and here&is another&mary jones\cr\noalign{\Hrule}}}
\bigskip
\bigskip

%% Color row cells of known height (size of \strut) by putting down
%% a row of blobs and then moving back up the page.
Color cell of known height:
\bigskip
\def\cc#1{\omit{\special{ps: gsave #1}%
  \leaders\Hrule depth\dp\strutbox height\ht\strutbox\hfil
  \special{ps: grestore}}}
\def\rowcolors#1\cr{%
  \noalign{\global\let\Color\color\global\let\color\cc}%
  #1\cr\noalign{\vskip -\dp\strutbox\vskip-\ht\strutbox
  \global\let\color\Color}}
\vbox{\offinterlineskip
\halign{\Vrule\strut\quad\hfil#\hfil\quad
       &\Vrule\hbox to 1in{\quad#\hfil\quad}%
       &\Vrule\quad\hfil#\quad\Vrule\cr
  \noalign{\Hrule}%
  Broil, grill&dog&went home\cr\noalign{\Hrule}%
  \rowcolors\yellow&\white&\red\cr
  This is&another&john\cr\noalign{\Hrule}%
  and here&is another&mary jones\cr\noalign{\Hrule}}}
\bigskip
\bigskip

%% To color row cells of unknown height we first have to put the rows
%% in an alignment and measure the resulting height, and then do
%% what we did for rows of known height. The preamble of the alignment
%% is saved in a token register so that it can be used to determine the
%% height of the rows.
Color row cells of unknown height:
\bigskip
\newtoks\preamble
\newbox\rowbox
\newdimen\rowsize
\def\CC#1{\omit{\psVerb{gsave #1}%
  \leaders\hrule height\rowsize\hfil\psVerb{grestore}}}
\def\Rowcolors#1#2{\noalign{%
  \global\let\Color\color
  \global\let\color\CC
  \global\setbox\rowbox=\vbox{\halign{\span\the\preamble#2}}%
    \rowsize=\ht\rowbox\global\advance\rowsize by \dp\rowbox}%
  #1\cr\noalign{\vskip -\rowsize\global\let\color\Color}%
  #2}
\preamble{\Vrule\strut\quad\hfil#\hfil\quad
       &\Vrule\hbox to 1in{\quad#\hfil\quad}%
       &\Vrule\quad\hfil#\quad\Vrule\cr}
\vbox{\offinterlineskip
\halign{\span\the\preamble
  \noalign{\Hrule}
  \Rowcolors{\yellow&\white&\red}
    {Broil, grill&dog&went home\cr}\noalign{\Hrule}
  \Rowcolors{\gray&\yellow&\white}
  {This is&another&john\cr\noalign{\Hrule}
  and here&is another&mary jones\cr}\noalign{\Hrule}}}
\bigskip
\bigskip

\bye

-- 
Timothy Van Zandt   (609)258-4050  tvz@Princeton.EDU (NeXTMail)
Dept. of Economics,  Princeton University,  Princeton, NJ 08544