2

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}
4

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

  • 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. – user2373198 Dec 6 '15 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 Dec 6 '15 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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