# How to style a cell depending on CellTags?

Is there a way to create a style definition that reformats its background based on it's own tags?

The following doesn't work:

Cell[StyleData["MyInput"],
Background -> If[CurrentValue[ThisCell[], CellTags]=="blue", Blue, Red]
]
Cell[BoxData["test"], "MyInput", CellTags->"blue"]


• What is ThisCell? Shouldn't it be EvaluationCell? p.s. related: version specific notebook styles – Kuba May 22 '17 at 20:55
• @Kuba that's the question, it is undefined - and I don't think EvaluationCell is what is wanted – M.R. May 22 '17 at 21:24
• You said based on its own tags so I think it is. Or I am missing the point. – Kuba May 22 '17 at 21:30
• @Kuba Michael E2's answer works, I don't know why/how it works. – M.R. May 22 '17 at 21:35

Using Dynamic and SameQ work for me:

Cell[StyleData["MyInput"],
Background -> Dynamic[If[CurrentValue[CellTags]==="blue", Blue, Red]]
]


An alternative, from @Kuba:

Cell[StyleData["MyInput"],
Background -> FEPrivateIf[CurrentValue[EvaluationCell[],
CellTags]==="blue", RGBColor[0,0,1], Inherited]
]


or this, from @MB1965:

Cell[StyleData["MyInput"],
Background -> FEPrivateIf[FEPrivateSameQ[
FrontEndCurrentValue[FrontEndEvaluationCell[], CellTags],
"blue"],
RGBColor[0,0,1],
Inherited]
]

• It isn't different enough to post a separate answer so maybe you can add it, if you want ofc. Background -> FEPrivateIf[CurrentValue[EvaluationCell[], CellTags]==="red", RGBColor[1,0,0], Inherited] – Kuba May 22 '17 at 21:14
• That works, but what is ThisCell[]? – M.R. May 22 '17 at 21:26
• @MichaelE2 it might be worth making a note that if people are using the second programmatically (e.g. with SetOptions) they'll want FEPrivateSameQ[ FrontEndCurrentValue[FrontEndEvaluationCell[], CellTags], "blue"] – b3m2a1 May 23 '17 at 2:07
• @Kuba @MB1965 I have problems with the reliance on non-Dynamic heads. Yes, it tends to get used a lot, and sometimes by us, but it's problematic for various reasons and not documented anywhere, so I wouldn't count on it surviving forever. Wanting to avoid kernel round trips is a good thing, but a Dynamic head does that just fine, if you stick to the subset of symbols the FE knows. And the FE does know SystemIf, SystemCurrentValue, and SystemSameQ. It does not know color names, though, so the usage of RGBColor is entirely appropriate here. – John Fultz May 23 '17 at 7:36
• @Kuba, I see what you're referring to. I'll file a bug report for myself to, at some point, go and clean up that tutorial. – John Fultz Jul 1 '17 at 21:50