Are there an easy way to coloring tables depending on the value in each cell?

Adapted from the link that I mentioned in my comments to the question (ie click HERE), here is a solution which produces the following:

enter image description here

A subtle difference between this solution, and the base code taken from the above link, is that this solution introduces max, min and MIDPOINT values, so that one gradient is achieved for the lower half (yellow -> red), and another for the upper half (yellow -> green) of the numbers.

I have also added a max / min limit to the calculated values, so that they are in the range 0 to 100, if they are outside this value, errors are thrown.

Here is the code to do it:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{collcell}

 %The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.5} %
\newcommand*{\MaxNumber}{1.0}%

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
        \ifdim #1 pt > \MidNumber pt
            \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
            \hspace{-0.33em}\colorbox{green!\PercentColor!yellow}{#1}
        \else
            \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
            \hspace{-0.33em}\colorbox{red!\PercentColor!yellow}{#1}
        \fi
}

\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

\begin{document}
    \begin{table}[ht]
        \begin{center}
            \begin{tabular}{*{10}{R}}
              1.00 & 1.00 & 1.00 & 1.00 & 0.99 & 0.98 & 0.96 & 0.90 & 0.82 & 0.37 \\
              1.00 & 1.00 & 0.99 & 0.98 & 0.95 & 0.90 & 0.82 & 0.61 & 0.37 & 0.01 \\
              1.00 & 0.99 & 0.98 & 0.96 & 0.90 & 0.82 & 0.67 & 0.37 & 0.14 & 0.00 \\
              1.00 & 0.98 & 0.95 & 0.90 & 0.78 & 0.61 & 0.37 & 0.08 & 0.01 & 0.00 \\
              0.99 & 0.95 & 0.90 & 0.82 & 0.61 & 0.37 & 0.14 & 0.01 & 0.00 & 0.00 \\
              0.98 & 0.90 & 0.82 & 0.67 & 0.37 & 0.14 & 0.02 & 0.00 & 0.00 & 0.00 \\
              0.95 & 0.78 & 0.61 & 0.37 & 0.08 & 0.01 & 0.00 & 0.00 & 0.00 & 0.00 \\
              0.90 & 0.61 & 0.37 & 0.14 & 0.01 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
              0.82 & 0.37 & 0.14 & 0.02 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
              0.37 & 0.01 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
            \end{tabular}
        \end{center}
    \end{table}
\end{document}

For the record, I find it totally frustrating producing things like the following, where NOT ALL of the cells are subject to the shading, and, there are header rows etc. I am sure there is a more glamorous way to do this, and I am interested in finding out how...

enter image description here


enter image description here

The linked questions give answers if you are using pgfplotstable/tikz but they are not needed if you only want colouring and not the other features of those packages. The following aligns the numbers on the comma and gives one of three background colours depending on the value.

\documentclass{article}

\usepackage{colortbl,dcolumn}

\def\zz#1{%
\ifdim#1pt<5pt\cellcolor{green}\else
\ifdim#1pt<50pt\cellcolor{yellow}\else
\cellcolor{red}\fi\fi
#1}


\begin{document}



\begin{tabular}{*3{D,,{2.2}}c}
\zz {1,2} &\zz  {3,04}  &\zz {5,44}  \\
\zz {1,01}&\zz {77,5}  &\zz {77,94} \\
\zz {3,42}&\zz   {4,04} &\zz {51,04} 
\end{tabular}

\end{document}

The solution by Nicholas Hamilton does not work if you want to have non-numeric header in the table. I have found the following workaround (https://tex.stackexchange.com/a/40603):

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{collcell}

% https://tex.stackexchange.com/a/40603 - only color in table body
\usepackage{etoolbox}

\newtoggle{inTableHeader}% Track if still in header of table
\toggletrue{inTableHeader}% Set initial value
\newcommand*{\StartTableHeader}{\global\toggletrue{inTableHeader}}%
\newcommand*{\EndTableHeader}{\global\togglefalse{inTableHeader}}%

% Redefine tabular to initialize \StartTableHeader at start and end
\let\OldTabular\tabular%
\let\OldEndTabular\endtabular%
\renewenvironment{tabular}{\StartTableHeader\OldTabular}{\OldEndTabular\StartTableHeader}%

 %The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.5} %
\newcommand*{\MaxNumber}{1.0}%

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
  \iftoggle{inTableHeader}{#1}{
    \ifdim #1 pt > \MidNumber pt
        \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
        \hspace{-0.33em}\colorbox{green!\PercentColor!yellow}{#1}
    \else
        \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
        \hspace{-0.33em}\colorbox{red!\PercentColor!yellow}{#1}
    \fi
  }}

\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

\begin{document}
    \begin{table}[ht]
        \begin{center}
            \begin{tabular}{*{10}{R}}
              A    & B    & C    & D    & E    & F    & G    & H    & I    & J    \EndTableHeader\\
              \hline
              1.00 & 1.00 & 1.00 & 1.00 & 0.99 & 0.98 & 0.96 & 0.90 & 0.82 & 0.37 \\
              1.00 & 1.00 & 0.99 & 0.98 & 0.95 & 0.90 & 0.82 & 0.61 & 0.37 & 0.01 \\
              1.00 & 0.99 & 0.98 & 0.96 & 0.90 & 0.82 & 0.67 & 0.37 & 0.14 & 0.00 \\
              1.00 & 0.98 & 0.95 & 0.90 & 0.78 & 0.61 & 0.37 & 0.08 & 0.01 & 0.00 \\
              0.99 & 0.95 & 0.90 & 0.82 & 0.61 & 0.37 & 0.14 & 0.01 & 0.00 & 0.00 \\
              0.98 & 0.90 & 0.82 & 0.67 & 0.37 & 0.14 & 0.02 & 0.00 & 0.00 & 0.00 \\
              0.95 & 0.78 & 0.61 & 0.37 & 0.08 & 0.01 & 0.00 & 0.00 & 0.00 & 0.00 \\
              0.90 & 0.61 & 0.37 & 0.14 & 0.01 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
              0.82 & 0.37 & 0.14 & 0.02 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
              0.37 & 0.01 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
            \end{tabular}
        \end{center}
    \end{table}
\end{document}

This gives:

The resulting table

Tags:

Color

Tables