# Drawing tables with colors

I’m trying to create a table in my tex document that resembles more or less the table in the picture below:

I found this chunk of code but still, I don’t know how to separate each table as it appears in the picture.

 \documentclass{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{graphicx}% http://ctan.org/pkg/graphicx

\colorlet{tablerowcolor}{gray!10} % Table row separator colour = 10% gray
\newcommand{\rowcol}{\rowcolor{tablerowcolor}} %
% Command \topline consists of a (slightly modified) \toprule followed by a \heavyrule rule of colour tableheadcolor (hence, 2 separate rules)
\newcommand{\topline}{\arrayrulecolor{black}\specialrule{0.1em}{\abovetopsep}{0pt}%
\arrayrulecolor{black}}
% Command \midline consists of 3 rules (top colour tableheadcolor, middle colour black, bottom colour white)
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
\arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
\arrayrulecolor{black}}
% Command \rowmidlinecw consists of 3 rules (top colour tablerowcolor, middle colour black, bottom colour white)
\newcommand{\rowmidlinecw}{\arrayrulecolor{tablerowcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
\arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
\arrayrulecolor{black}}
% Command \rowmidlinewc consists of 3 rules (top colour white, middle colour black, bottom colour tablerowcolor)
\newcommand{\rowmidlinewc}{\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
\arrayrulecolor{tablerowcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
\arrayrulecolor{black}}
% Command \rowmidlinew consists of 1 white rule
\newcommand{\rowmidlinew}{\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{black}}
% Command \rowmidlinec consists of 1 tablerowcolor rule
\newcommand{\rowmidlinec}{\arrayrulecolor{tablerowcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{black}}
% Command \bottomline consists of 2 rules (top colour
\newcommand{\bottomline}{\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{\belowbottomsep}}%
\newcommand{\bottomlinec}{\arrayrulecolor{tablerowcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{\belowbottomsep}}%

\begin{document}

\renewcommand{\arraystretch}{1.5}
\begin{tabular}{llll}
\topline
\headcol Approach & Estimator & ME & MAE \\
\midline
& $\widehat{JV}_{\text{na\"ive}}$ & 4.37e-07 & 4.37e-07 \\
\rowcol \smash{\raisebox{1em}{Na\"ive}} & $\widetilde{JV}_{\text{na\"ive}}$ & 3.88e-07 & 3.88e-07 \\
& $\widehat{JV}$ & 1.33e-06 & 1.33e-06 \\
\rowcol \smash{\raisebox{1em}{Regular}} & $\widetilde{JV}$ & 1.20e-06 & 1.20e-06 \\
\bottomlinec
\end{tabular}

\end{document}


\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\usepackage{graphicx}
\newcommand\SR{\specialrule{1pt}{1pt}{1pt}}

\begin{document}

\begingroup
\arrayrulecolor{white}\arrayrulewidth=1pt
\begin{tabular}[t]{c !{\color{white}\vrule width3pt} c !{\color{white}\vrule width3pt} c }
\rowcolor{black}\textcolor{white}{Movies} &\textcolor{white}{Ratings}\\\SR
\rowcolor{gray!25}ID & Movie ID\\\SR
\rowcolor{gray!10}Title (Year) & User ID\\\SR
\rowcolor{gray!25}Genre List & Rating \\\SR
& \cellcolor{gray!10}Timestamp
\end{tabular}
\endgroup

\end{document}


• The row sep of each "normal" row (not the top row) is smaller. – user156344 May 20 '19 at 7:15
• When I load \arrayrulecolor{white}\arrayrulewidth=1pt in the preamble, all the lines from the other tables disappear, or turn into white and you can't see them! How can I fix it? – NaveganTeX May 20 '19 at 7:59
• @naveganteX, you need change rules inside of group.it can be define for example { before definitions of commands and } after table. – Zarko May 20 '19 at 10:22
• see edited answer – user187802 May 20 '19 at 10:36

This might be an unpopular opinion, but the best way to use alternating colours in tables is to not use them. The trend to have alternating row colours goes back to word who used it at one point as default formatting. However this does not mean that this is good practise. Due to the changing background colour, the contrast between the font and the background gets reduces which actually makes it harder to read. I suggest to use a clean and professional looking design instead:

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{tabular}{@{}lll@{}}
\toprule
Movies & Ratings & Tags\\
\midrule
ID & Movie ID & User ID\\
Title (Year) & User ID & Movie ID\\
Genre List & Rating & Tag\\
& Timestamp & Timestamp\\
\bottomrule
\end{tabular}

\end{document}