# Change the text color in table cells based on the value criterion

I have a confusion matrix ranges from 0.0 to 1.0. In which the value beyond 0.70 is bad. Therefore, I am coloring them as red. I am following the example presented here to color my confusion table. Now the only problem I have is that when it gets completely red which is close to the 1.0 values, the text color which is black is not much readable.

My code is as follows. What change can I make in this code, so that if the color reaches a certain point, then it changes the text color? Being new to latex, I am not very well versed with its command.

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

%\ifnum\pgfmathresult=0\relax\color{white}\fi
\ifdim #1 pt > \MidNumber pt
\pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
\hspace{-0.33em}\colorbox{red!\PercentColor!red}{#1}
\else
\pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
\hspace{-0.33em}\colorbox{white!\PercentColor!red}{#1}
\fi
}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

%% for rotation in table
\usepackage{array}

\newcolumntype{R}[2]{%
l%
<{\egroup}%
}
\newcommand*\rotz{\multicolumn{1}{R{0}{-1em}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{table}[t]
\caption{The confusion matrix}
\label{tab:CM}
\centering
\resizebox{1\textwidth}{!}{ % to nicely resize the table in the text width

\begin{tabular}{r*{4}{G}}
\rotz{} &
\rotz{A} &
\rotz{B} &
\rotz{C} & \smallskip \\
A        & 0.54  & 0     & 0.08     \\

B        & 0     & 0.96  & 0       \\

C        & 0.04  & 0.04  & 0.38   \\

\end{tabular}%\par\bigskip
}
\end{table}


As in the image. The black font color of extreme values decoded as red is not much visible if I decrease the size of the table. I would like to change the color of these extreme values to white for better visibility.

• Please could you give us a MWE (tex.meta.stackexchange.com/questions/228/…)
– flav
Oct 2 '19 at 7:19
• Just replace \hspace{-0.33em}\colorbox{red!\PercentColor!red}{#1} by \hspace{-0.33em}\colorbox{red!\PercentColor!red}{\textcolor{white}{#1}}. Given that you do not want a gradient, you could just drop \pgfmathsetmacro... in this case and use \hspace{-0.33em}\colorbox{red}{\textcolor{white}{#1}}.
– user194703
Oct 2 '19 at 13:47
• Thanks, it solved the problem. can you please put it to the answer so that I can accept it. Oct 2 '19 at 15:49

You have several spurious spaces in your code, which lead to try and cancel them with \hspace{-0.33em}. Better removing them.

The calculations can be simplified with the help of xfp which is better than PGF in floating point numbers and provides expandable operations.

\documentclass{article}
\usepackage{xcolor}
\usepackage{collcell}

% xfp is better than PGF in floating point numbers
\usepackage{xfp}

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

\ExplSyntaxOn
\NewExpandableDocumentCommand{\fpcompareTF}{mmm}
{
\fp_compare:nTF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff

\newcommand{\calcpercent}[2]{%
\fpeval{%
max(min(100*(#1-\MidNumber)/(#2-\MidNumber),100),0)%
}%
}

\fpcompareTF{#1 > \MidNumber}
{%
\fpcompareTF{#1 > 0.90}{\color{white}}{}%
\colorbox{red!\calcpercent{#1}{\MaxNumber}!red}{#1}%
}%
{%
\colorbox{white!\calcpercent{#1}{\MinNumber}!red}{#1}%
}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{table}[t]
\centering
\setlength{\tabcolsep}{0pt}
\renewcommand{\arraystretch}{0}

\caption{The confusion matrix}
\label{tab:CM}

\medskip

\begin{tabular}{r@{\,}*{3}{G}}
& \multicolumn{1}{@{}c@{}}{A}
& \multicolumn{1}{c}{B}
& \multicolumn{1}{c}{C} \\[\smallskipamount]
A & 0.54  & 0     & 0.08 \\
B & 0     & 0.96  & 0    \\
C & 0.04  & 0.04  & 0.38 \\
\end{tabular}

\end{table}

\end{document}


Maybe you have to adjust the formulas for computing the percentage.