Format cells if they contain a specific symbol with siunitxFormat cells if they contain a specfic symbol /...
Other authors are notified except me, good or bad sign?
Can inspiration allow the Rogue to make a Sneak Attack?
Why is there an extra space when I type "ls" on the Desktop?
What happens when you cast a spell on yourself with spell turning on?
newcommand: Using one parameter as the default for the other
What is Tony Stark injecting into himself in Iron Man 3?
Why would /etc/passwd be used every time someone executes `ls -l` command?
How do you make a gun that shoots melee weapons and/or swords?
I can't die. Who am I?
How does Sundering Titan handle snow lands?
New invention compresses matter to produce energy? or other items? (Short Story)
Insult for someone who "doesn't know anything"
Detemine the unit digit of a number
Do natural melee weapons (from racial traits) trigger Improved Divine Smite?
Having the player face themselves after the mid-game
What can I do if someone tampers with my SSH public key?
How does a sound wave propagate?
SHA 256 Algorithm
Deal the cards to the players
What is the orbital boost acceleration of the ISS?
If nine coins are tossed, what is the probability that the number of heads is even?
Are small insurances worth it
Is there a way to find out the age of climbing ropes?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Format cells if they contain a specific symbol with siunitx
Format cells if they contain a specfic symbol / conditional cell colorFormat cells if they end with an * with siunitxifthen with different format numbersCsvSimple with tabular: set specific number of rowsConTeXt : defining specific behavior with modeFormat cells if they contain a specfic symbol / conditional cell colorDetect if a unit is defined with siunitxFormat cells if they end with an * with siunitx
In my previous question, David Carlisle gave a great solution for coloring all text in a cell when the cell contains an *.
I played a little bit with this code, but couldn't include it, if the siunitx package was used:
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
{catcode`*=active
gdefzz#1{%
mathcode`*="8000
gdeffoo{}%
def*{gdeffoo{color{red}}}%
setbox0hboxbgroup$}
}
defzzz{$egroupfoo{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}
Do you have any ideas, how to set this up to work with siunitx?
Also, it would be great, if the * would stay and not vanish in the coloring process.
conditionals
add a comment |
In my previous question, David Carlisle gave a great solution for coloring all text in a cell when the cell contains an *.
I played a little bit with this code, but couldn't include it, if the siunitx package was used:
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
{catcode`*=active
gdefzz#1{%
mathcode`*="8000
gdeffoo{}%
def*{gdeffoo{color{red}}}%
setbox0hboxbgroup$}
}
defzzz{$egroupfoo{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}
Do you have any ideas, how to set this up to work with siunitx?
Also, it would be great, if the * would stay and not vanish in the coloring process.
conditionals
add a comment |
In my previous question, David Carlisle gave a great solution for coloring all text in a cell when the cell contains an *.
I played a little bit with this code, but couldn't include it, if the siunitx package was used:
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
{catcode`*=active
gdefzz#1{%
mathcode`*="8000
gdeffoo{}%
def*{gdeffoo{color{red}}}%
setbox0hboxbgroup$}
}
defzzz{$egroupfoo{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}
Do you have any ideas, how to set this up to work with siunitx?
Also, it would be great, if the * would stay and not vanish in the coloring process.
conditionals
In my previous question, David Carlisle gave a great solution for coloring all text in a cell when the cell contains an *.
I played a little bit with this code, but couldn't include it, if the siunitx package was used:
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
{catcode`*=active
gdefzz#1{%
mathcode`*="8000
gdeffoo{}%
def*{gdeffoo{color{red}}}%
setbox0hboxbgroup$}
}
defzzz{$egroupfoo{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}
Do you have any ideas, how to set this up to work with siunitx?
Also, it would be great, if the * would stay and not vanish in the coloring process.
conditionals
conditionals
edited Apr 13 '17 at 12:35
Community♦
1
1
asked Jul 27 '16 at 15:58
MilMil
5201419
5201419
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I assume that the * is used only in cells containing numbers; then in the special environments any run of minus signs, digits and period followed by a * is prefixed by color{red} and the * is removed.
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{#1cE}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}

If you want to keep the asterisk, you can do
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse,environ}
newcommandmilasterisk{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{#1cE}1c{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}

With xparse released 2019-05-03, with built-in functionality similar to environ, the code becomes
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse}
newcommandmilasterisk{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{#1cE}1c{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}
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
1
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f321229%2fformat-cells-if-they-contain-a-specific-symbol-with-siunitx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume that the * is used only in cells containing numbers; then in the special environments any run of minus signs, digits and period followed by a * is prefixed by color{red} and the * is removed.
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{#1cE}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}

If you want to keep the asterisk, you can do
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse,environ}
newcommandmilasterisk{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{#1cE}1c{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}

With xparse released 2019-05-03, with built-in functionality similar to environ, the code becomes
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse}
newcommandmilasterisk{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{#1cE}1c{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}
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
1
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
add a comment |
I assume that the * is used only in cells containing numbers; then in the special environments any run of minus signs, digits and period followed by a * is prefixed by color{red} and the * is removed.
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{#1cE}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}

If you want to keep the asterisk, you can do
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse,environ}
newcommandmilasterisk{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{#1cE}1c{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}

With xparse released 2019-05-03, with built-in functionality similar to environ, the code becomes
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse}
newcommandmilasterisk{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{#1cE}1c{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}
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
1
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
add a comment |
I assume that the * is used only in cells containing numbers; then in the special environments any run of minus signs, digits and period followed by a * is prefixed by color{red} and the * is removed.
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{#1cE}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}

If you want to keep the asterisk, you can do
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse,environ}
newcommandmilasterisk{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{#1cE}1c{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}

With xparse released 2019-05-03, with built-in functionality similar to environ, the code becomes
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse}
newcommandmilasterisk{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{#1cE}1c{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}
I assume that the * is used only in cells containing numbers; then in the special environments any run of minus signs, digits and period followed by a * is prefixed by color{red} and the * is removed.
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{#1cE}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}

If you want to keep the asterisk, you can do
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse,environ}
newcommandmilasterisk{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{#1cE}1c{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}

With xparse released 2019-05-03, with built-in functionality similar to environ, the code becomes
documentclass{article}
usepackage{color}
usepackage{array}
usepackage{tabularx}
usepackage{siunitx}
usepackage{xparse}
newcommandmilasterisk{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{#1cE}1c{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}
edited 4 mins ago
answered Jul 27 '16 at 22:02
egregegreg
725k8819183223
725k8819183223
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
1
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
add a comment |
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
1
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
That looks like a fantastic answer, thank you very much. How do I keep the * though? I don't want it to vanish. Simply writing { c{color}cB{#1cE}1* } in the syntax you provided results in ugly typesetting for the * (spacing). Is there a way to simply keep it in the first place?
– Mil
Jul 28 '16 at 8:52
1
1
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
@Mil I added the asterisk back
– egreg
Jul 28 '16 at 9:04
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f321229%2fformat-cells-if-they-contain-a-specific-symbol-with-siunitx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown