6

I am trying to create a table with alternating row colors using the xcolor package with the [table] option. The following example demonstrates that the row coloring (uncommenting the \rowcolors..) may hide/cover the table contents. In particular there seems to be an issue with the @-expressions.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}

\begin{document}

\begin{table}
    \footnotesize
    \centering
    %\rowcolors{2}{gray!15}{white}
    \begin{tabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
        &&&& Col 1 & Col 2 \\ \toprule
        Row 1 & $(100$ & $\times$ & $100)$    & $1$ & $2$ \\
        Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
     \bottomrule
\end{tabular}
\end{table}

\end{document}
  1. How can we prevent that?
  2. A second question is whether there is an easy way to cover the entire line with color; one can observe the white padding in the gray line.

enter image description here enter image description here

  • Welcome to TeX.SX! Please add a minimal working example that takes the form \documentclass{...}\usepackage{....}\begin{document}...\end{document}. If possible, it should compile and have the minimum amount of code needed to illustrate your problem. This makes it much easier for people to help you - and much more likely that they will! – Andrew May 3 '15 at 0:38
  • Apologies @Andrew. I have updated the code sample in the question. Thnx – megas May 3 '15 at 0:49
3

For the first question, you can remove the \tabcolsep separately for those two columns like

r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
                    <{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}

and for second, you can define a \bottomrulec like

\newcommand{\bottomrulec}{%
  \arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
  \arrayrulecolor{black}
}

and use it instead of \bottomrule. Here are the coloured versions for \toprule and \midrule in case if you need them.

\newcommand{\toprulec}{%
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
  \arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}
}
\newcommand{\midrulec}{%
  \arrayrulecolor{gray!15}\specialrule{\aboverulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}

Your code modified:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\newcommand{\bottomrulec}{% Coloured \toprule
  \arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
  \arrayrulecolor{black}
}

\begin{document}

\begin{table}
    \footnotesize
    \centering
    \rowcolors{2}{gray!15}{white}
    \begin{tabular}{lr<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
                        <{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
        &&&& Col 1 & Col 2 \\ \toprule
        Row 1 & $(100$ & $\times$ & $100)$    & $1$ & $2$ \\
        Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
     \bottomrulec
\end{tabular}
\end{table}

\end{document}

enter image description here

4

The white space comes from booktabs adding some vertical padding above and below table rules. A workaround consists in setting this padding to $0$ pt and replacing it with vertical spacing with the cellspace package: it defines a minimal such vertical spacing and is compatible with \rowcolors (the column specifier must be preceded by the letter S).

As for the issue with @{}, I replace it with adding negative horizontal spacing on leaving cells in the second column and on entering the third, with >{} and <{}.

\documentclass[preview]{article}
\usepackage{mathtools}
\usepackage{tabularx, booktabs, caption, array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}

    \begin{document}

\begin{table}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
 \rowcolors{2}{gray!15}{white}% <{\hskip-\arraycolsep}>{\hskip-\arraycolsep{}}
    $ \begin{array}{Slr <{{}\hskip-\arraycolsep}>{\hskip-\arraycolsep\mkern-.5mu}lcc}
        && & \text{Col 1} & \text{Col 2} \\
        \toprule
        \text{Row 1} & (100 \times{} & 100) & 1 & 2 \\
        \text{Row 2} & (100 \times{} &1000) & 3 & 4 \\
        \bottomrule
    \end{array} $
\end{table}

\end{document} 

enter image description here

  • Thanks Bernard. That was really helpful. One quick question: I have never used array instead of tabular for a table before. Is that choice important in this solution or is it convenient assuming that most of the table contents is numerical? – megas May 3 '15 at 2:20
  • It was only because most cells seemed to be in math mode. However loading the array package is always useful, as you get access to some useful functionalities, even in a tabular environment. – Bernard May 3 '15 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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