[Tex/LaTex] Table Row Color Covers Text

colorcolortbltables

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

Best Answer

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

Related Question