Latex 根据其在乳胶中的值对颜色进行编号

Latex 根据其在乳胶中的值对颜色进行编号,latex,tex,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 那么输出应该是: 但是自动地,我知道可以用tex编程,但我不知道从哪里开始。欢迎您提出任何建议您可以通过宏(使用)和基于值的条件(使用)传递每个表格单元格条目: \documentcla

我需要创建一个包含数字内容的表,并根据其值,要求它自动采用颜色。例如如果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
那么输出应该是:


但是自动地,我知道可以用tex编程,但我不知道从哪里开始。欢迎您提出任何建议

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

\documentclass{article}
\usepackage{collcell,xcolor,xfp}
\纽科曼{\fmtnum}[1]{%
\ifnum\fpeval{1<0}=1
\textcolor{red}{$#1$}%
\否则
\ifnum\fpeval{1<0.5}=1
\textcolor{green}{$#1$}%
\否则
\textcolor{blue}{$#1$}%
\fi
\fi
}
\开始{document}

\begin{tabular}{*{3}{>{\collectcell\fmtnum}cPS:我找到了这个示例,但我不知道如何处理我需要的范围
\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}
\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}