Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
    \cellcolor{LightSpringGreen!\PercentColor!LightRed}{#1}
}
\newcolumntype{H}{>{\collectcell\ApplyGradient}X<{\endcollectcell}}
\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?

share|improve this question
1  
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 at 15:57
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  tohecz Aug 10 at 16:10

1 Answer 1

up vote 4 down vote accepted

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:

enter image description here

\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}%
\newcommand{\ApplyGradient}[1]{%
  \pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
  \xdef\PercentColor{\PercentColor}%
  \cellcolor{LightSpringGreen!\PercentColor!LightRed}{#1}
}
\newcolumntype{H}{>{\collectcell\ApplyGradient}X<{\endcollectcell}}
\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.