# Automatic cell colouring using cellcolor: Undefined Control Sequence

I am trying to produce a table, using tabularx, where the cells are coloured automatically depending upon their value.

I want to use the \cellcolor command as it will fill the whole cell, rather than colorbox command which seems to only fill the area around the numbers, and one has to fiddle with \fboxsep to get a suitable fill.

Here is a MWE, with the Value 1 column coloured manually, the Value 2 column should look the same, but there is an error in passing the numerical value to \cellcolor, which says I have an Undefined Control Sequence

\documentclass[11pt]{book}% openany
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{mathtools}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\colorlet{LightSpringGreen}{white!70!lime}
\colorlet{LightRed}{white!70!red}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\cellcolor{LightSpringGreen!\PercentColor!LightRed}{#1}
}
\begin{document}
\begin{table}
\renewcommand{\MinNumber}{0}%
\renewcommand{\MaxNumber}{1}%
\begin{tabularx}{\textwidth}{l|l|H}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} \\
\hline
a & \cellcolor{LightSpringGreen!20!LightRed}0.2 & 0.2\\
b & \cellcolor{LightSpringGreen!50!LightRed}0.5 & 0.5\\
c & \cellcolor{LightSpringGreen!60!LightRed}0.6 & 0.6\\
d & \cellcolor{LightSpringGreen!80!LightRed}0.8 & 0.8\\
\end{tabularx}
\end{table}
\end{document}


The problem appears to be associated with the way the number is passed. In a test run, if I put \num{20} in the 2nd column, 2nd row then the same error comes up.

Can anyone tell me where I am going wrong?

-
cellcolor is defined in the colortbl package, which you didn't load. I suspect you loaded collcell for that, am I wrong? –  Bernard Aug 10 '14 at 15:57
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  yo' Aug 10 '14 at 16:10

Each table cell forms a group, so your definition of \PercentColor is lost by the time it reaches \ApplyGradient. If you make the definition \global (I've done so using \global\edef or, equivalently, \xdef) it works as expected:

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tabularx}
\usepackage{collcell}
\colorlet{LightSpringGreen}{white!70!lime}
\colorlet{LightRed}{white!70!red}
\newcommand{\MinNumber}{0}%
\newcommand{\MaxNumber}{0}%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\xdef\PercentColor{\PercentColor}%
\cellcolor{LightSpringGreen!\PercentColor!LightRed}{#1}
}
\begin{document}
\begin{table}
\renewcommand{\MinNumber}{0}%
\renewcommand{\MaxNumber}{1}%
\begin{tabularx}{\textwidth}{l|l|H}
\hline
Name & Value 1 & \multicolumn{1}{c}{Value 2} \\
\hline
a & \cellcolor{LightSpringGreen!20!LightRed}0.2 & 0.2\\
b & \cellcolor{LightSpringGreen!50!LightRed}0.5 & 0.5\\
c & \cellcolor{LightSpringGreen!60!LightRed}0.6 & 0.6\\
d & \cellcolor{LightSpringGreen!80!LightRed}0.8 & 0.8\\
\end{tabularx}
\end{table}
\end{document}

-