4

Color tables work great when using within a single tabular environment. However, when using it within nested tabular environments, a rowcolor command spans the whole nested table instead of a single row within the nexted table. This is show in the following sample file

\documentclass{report}
\usepackage{colortbl}

\begin{document}

\begin{center}
\small\sf
\begin{tabular}[t]{|c|c|c|c|}\hline
\rowcolor[gray]{.8} EmpID & FirstName & LastName & Title \\\hline
e1 & John & Smith & Mr. \\
e2 & Mary & Heart & Mrs. \\
e3 & Juan & Guerra & Mr. \\\hline
\end{tabular}
\hspace{3mm}
\begin{tabular}[t]{|c|c|c|}\hline 
\rowcolor[gray]{.8} ProductKey & TimeKey & Quantity \\\hline
p2 & t2 & 100 \\
p3 & t3 & 100 \\
p4 & t4 & 100 \\
p5 & t5 & 100 \\
p6 & t6 & 100 \\\hline
\end{tabular}
\end{center} 

\begin{center}
\small\sf
\begin{tabular}{ccc}
\begin{tabular}[t]{|c|c|c|c|}\hline
\rowcolor[gray]{.8} EmpID & FirstName & LastName & Title \\\hline
e1 & John & Smith & Mr. \\
e2 & Mary & Heart & Mrs. \\
e3 & Juan & Guerra & Mr. \\\hline
\end{tabular}
& &
\begin{tabular}[t]{|c|c|c|}\hline 
\rowcolor[gray]{.8} ProductKey & TimeKey & Quantity \\\hline
p2 & t2 & 100 \\
p3 & t3 & 100 \\
p4 & t4 & 100 \\
p5 & t5 & 100 \\
p6 & t6 & 100 \\\hline
\end{tabular}
\end{tabular}
\end{center} 

\end{document}

When I compile it with pdflatex using MikTeX I obtain the following output

problem with rowcolor in nested tabular

3 Answers 3

2

use instead \cellcolor[black]{0.8} for every column in that line:

\cellcolor[black]{0.8} EmpID & \cellcolor[black]{0.8}FirstName & \cellcolor[black]{0.8}LastName & \cellcolor[black]{0.8}Title \\\hline

or write in the following tabular row

\rowcolor{white}

or (my preferred solution) use package xcolor and the command \hiderowcolors

\documentclass{report}
\usepackage[table]{xcolor}

\begin{document}

\begin{center}
\small\sf
\begin{tabular}{ccc}
\begin{tabular}[t]{|c|c|c|c|}\hline
\rowcolor[gray]{.8} EmpID & FirstName & LastName & Title \\\hline
\hiderowcolors
e1 & John & Smith & Mr. \\
e2 & Mary & Heart & Mrs. \\
e3 & Juan & Guerra & Mr. \\\hline
\end{tabular}
& &
\begin{tabular}[t]{|c|c|c|}\hline 
\rowcolor[gray]{.8} ProductKey & TimeKey & Quantity \\\hline
\hiderowcolors
p2 & t2 & 100 \\
p3 & t3 & 100 \\
p4 & t4 & 100 \\
p5 & t5 & 100 \\
p6 & t6 & 100 \\\hline
\end{tabular}
\end{tabular}
\end{center} 

\end{document}
1

I don't see your problem, it compiles fine here:

enter image description here

Output of \listfiles:

 *File List*
  report.cls    2005/09/16 v1.4f Standard LaTeX document class
  size10.clo    2005/09/16 v1.4f Standard LaTeX file (size option)
colortbl.sty    2001/02/13 v0.1j Color table columns (DPC)
   array.sty    2005/08/23 v2.4b Tabular extension package (FMi)
   color.sty    2005/11/14 v1.0j Standard LaTeX Color (DPC)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2007/01/08 v0.04d Graphics/color for pdfTeX
 ***********
1

If you use {NiceTabular} of nicematrix in the nested tabulars (with the key colortbl-like), you will have the expected output (nicematrix has its own system to color rows, cells and columns based on PGF/Tikz).

\documentclass{report}
\usepackage{colortbl}
\usepackage{nicematrix}

\begin{document}

\begin{center}
\small\sf
\begin{tabular}[t]{|c|c|c|c|}\hline
\rowcolor[gray]{.8} EmpID & FirstName & LastName & Title \\\hline
e1 & John & Smith & Mr. \\
e2 & Mary & Heart & Mrs. \\
e3 & Juan & Guerra & Mr. \\\hline
\end{tabular}
\hspace{3mm}
\begin{tabular}[t]{|c|c|c|}\hline 
\rowcolor[gray]{.8} ProductKey & TimeKey & Quantity \\\hline
p2 & t2 & 100 \\
p3 & t3 & 100 \\
p4 & t4 & 100 \\
p5 & t5 & 100 \\
p6 & t6 & 100 \\\hline
\end{tabular}
\end{center} 

\begin{center}
\small\sf
\begin{tabular}{ccc}
\begin{NiceTabular}[t]{|c|c|c|c|}[colortbl-like]\Hline
\rowcolor[gray]{.8} EmpID & FirstName & LastName & Title \\\Hline
e1 & John & Smith & Mr. \\
e2 & Mary & Heart & Mrs. \\
e3 & Juan & Guerra & Mr. \\\Hline
\end{NiceTabular}
& &
\begin{NiceTabular}[t]{|c|c|c|}[colortbl-like]\Hline 
\rowcolor[gray]{.8} ProductKey & TimeKey & Quantity \\\Hline
p2 & t2 & 100 \\
p3 & t3 & 100 \\
p4 & t4 & 100 \\
p5 & t5 & 100 \\
p6 & t6 & 100 \\\Hline
\end{NiceTabular}
\end{tabular}
\end{center} 

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz under the hood).

Output of the above code

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.