2

I have a table for a machine learning ablation study and I would like to have cell colors that vary row-wise according to the values contained in the cell. The situation is similar to the following:

\documentclass{article}

%opening \title{} \author{}

\usepackage[utf8]{inputenc} % allow utf-8 input \usepackage[T1]{fontenc} % use 8-bit T1 fonts \usepackage{booktabs} % professional-quality tables \usepackage{nicefrac} % compact symbols for 1/2, etc. \usepackage{doi} \usepackage{amsmath} \usepackage{bm} \usepackage{multicol} \usepackage{multirow}

\begin{document}

\begin{table}[h] \setlength{\tabcolsep}{3.5pt} \renewcommand{\arraystretch}{1.6} \caption{Ablation study.} \label{table:ablation} \begin{center} \begin{tabular}{ l | p{0.8cm}| c c c c c c} \toprule \vspace{-6pt} % Adjust the value to reduce space, the higher the lower the spacing between the first two rows Dataset & & Model1 & Model2 & Model3 & Model4\ & & & & & \ \midrule Dataset1 & M1 & $134.49$ & $145.05_{\pm 13.83}$ & $129.59_{\pm 4.98}$ & $\bm{128.7.06}{\pm 11.01}$\ Dataset2 & M1 & $ 1.76{\pm 0.00}$ & $1.72_{\pm 0.05}$ & $1.69_{\pm 0.05}$ & $\bm{1.71_{\pm 0.01}}$\ \bottomrule \end{tabular} \end{center} \end{table*}

\end{document}

enter image description here

Basically I want to have a heatmap for table cells with color that varies reasonably according to cell values in each row, since the metric's (M1) scale is different according to each dataset in consideration. How can I achieve that?

  • You probably need to define a \newcommand, which accepts value, variation and a max-value, outputs color and those texts, e.g. using \cellcolor from package colortbl, and put it wherever you put data so far. – MS-SPO Nov 15 '23 at 21:53
  • Did you see https://tex.stackexchange.com/q/174998/47927 ? – Jasper Habicht Nov 15 '23 at 21:55
  • could you show me how to do that please? – James Arten Nov 15 '23 at 21:55
  • @JasperHabicht I have checked that, but there the color variation seems to be global for all the table. I would need a 'local' color variation for each row as the metric scale I'm taking into account is different. – James Arten Nov 15 '23 at 21:56
  • Unrelated, but you seem to combine the booktabs package with vertical rules which does not work well and should be avoided if possible. – Jasper Habicht Nov 15 '23 at 21:56
  • Hi! Can you make your example actually minimal, and compilable? Remove the small, sc, and other choices that are not relevant to your question (vskip, for instance), and insert your code into a proper document that can be compiled (it should start with \documentclass…). Thanks! – Clément Nov 15 '23 at 22:28
  • Hi @Clément and thanks for your comment! I have edited my question by making the example minimal and compilable as suggested. many thanks for your consideration – James Arten Nov 15 '23 at 22:35
  • Which cell would be the one that is used for coloring the row? As you seem to put additional stuff in the cells, not just plain numbers, it might be difficult to just grab the contents of such a cell and process it automatically. – Jasper Habicht Nov 16 '23 at 08:09
  • I mean, just the cells with numbers. I have additional things like \pm but the color cell should just influence the number cell like $145.05_{\pm 13.83}$ – James Arten Nov 16 '23 at 09:50

0 Answers0