# how can shading rows in table

I wanted to do shading in a table according to its rows. For example, if you see the table below:

a  b   c   d
a 90 10  0   0
b 0  80  10  10
c 0  0   95  5
d 0  10  5   85


I want to shade automatically each rows. Here is a code from (Parametrize shading in table through TikZ) and I want this theme

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}
\usepackage{multirow}

\def\colorModel{hsb} %You can use rgb or hsb

\newcommand\ColCell[1]{
\pgfmathparse{#1<50?1:0}  %Threshold for changing the font color into the cells
\ifnum\pgfmathresult=0\relax\color{white}\fi
\pgfmathsetmacro\compA{0}      %Component R or H
\pgfmathsetmacro\compB{#1/100} %Component G or S
\pgfmathsetmacro\compC{1}      %Component B or B
\edef\x{\noexpand\centering\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x #1
}
\newcolumntype{E}{>{\collectcell\ColCell}m{0.4cm}<{\endcollectcell}}  %Cell width

\begin{document}
\newcommand\items{3}   %Number of classes
\arrayrulecolor{white} %Table line colors
\noindent\begin{tabular}{c*{\items}{|E}|}
\multicolumn{1}{c}{} &
\multicolumn{1}{c}{A} &
\multicolumn{1}{c}{B} &
\multicolumn{1}{c}{C} \\ \hhline{~*\items{|-}|}
A  & 100   & 0  & 10   \\ \hhline{~*\items{|-}|}
B  & 10   & 80  & 10   \\ \hhline{~*\items{|-}|}
C  & 30   & 0   & 70   \\ \hhline{~*\items{|-}|}
\end{tabular}

\end{document}


I want a table like this image I attached

• Hello and welcome to TeX-SE. With nicematrix package, you can create a NiceTabular where you automatically alternate colors for your rows. You should take a look at it. Mar 6 at 20:39
• You don't need a sledge-hammer like TiKZ to do that. The xcolor package with option [table] does it in a very simple way. Mar 6 at 20:42
• Thanks for the tips. But I get this error: (Environment NiceTabular undefined. \begin{NiceTabular}). In the meantime, I want the background to be red
– Reza
Mar 6 at 22:02
• @Reza: I think that you have not the latest version of nicematrix. Mar 6 at 22:23
• Thanks F.Pantigny. may I aske you what should I do and how?
– Reza
Mar 7 at 9:09

Here is solution with {NiceTabular} of nicematrix.

\documentclass[english]{article}
\usepackage{nicematrix}

\begin{document}
\arrayrulecolor{white}
\newcommand{\white}{\color{white}}

\begin{NiceTabular}{c*{3}{|wc{1cm}}}%
[ cell-space-limits=3pt, code-before = \rowcolors{1}{blue!15}{blue!10} \rowcolor{blue!50}{1} ]
& \white A    & \white B  & \white C  \\
A  & 100         & 0         & 10        \\
B  & 10          & 80        & 10        \\
C  & 30          & 0         & 70        \\
\end{NiceTabular}

\end{document}


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

Interesting your approach to having a "template style" to apply to tables according the value of the cell.

This answer does not differ significantly from that of F. Pantigny. Maybe it's closer to the MWE style you posted.

% !TeX TS-program = pdflatex

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}
\usepackage{multirow}

\usepackage{nicematrix}

\def\colorModel{hsb} %You can use rgb or hsb

\newcommand\ColCell[1]{
\pgfmathparse{#1<50?1:0}  %Threshold for changing the font color into the cells
\ifnum\pgfmathresult=0\relax\color{white}\fi
\pgfmathsetmacro\compA{0}      %Component R or H
\pgfmathsetmacro\compB{#1/100} %Component G or S
\pgfmathsetmacro\compC{1}      %Component B or B
\edef\x{\noexpand\centering\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x #1
}
\newcolumntype{E}{>{\collectcell\ColCell}m{0.4cm}<{\endcollectcell}}  %Cell width

\begin{document}
\newcommand\items{3}   %Number of classes
\arrayrulecolor{white} %Table line colors

\noindent\begin{NiceTabular}{cccc}[code-before =  \rowcolors{2}{blue!25}{blue!15} \columncolor{white}{1}]
& A     & B   & C    \\ \hhline{~*\items{|-}|}
A   & 100   & 0   & 10   \\ \hhline{~*\items{|-}|}
B   & 10    & 80  & 10   \\ \hhline{~*\items{|-}|}
C   & 30    & 0   & 70   \\ \hhline{~*\items{|-}|}
\end{NiceTabular}

\end{document}


Use [code-before = \rowcolors{2}{red!25}{red!15} \columncolor{white}{1}] to change the background color to red.