2

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

You could try

\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}%
}%

But a mwe would help. Ok, mwe showed I needed \xdef\res but I am quite in the dark about what is aimed at. Update to explain I better understand now... (I was confused about xcolor color specification with a ! as I was led to believe it needed a number between 0 and 1, whereas a percentage between 0 and 100 is asked for).

Updated to avoid defining macros \res and \eighty (especially \res was annoying as it needed a global scope; but we can use expandability of \xinttheiexpr here).

\documentclass[letterpaper, 10 pt]{article}

% Color
\usepackage{xcolor,colortbl}

% Fine stuff
\usepackage{xintexpr, xinttools}

\newcommand{\cTab}[2]% #1 = cell, #2 = max
{%
    \cellcolor{black!\xinttheiexpr 100*#1/#2\relax}%
    \xintifboolexpr {#1>0.8*#2}%
    % "yes" branch 
       {\textcolor{blue}{#1}}% when #1 is big, print it blue
    % "no" branch
       {#1}% 
}%

\begin{document}
\begin{table}[!ht]
\centering
\caption{True data classification.}
\label{tab:CollectedErrors}
\smallskip
\begin{tabular}{|p{1.5cm}|c|}
\hline
\textbf{Exposure}& \textbf{Attribute} \\ \hline
\xintFor* #1 in {\xintSeq[3]{1}{100}}\do
{%
  \cTab{#1}{100} & \cTab{\the\numexpr100-#1\relax}{100}\\
}
\hline 
\end{tabular}
\end{table}
\end{document}

Notice also the use of \textcolor.

Blockquote

  • 1
    a minimal working example means a small document starting with \documentclass and ending with \end{document} which one can copy paste and check. I have no idea where your command \cTab is used ... – user4686 Feb 15 '16 at 14:28
  • 1
    your mwe does not show \cTab and where it is supposed to be use. Can you post the code which leads to the error you mention ? – user4686 Feb 15 '16 at 14:59
  • 1
    @JTIM yes, I understand now. Will try to update with a better example. If you needed the tabular to determine itself the maximum number inside the cells, that would make for another interesting question ... ;-) – user4686 Feb 15 '16 at 15:48
  • 1
    Please look at simplified \cTab macro. No need to define \res and \eighty anymore. – user4686 Feb 15 '16 at 16:16
  • 1
    side note: please note the use of \textcolor. A \color{blue} as in your original code adds some unwanted vertical space due to rather deep TeXnical things. – user4686 Feb 16 '16 at 11:04

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.