9

I have a working table with cell colors:

\newcommand{\ye}{\cellcolor{yellow}}
\newcommand{\re}{\cellcolor{red}}
\begin{tabular}{l l l}
        & A        & B\\
Test1   & \gr Good & \re Bad \\
test2   & \ye OK   & \re Bad \\
test3   & \gr Good & \re Good \\
\end{tabular}

However it would improve the readability a lot, if I there were some spacing between the colors, just like the examples on page two of The official guide.

Based on section 4.3 and 4.1 in this guide I have tried both:

\newcommand{\gr}{\cellcolor{green}[0.5\tabcolsep][0.5\tabcolsep]}
\newcommand{\gr}{\cellcolor{green}[0.5\tabcolsep]}

Both of those throws these two errors (multiple times) when I compile.

! Missing number, treated as zero.
! Illegal unit of measure (pt inserted).

If i try any of

\newcommand{\gr}{\cellcolor{green}[0.5][0.5]}
\newcommand{\gr}{\cellcolor{green}[0.5]}

The text [0.5][0.5] is just inserted as normal text.

So my question is: How to add padding/spacing to the \cellcolor command, so I achieve a small white border between the cells? Preferably I would be able to control vertical and horizontal padding sperately

David Carlisle
  • 757,742
Bittenus
  • 322

2 Answers2

5

\tabcolsep is a length so you can set

\setlength\tabcolsep{5pt}

before the table to set the spacing between columns, then use a smaller amount (such as 0pt in the color optional arguments so perhaps

\cellcolor{green}[1pt]

note it has to have a unit such as pt not a number as in the examples in your question.

David Carlisle
  • 757,742
  • 13
    Tried it out. The '\tabcolsep' seems to work fine, but '\cellcolor{green}[1pt]' still only puts [1pt] as text into the cell. Is there a command length for rows, '\tabrowsep' doesn't works – Bittenus May 30 '12 at 08:48
  • Does anyone know how to get this to work? I tried to read the code, but it‘s beyond my current level of TeX trickery. – Kess Vargavind Aug 19 '23 at 15:14
0

I have found a half solution. It only solves vertical spacing, and is a bit hacky.

The solution is to use \midrule defined in booktabs to create the spacing. A normal \hrule or \hline does not work. To avoid the black ruler simply make it white using \arrayrulecolor{white} so the entire table will look like:

\newcommand{\gr}{\cellcolor{green}}
\newcommand{\ye}{\cellcolor{yellow}}
\newcommand{\re}{\cellcolor{red}}
\arrayrulecolor{white}
\begin{tabular}{l l l}           \toprule
        & A        & B        \\ \toprule
\arrayrulecolor{white}
Test1   & \gr Good & \re Bad  \\ \midrule
test2   & \ye OK   & \re Bad  \\ \midrule
test3   & \gr Good & \gr Good \\ 
\arrayrulecolor{black}           \bottomrule
\end{tabular}

I know it's not pretty, but it works - for row seperation =).

Bittenus
  • 322