4

I want to modify this code to change the cell color instead of font color automatically according to the number in the cell.

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgf}
\usepackage{collcell}
\usepackage{adjustbox}
\usepackage{multirow} 
\usepackage{pdflscape} 
\usepackage{booktabs}
\usepackage{colortbl}

% This selects the amount of color to use
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{100}%

\newcommand{\ApplyGradient}[1]{
    \pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
    \textcolor{black!\PercentColor}{#1}
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}

\begin{document}

\begin{table}{
\begin{tabular}{ c R R R R R R R R}
\toprule
\multicolumn{1}{c} {Tools} &
\multicolumn{1}{c} {D1} &
\multicolumn{1}{c} {D2} &
\multicolumn{1}{c} {D3} &
\multicolumn{1}{c} {D4} &
\multicolumn{1}{c} {D5} &
\multicolumn{1}{c} {D6} &
\multicolumn{1}{c} {D7} &
\multicolumn{1}{c} {D8} \\
A &   70 &    80.81 &   54.5 & 100 & 51 & 88&  99 & 90\\
B &   66 &     99 &   73 & 88 & 66 &  59&  88 & 60\\
\toprule
\end{tabular}
}{}
\end{table}
\end{document}
2
  • Very much related: Partially coloring cell background with histograms
    – Werner
    Commented Dec 6, 2015 at 19:55
  • the different is in that case, it has only two different color and you specify it in code, I want something which changes automatically with value the amount of emphasizes in the background color Commented Dec 6, 2015 at 19:59

1 Answer 1

6

\cellcolor can be used to change the background colour of the cell:

enter image description here

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgf}
\usepackage{collcell}
\usepackage{booktabs}

% This selects the amount of color to use
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{100}%

\newcommand{\ApplyGradient}[1]{%
  \pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}%
  %\textcolor{black!\PercentColor}{#1}
  \edef\x{\noexpand\cellcolor{black!\PercentColor}}\x\textcolor{white}{#1}%
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}

\begin{document}

\begin{tabular}{ c R R R R R R R R}
  \toprule
  \multicolumn{1}{c} {Tools} &
  \multicolumn{1}{c} {D1} &
  \multicolumn{1}{c} {D2} &
  \multicolumn{1}{c} {D3} &
  \multicolumn{1}{c} {D4} &
  \multicolumn{1}{c} {D5} &
  \multicolumn{1}{c} {D6} &
  \multicolumn{1}{c} {D7} &
  \multicolumn{1}{c} {D8} \\
  A &   70 &    80.81 &   54.5 & 100 & 51 & 88&  99 & 90\\
  B &   66 &     99 &   73 & 88 & 66 &  59&  88 & 60\\
  \toprule
\end{tabular}

\end{document}

In order to allow the \PercentColor to be "visible" to \cellcolor, we expand the expression first, and then call \cellcolor using an \edef\x{..}\x trick. This is local to the cell.

The use of \toprule at the end seems bizarre, but it allows the bottom row's cell colour to stretch up to the horizontal rule.

2
  • Thanks a lot, you solved my problem simply by modifying a line, its amazing. Do you have any suggestion about the color to be more visible and printable in paper? I mean something like gray not completely dark like this. Commented Dec 6, 2015 at 20:27
  • @user2373198: You could manipulate the value of \PercentColor to vary the range. That is, instead of 0-100, you could make it 0-75 using \pgfmathsetmacro{\PercentColor}{75*...}.
    – Werner
    Commented Dec 6, 2015 at 20:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .