1

I have used the latex command presented in this TexStackExchange answer to create a table with cell background color based on the cell's numerical value.

The code works but it is changing the cell width. While the newly defined command uses \cellcolor, the same behavior does not occur when using \cellcolor directly.

Here is an example document which illustrates the problem:

\documentclass[table]{article}
\usepackage{hhline} % used for the top-most horizontal line! in order to fix the top left corner cell empty
\usepackage[table]{xcolor}
\usepackage{etoolbox}
\usepackage{pgf} % for calculating the values for gradient
%======================================
% Color set related!
\definecolor{high}{HTML}{0083b6}  % the color for the highest number in your data set
\definecolor{low}{HTML}{ffffff}  % the color for the lowest number in your data set
\newcommand*{\opacity}{70}% here you can change the opacity of the background color!
%======================================
% Data set related!
\newcommand*{\minval}{0.0}% define the minimum value on your data set
\newcommand*{\maxval}{1.0}% define the maximum value in your data set!
%======================================
% gradient function!
\newcommand{\gradient}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxval pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minval pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxval-\minval))-(\minval*(100/(\maxval-\minval)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{high!\tempa!low!\opacity} #1
    }}
}


\renewcommand{\opacity}{50}
 
\begin{document}
\begin{table}[ht]
    \centering
    \caption{Version A}
    \label{tab:mytab}
    \begin{tabular}{|*{8}{c|}}
        \hhline{~*{3}{-}}
        \multicolumn{1}{c|}{} & A & B & C \\ \hline
        1 & \gradient{0.20} & \gradient{0.00} & \cellcolor{red}{0.00}  \\ \hline
        6 & \gradient{0.80} & \gradient{1.00} & \cellcolor{red}{0.00}  \\ \hline
    \end{tabular}
\end{table}

\begin{table}[ht]
    \centering
    \caption{Version B}
    \label{tab:mytab}
    \begin{tabular}{|*{8}{c|}}
        \hhline{~*{3}{-}}
        \multicolumn{1}{c|}{} & A & B & C \\ \hline
        1 & \gradient{0.20} & \gradient{0.00} & \gradient{0.00}  \\ \hline
        6 & \gradient{0.80} & \gradient{1.00} & \gradient{0.00}  \\ \hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

Does anyone know how to keep the same cell width?

4
  • 1
    Welcome to TeX.SX! I would guess that this is due to the spaces which are added to the cell because of the uncommented line breaks in the defintion of \gradient. Make sure to end every line in the defintion with a % and/or remove empty lines from the code of the defintion. Commented May 8, 2023 at 13:55
  • You mean the two empty lines? They don't have an impact. I updated the example accordingly.
    – elya5
    Commented May 9, 2023 at 7:20
  • 1
    Jasper is correct you have sporadic spaces at the end of the lines in your definition of \gradient end each line with % to remove them. I added 7 (probably not all needed) to your example and the two tables have the same width (if you also remove the space before #1 on the \cellcolor line).
    – daleif
    Commented May 9, 2023 at 7:50
  • Ah, got it. Thanks a lot!
    – elya5
    Commented May 9, 2023 at 8:20

1 Answer 1

2

You need to make sure that the returning value of the \gradient command does not contain any spurious spaces. Line breaks and empty lines will result in such spaces. Also, you could do without the help of etoolbox since PGF already provides its own if-then syntax:

\documentclass{article}
\usepackage{hhline}
\usepackage[table]{xcolor}
\usepackage{pgf}

\definecolor{high}{HTML}{0083b6}  
\definecolor{low}{HTML}{ffffff} 
\newcommand*{\opacity}{70}
\newcommand*{\minval}{0.0}
\newcommand*{\maxval}{1.0}

\newcommand{\gradient}[1]{%
    \pgfmathparse{
        #1 > \maxval ? 100 : ( #1 < \minval ? 0 : 
            int( round(
                100 * ( #1 / ( \maxval - \minval) ) -
                ( \minval * ( 100 / ( \maxval - \minval ) ) )
            ) )
        )
    }%
    \xdef\colorval{\pgfmathresult}%
    \cellcolor{high!\colorval!low!\opacity}#1%
}

\renewcommand{\opacity}{50}
 
\begin{document}
\begin{table}[ht]
    \centering
    \caption{Version A}
    \label{tab:mytab}
    \begin{tabular}{|*{8}{c|}}
        \hhline{~*{3}{-}}
        \multicolumn{1}{c|}{} & A & B & C \\ \hline
        1 & \gradient{0.20} & \gradient{0.00} & \cellcolor{red}{0.00}  \\ \hline
        6 & \gradient{0.80} & \gradient{1.00} & \cellcolor{red}{0.00}  \\ \hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

You must log in to answer this question.

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