5

I have the following minimum working example:

\documentclass{article}

\begin{document}

\begin{table}[!ht]
\centering
\begin{tabular}{|c|c|}
\hline a & b & c \\
\hline  
0.1 & 9  \\
\hline
0.5 & 4 \\
\hline
0.9 & 12 \\
\hline
\end{tabular}
\end{table}
\end{document}

Now I want cells to be colored depending on the size of the value; I found examples for this, but I want it differently for every column;

Let's say for column a, I have the values 0.1, 0.5 and 0.9 - assume larger values are better - then I'd like the cell of 0.1 to be colored like a soft red, 0.5 a soft orange and 0.9 to be a soft green;

On the other side for column b, it might be the case that smaller values are better, so 12 should be a soft red, while 4 should be a soft green;

How could I do that?


After a very useful remark from Werner, I need to add one additional information: The specific range of the numbers for each column is of course manual, for example, I know that in column a, values are in the interval [0,1] and larger values are better and in column b, values are let's say in the interval [0,20] and small values are better;

My goal is that from that information that I feed - latex automatically colors the cells individually

5
  • 2
    Is the scale/range deducible from the table itself? Probably not. So how would you specify that 12 is bad and 4 is good, while 0.9 is good` and 0.1 is bad in the respective columns? Or are you just interested in a manual way to achieve this (since that seems logical for the table size and the wide range of possibilities to colour)?
    – Werner
    Mar 16, 2021 at 17:46
  • The range can be entered manually; However, I do not want to color every cell infividually, as I have only a few columns but many many rows; So basically, I say: Column 'a' has values between 0 and 1 and column 'b' has values between 0 and 20 and for column 'a', large values are better and for column 'b' small values are better and then latex automatically adds the right color to the right cell
    – Johannes
    Mar 16, 2021 at 17:56
  • I don't see how to do this very easily. Maybe by creating a command that tests the value. In that perspective, I would use NiceTabular environment from the nicematrix package. If you can achieve this, I would be interested in the implementation.
    – SebGlav
    Mar 16, 2021 at 18:03
  • @SebGlav Thanks for your answer, I'll check that out - but just to understand you: Even with NiceTabular, you think it is going to be extremely difficult?
    – Johannes
    Mar 16, 2021 at 18:05
  • @Johannes I just say that right now, I don't really know what to start with but I could give it a try when I get some time. Hopefully, one of the mad TeXers here would come and help you out ;) The only thing that makes me think nicematrix could be a good idea is that it's TikZ based and so is maybe more versatile.
    – SebGlav
    Mar 16, 2021 at 18:16

1 Answer 1

7

Here is a solution with collcell. I have used nicematrix because you use colored cells and rules: with the tools of nicematrix, you will have a better result in the PDF viewers (the rules won't seem to disappear in some PDF viewers at some levels of zoom).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{collcell}
\begin{document}


\ExplSyntaxOn
\newcolumntype { A } { > { \collectcell \MyColumnA } c < { \endcollectcell } } 

\NewDocumentCommand \MyColumnA { m } 
  {
    \int_compare:nT { \int_use:c { c@iRow } > 1 }
      {
        \fp_set:Nn \l_tmpa_fp { #1 * 100 }
        \exp_args:Nx \cellcolor {red \exp_not:N ! \fp_use:N \l_tmpa_fp }
      }
    #1
  }

\newcolumntype { B } { > { \collectcell \MyColumnB } c < { \endcollectcell } } 

\NewDocumentCommand \MyColumnB { m } 
  {
    \int_compare:nT { \int_use:c { c@iRow } > 1 }
      {
        \fp_set:Nn \l_tmpa_fp { 100 - ( #1 * 100 ) }
        \exp_args:Nx \cellcolor {blue \exp_not:N ! \fp_use:N \l_tmpa_fp }
      }
    #1
  }
\ExplSyntaxOff



\begin{table}
\centering
\begin{NiceTabular}{ABA}[hvlines,colortbl-like]
X & Y & Z \\
0.3 & 0.35 & 0.5 \\
0.6 & 0.7 & 0.1 \\
0 & 0.3 & 0.35 \\
0.1 & 0.5 & 0.15 
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

5
  • Thank you so much, this is exactly what I wanted - but I cannot seem to make it work (I copied it into overleaf and compiled multiple times) - am I missing something? Does it compile normally for you?
    – Johannes
    Mar 16, 2021 at 22:59
  • 1
    I think your TeX installation is not up-to-date. Mar 17, 2021 at 6:34
  • I am using the TeX Live version 2020 on overleaf but apparently this is not enough - I'll try it offlinge then; Thank you very much
    – Johannes
    Mar 17, 2021 at 8:54
  • 1
    Since you use Overleaf, you can upload in the repertory of your Overleaf project the latest version of the file nicematrix.sty that you can find on the SVN server of TeXLive: www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/nicematrix.sty Mar 17, 2021 at 9:31
  • Oh it worked, thank you so much..!!!
    – Johannes
    Mar 17, 2021 at 9:49

You must log in to answer this question.

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