如果单元格包含特定符号,请使用 siunitx 格式化单元格

如果单元格包含特定符号,请使用 siunitx 格式化单元格

在我之前的问题大卫·卡莱尔当单元格包含 时,为单元格中的所有文本着色提供了很好的解决方案\*

我玩了一下这段代码,但如果siunitx使用该包则无法将其包含在内:

\documentclass{article}
\usepackage{color}
\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}

{\catcode`\*=\active
\gdef\zz#1{%
\mathcode`\*="8000
\gdef\foo{}%
\def*{\gdef\foo{\color{red}}}%
\setbox0\hbox\bgroup$}
}
\def\zzz{$\egroup\foo{\box0}}

\begin{document}
Not colored here: -12*, But colored within the tabular
\begin{tabular}{ 
>{\zz}l<{\zzz}
>{\zz}c<{\zzz}
>{\zz}r<{\zzz}
}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{tabular}
and again not colored here: $-12$.

But not here:
\begin{tabularx}{\linewidth}
    {
    X
    >{\zz}c<{\zzz}
    >{\zz}S[table-format=1.2,input-close-uncertainty=]<{\zzz}
    >{\zz}c<{\zzz}  
    }
Numbers & 1 & 2.00* & 3 \\
\end{tabularx}
\end{document}

您有什么想法吗?如何进行设置siunitx

\*此外,如果它们能够在着色过程中停留并且不会消失,那就太好了。

答案1

我假设 * 仅用于包含数字的单元格;然后在特殊环境中,任何连贯的减号、数字和句点后跟一个 * 都会以 * 作为前缀,\color{red}并且 * 会被删除。

\documentclass{article}
\usepackage{color}
\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}

\usepackage{xparse,environ}

\ExplSyntaxOn
\cs_new_protected:Nn \mil_colorcells:n
 {
  \regex_replace_all:nnN
   { ([-.0-9]*)\* } % any run of minus sign, digits or period
   { \c{color}\cB\{#1\cE\}\1 }
   \BODY
 }

\NewEnviron{startabular}[1]{%
  \mil_colorcells:n { red }
  \begin{tabular}{#1}
  \BODY
  \end{tabular}
}
\NewEnviron{stararray}[1]{%
  \mil_colorcells:n { red }
  \begin{array}{#1}
  \BODY
  \end{array}
}
\NewEnviron{startabularx}[2]{%
  \mil_colorcells:n { red }
  \begin{tabularx}{#1}{#2}
  \BODY
  \end{tabularx}
}
\ExplSyntaxOff

\begin{document}
Not colored here: -12*, But colored within the tabular

\begin{startabular}{lcr}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{startabular}\quad
$\begin{stararray}{lcr}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{stararray}$

and again not colored here: $-12$.


But not here:

\noindent
\begin{startabularx}{\linewidth}
 {
  X
  c
  S[table-format=1.2,input-close-uncertainty=]
  c
 }
Numbers & 1 & 2.00* & 3 \\
\end{startabularx}
\end{document}

在此处输入图片描述

如果你想保留星号,你可以这样做

\documentclass{article}
\usepackage{color}
\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}

\usepackage{xparse,environ}

\newcommand\milasterisk{\makebox[0pt][l]{$^*$}}

\ExplSyntaxOn
\cs_new_protected:Nn \mil_colorcells:n
 {
  \regex_replace_all:nnN
   { ([-.0-9]*)\* } % any run of minus sign, digits or period
   { \c{color}\cB\{#1\cE\}\1\c{milasterisk} }
   \BODY
 }

\NewEnviron{startabular}[1]{%
  \mil_colorcells:n { red }
  \begin{tabular}{#1}
  \BODY
  \end{tabular}
}
\NewEnviron{stararray}[1]{%
  \mil_colorcells:n { red }
  \begin{array}{#1}
  \BODY
  \end{array}
}
\NewEnviron{startabularx}[2]{%
  \mil_colorcells:n { red }
  \begin{tabularx}{#1}{#2}
  \BODY
  \end{tabularx}
}
\ExplSyntaxOff

\begin{document}
Not colored here: -12*, But colored within the tabular

\begin{startabular}{lcr}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{startabular}\quad
$\begin{stararray}{lcr}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{stararray}$

and again not colored here: $-12$.


But not here:

\noindent
\begin{startabularx}{\linewidth}
 {
  X
  c
  S[table-format=1.2,input-close-uncertainty=]
  c
 }
Numbers & 1 & 2.00* & 3 \\
\end{startabularx}
\end{document}

在此处输入图片描述

随着xparse2019-05-03 的发布,具有类似于的内置功能environ,代码变为

\documentclass{article}
\usepackage{color}
\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}

\usepackage{xparse}

\newcommand\milasterisk{\makebox[0pt][l]{$^*$}}

\ExplSyntaxOn

\tl_new:N \tl_mil_colorcells_tl

\cs_new_protected:Nn \mil_colorcells:nn
 {
  \tl_set:Nn \tl_mil_colorcells_tl { #2 }
  \regex_replace_all:nnN
   { ([-.0-9]*)\* } % any run of minus sign, digits or period
   { \c{color}\cB\{#1\cE\}\1\c{milasterisk} }
   \tl_mil_colorcells_tl
 }

\NewDocumentEnvironment{startabular}{m +b}
 {
  \mil_colorcells:nn { red } { #2 }
  \begin{tabular}{#1}
  \tl_mil_colorcells_tl
  \end{tabular}
 }{}
\NewDocumentEnvironment{stararray}{m +b}
 {
  \mil_colorcells:nn { red } { #2 }
  \begin{array}{#1}
  \tl_mil_colorcells_tl
  \end{array}
 }{}
\NewDocumentEnvironment{startabularx}{mm +b}
 {
  \mil_colorcells:nn { red } { #3 }
  \begin{tabularx}{#1}{#2}
  \tl_mil_colorcells_tl
  \end{tabularx}
 }{}
\ExplSyntaxOff

\begin{document}
Not colored here: -12*, But colored within the tabular

\begin{startabular}{lcr}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{startabular}\quad
$\begin{stararray}{lcr}
  1 & 2 & 3 \\
  14* & 5 & 6 \\
  7 & 8* & -9* \\
\end{stararray}$

and again not colored here: $-12$.


But not here:

\noindent
\begin{startabularx}{\linewidth}
 {
  X
  c
  S[table-format=1.2,input-close-uncertainty=]
  c
 }
Numbers & 1 & 2.00* & 3 \\
\end{startabularx}
\end{document}

相关内容