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 make a table with the following code:

\begin{tabular}{@{}ccc@{}}
  \hline 
  \rowcolor[gray]{.9} a & b & c \\
  \hline
\end{tabular}

Unfortunately the \rowcolor command seems to not care about the empty leading and ending spaces in the table, i.e. the @{}'s in the tabular 'preamble'.

Any suggestions to overcome this issue are most welcome.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted
\documentclass[a4paper,12pt]{article}
\usepackage{colortbl}
\newcolumntype{C}{c<{\kern\tabcolsep}@{}}
\begin{document}

\begin{tabular}{@{}CCc@{}}\hline 
  \rowcolor[gray]{.9}[0pt][0pt] a & b & c \\\hline
\end{tabular}
\end{document}

enter image description here

share|improve this answer
    
Thank you for the solution! I actually ended up using: \newcolumntype{C}{c<{\kern\tabcolsep}<{\kern\tabcolsep}@{}} since it gave me the same distance between columns. –  lysbjerg Nov 17 '11 at 16:48
1  
I think it would make more sense logically to define two new columns L and R (say) - for the left and right no-overhang columns - as \newcolumntype{L}{>{\hspace*{-\tabcolsep}}c} and \newcolumntype{R}{c<{\hspace*{-\tabcolsep}}} and use \begin{tabular}{L..R} without the additional \rowcolor options. That is, just \rowcolor[gray]{.9}. –  Werner Nov 17 '11 at 17:03
    
Works like a charm! And I even understand what the code does :-) –  lysbjerg Nov 17 '11 at 17:46
add comment

From the userguide of the colortbl package (which provides the commands \columncolor and \rowcolor):

\rowcolor takes the same argument forms as \columncolor. It must be used at the start of a row. If the optional overhang arguments are not used the overhangs will default to ... \tabcolsep ... [emphasis added]

Here, the length \tabcolsep is (half) the width of the intercolumn white space, and equals 6pt by default. However, specifying the \rowcolor command as

\rowcolor[gray]{.9}[][] % empty contents resolve to 0[pt]

will not produce what you're after either, I'm guessing, because this will leave white gaps in the table's two interior columns. The colortbl package doesprovides a \cellcolor command, but unfortunately this command doesn't take left- or right-hand trimming options, so you'd be back where you started out with.

In short, if you insist on using the \rowcolor command, you're probably best served by omitting the @{} specifiers at the far left and far right of the tabular specification.

share|improve this answer
    
Thank you for your suggestion. That fact that \tabcolsep equals half the width of the intercolumn white space helped me to modify Herbert's solution such that the intercolumn white space is unchanged. –  lysbjerg Nov 17 '11 at 16:52
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.