I know there exists pgf, and many people uses this to create heatmaps. I however am not allowed to use this package. I therefore want to create a command that enables me to color the different cells.
I found this question, that showed the following def
\def\cca#1{\cellcolor{black!#10}\ifnum #1>5\color{white}\fi{#1}}
But it does only work from 0-9 as per his comments and my tests.
I wanted to create something more generic like this pseudo code:
\newcommand{\cTab}[2]
{
\res = #1/#2 %maybe using FP (?)
\eighty = 0.8*#2 %maybe using FP (?)
\cellcolor{black!\res}
\ifnum #1>\eighty
\color{white}
\fi{#1}
}
So basicly the issue is to calculate a number and then reuse it to define color and other?
I can calculate the number using FP, but not reuse it within cellcolor
or ifnum
.
MWE
\documentclass[letterpaper, 10 pt]{article}
% Color
\usepackage{xcolor,colortbl}%
\begin{document}
\begin{table}[!ht]
\centering
\caption{True data classification.}
\label{tab:CollectedErrors}
\begin{tabular}{|p{1.5cm}|p{0.5cm}|}
\hline
\textbf{Exposure}& \textbf{Attribute} \\ \hline
4 & 2 \\ \hline
11 &144 \\ \hline
\end{tabular}
\end{table}
\end{document}
Basicly what I want is to create a heatmap, i.e. color cells based on the number within the cell in the table.
MWE using code from solution 1
\documentclass[letterpaper, 10 pt]{article}
% Color
\usepackage{xcolor,colortbl}%
\usepackage{xintexpr}
\newcommand{\cTab}[2]
{%
\edef\res {\xinttheiexpr [2] #1/#2\relax}% [2] = "two digits after ."
\edef\eighty {\xinttheiexpr [2] 0.8*#2\relax}%
\cellcolor{black!\res}%
\xintifboolexpr {#1>\eighty}
% yes branch
{\color{white}}
% no branch (nothing to do)
{}%
{#1}%
}%
\begin{document}
\begin{table}[!ht]
\centering
\caption{True data classification.}
\label{tab:CollectedErrors}
\begin{tabular}{|p{1.5cm}|p{0.5cm}|}
\hline
\textbf{Exposure}& \textbf{Attribute} \\ \hline
\cTab{4}{4} & 2 \\ \hline %example, could also be \cTab{4}{144}, in theory all cells should be changed to cTab
11 &144 \\ \hline
\end{tabular}
\end{table}
\end{document}
\ifnum
does only work for integer numbers,not for floating point – user31729 Feb 15 '16 at 11:53tabular
or not, but here are two questions that show other approaches: tex.stackexchange.com/questions/172578/… and tex.stackexchange.com/questions/157080/… – Steven B. Segletes Feb 15 '16 at 14:51