TransWikia.com

how can shading rows in table

TeX - LaTeX Asked on May 28, 2021

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}

defcolorModel{hsb} %You can use rgb or hsb

newcommandColCell[1]{
  pgfmathparse{#1<50?1:0}  %Threshold for changing the font color into the cells
    ifnumpgfmathresult=0relaxcolor{white}fi
  pgfmathsetmacrocompA{0}      %Component R or H
  pgfmathsetmacrocompB{#1/100} %Component G or S
  pgfmathsetmacrocompC{1}      %Component B or B
  edefx{noexpandcenteringnoexpandcellcolor[colorModel]{compA,compB,compC}}x #1
  } 
newcolumntype{E}{>{collectcellColCell}m{0.4cm}<{endcollectcell}}  %Cell width

begin{document}
newcommanditems{3}   %Number of classes
arrayrulecolor{white} %Table line colors
noindentbegin{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

I want a table like this image I attached

2 Answers

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).

Output of the above code

Answered by F. Pantigny on May 28, 2021

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.

enter image description here

% !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}

defcolorModel{hsb} %You can use rgb or hsb

newcommandColCell[1]{
  pgfmathparse{#1<50?1:0}  %Threshold for changing the font color into the cells
    ifnumpgfmathresult=0relaxcolor{white}fi
  pgfmathsetmacrocompA{0}      %Component R or H
  pgfmathsetmacrocompB{#1/100} %Component G or S
  pgfmathsetmacrocompC{1}      %Component B or B
  edefx{noexpandcenteringnoexpandcellcolor[colorModel]{compA,compB,compC}}x #1
  } 
newcolumntype{E}{>{collectcellColCell}m{0.4cm}<{endcollectcell}}  %Cell width

begin{document}
newcommanditems{3}   %Number of classes
arrayrulecolor{white} %Table line colors

noindentbegin{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.

b

Answered by Simon Dispa on May 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP