在新命令中使用 cellcolor 来更改单元格宽度

在新命令中使用 cellcolor 来更改单元格宽度

我已经使用了提供的 latex 命令在此 TexStackExchange 答案中创建一个表格,其单元格背景颜色基于单元格的数值。

代码可以工作,但是会改变单元格宽度。虽然新定义的命令使用\cellcolor,但直接使用时不会发生相同的行为\cellcolor

以下是一个说明该问题的示例文档:

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

在此处输入图片描述

有谁知道如何保持相同的单元格宽度?

答案1

您需要确保\gradient命令的返回值不包含任何虚假空格。换行符和空行将导致此类空格。此外,您可以不用帮助,etoolbox因为 PGF 已经提供了自己的 if-then 语法:

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

在此处输入图片描述

相关内容