在以下 egreg 的 MWE 中(如果单元格包含特定符号,请使用 siunitx 格式化单元格*
), tabular、tabularx 或 array 环境中所有以 a 结尾的数字都会被重新格式化(颜色)。
但是,我对这段代码处理多个星号的方式不满意,例如,**
或被***
替换为一个*
。我尝试更改代码很长时间,以保留任意数量的星号,但没有成功。这能做到吗?
\documentclass{article}
\usepackage{color}
\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage{xparse,environ,l3regex}
\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}
答案1
您可以扩展正则表达式和替换以使用星号作为参数\milasterisk
:
\documentclass{article}
\usepackage{color}
\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage{xparse,environ,l3regex}
\newcommand\milasterisk[1]{\makebox[0pt][l]{$^{#1}$}}
\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}\cB\{\2\cE\} }
\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}