根据其在LaTeX中的值对颜色进行编号

时间:2018-08-31 22:31:16

标签: latex tex

我需要创建一个包含数字内容的表格,并根据其值自动设置颜色。例如。如果X取以下值:

x < 0; then x will be red
0 <= x < 0.5; then x will be green
0.5 <= x <= 1; then x will be blue

即:如果我使用以下内容创建表

\documentclass{article}
\begin{document}
\begin{table}[]
\begin{tabular}{ccc}
\hline\\
\textbf{a} & \textbf{b} & \textbf{c} \\
\hline\\
-1   & 0   & 1    \\
0.3  & 0.5 & -1   \\
-0.2 & 0.7   & -0.5 \\
\hline\\
\end{tabular}
\end{table}
\end{document}

然后输出应为:

enter image description here

但是自动地,我知道可以通过使用tex编程来完成,但是我不知道从哪里开始。请,任何建议都欢迎

1 个答案:

答案 0 :(得分:2)

您可以通过宏(使用collcell)和基于值的条件(使用xfp)传递每个表单元格条目:

enter image description here

SOFTWARE\Microsoft: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
SOFTWARE\Microsoft: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
SOFTWARE\Microsoft\Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
SOFTWARE\Microsoft\Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
SOFTWARE\Microsoft\Windows\CurrentVersion: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
SOFTWARE\Microsoft\Windows\CurrentVersion: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages: 
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages: 

\documentclass{article} \usepackage{collcell,xcolor,xfp} \newcommand{\fmtnum}[1]{% \ifnum\fpeval{#1 < 0} = 1 \textcolor{red}{$#1$}% \else \ifnum\fpeval{#1 < 0.5} = 1 \textcolor{green}{$#1$}% \else \textcolor{blue}{$#1$}% \fi \fi } \begin{document} \begin{tabular}{ *{3}{>{\collectcell\fmtnum}c<{\endcollectcell}} } \hline \multicolumn{1}{c}{\bfseries a} & \multicolumn{1}{c}{\bfseries b} & \multicolumn{1}{c}{\bfseries c} \\ \hline -1 & 0 & 1 \\ 0.3 & 0.5 & -1 \\ -0.2 & 0.7 & -0.5 \\ \hline \end{tabular} \end{document} 中设置标题也避免了\multicolumn对它们的处理。