表头与列类型不同的表格的渐变颜色

表头与列类型不同的表格的渐变颜色

我采用了这个代码是否有一种简单的方法可以根据每个单元格中的值为表格着色?。它在 Overleaf 上运行,但有一个错误,但它产生了所需的输出。然而,由于错误,相同的代码无法在 Texmaker 5.0.4 上运行:

! Missing number, treated as zero.
<to be read again>
p
l.62 \end{tabularx}
A number should have been here; I inserted `0'.
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{collcell}
\usepackage{adjustbox}
\usepackage{tabularx}
\begin{document}

\newtoggle{inTableHeader}%
\toggletrue{inTableHeader}%
\newcommand*{\StartTableHeader}{\global\toggletrue{inTableHeader}}%
\newcommand*{\EndTableHeader}{\global\togglefalse{inTableHeader}}%

\newcommand*{\MinNumber}{10}%
\newcommand*{\MidNumber}{25}%
\newcommand*{\MaxNumber}{50}%
\newcommand{\ApplyGradient}[1]{%
  \iftoggle{inTableHeader}{#1}{
    \ifdim #1 pt > \MidNumber pt
        \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
        \hspace{-0.33em}\colorbox{green!\PercentColor!yellow}{#1}
    \else
        \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
        \hspace{-0.33em}\colorbox{white!\PercentColor!gray}{#1}
    
    \fi
  }}

\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{2mm} % box size
\setlength{\tabcolsep}{0pt}
\newcommand{\rot}[1]{\multicolumn{1}{c}{\adjustbox{angle=90,lap=\width-1em}{#1}}}

\begin{table}[ht]
\caption{Product groups interaction}
\centering
\footnotesize
\begin{tabularx}{\textwidth}{p{.18\textwidth}*{11}{R}}  \hline
&
\rot{ Animal} &
\rot{ Vegetables } &
\rot{ Food} &
\rot{ Minerals} &
\rot{ Footwear} &
\rot{ Fuel} &
\rot { Chemicals} &
\rot{ Plastic} &
\rot{ Leather} &
\rot{ Wood } &
\rot{ Textile} \EndTableHeader \\ \hline
Animal  &      & 28.2 & 13.5 & 9.91  & 10.3 & 16.5 & 9.01  & 15.0 & 11.3 & 9.03  & 9.02  \\
Vegetables   & 14.6 &     & 23.0 & 15.8 & 15.0 & 23.3 & 27.0 & 34.7 & 18.3 & 7.71  & 10.3 \\
Food        & 40.6 & 58.5 &    & 51.9 & 64.6 & 55.5 & 47.0 & 48.1 & 49.8 & 56.8 & 54.8 \\
Minerals      & 30.3 & 29.3 & 35.8 &    & 33.9 & 34.1 & 25.9 & 34.1 & 33.5 & 26.7 & 29.9 \\
Footwear   & 17.7 & 19.6 & 32.9 & 24.4 &    & 27.2 & 23.6 & 43.3 & 30.1 & 15.5 & 23.2 \\
Fuel   & 24.7 & 39.0 & 36.2 & 28.3 & 26.9 &     & 26.4 & 24.8 & 24.0 & 20.9 & 24.4 \\
Chemicals     & 11.5 & 12.7 & 33.3 & 16.1 & 11.7 & 20.0 &    & 23.7 & 16.6 & 6.72  & 10.2 \\
Plastic     & 25.3 & 24.6 & 19.7 & 24.6 & 22.7 & 27.6 & 17.3 &    & 28.6 & 19.9 & 20.3 \\
Leather   & 15.1 & 29.7 & 21.1 & 15.0 & 14.4 & 16.3 & 9.02  & 15.5 &     & 10.5 & 10.8 \\
Wood & 16.7 & 7.83  & 18.9 & 15.9 & 8.74  & 13.9 & 28.8 & 15.2 & 19.8 &    & 7.31  \\
Textile   & 20.0 & 31.3 & 33.4 & 22.1 & 20.0 & 31.6 & 32.4 & 32.7 & 28.1 & 14.3 &   \\ 
\hline
\end{tabularx}
\end{table}
\end{document}

答案1

该错误消息来自代码\ifdim #1 pt > \MidNumber pt。当#1(单元格内容)为空白时,它会扩展为\ifdim pt > ...在 之前缺少一个数字p

您的表格中有空白单元格,这就是导致问题的原因。

这是一个修补的定义,\ApplyGradient它在空白单元格中不执行任何操作:

\newcommand{\ApplyGradient}[1]{%
  \iftoggle{inTableHeader}{#1}{%
    \def\NumberArgument{#1}%
    \ifx\NumberArgument\empty\else
      \ifdim #1 pt > \MidNumber pt
          \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)}%
          \hspace{-0.33em}\colorbox{green!\PercentColor!yellow}{#1}%
      \else
          \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)}%
          \hspace{-0.33em}\colorbox{white!\PercentColor!gray}{#1}%
      \fi
    \fi
  }}

相关内容