4
$\begingroup$

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"]

enter image description here

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

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 -> FEPrivate`If[CurrentValue[EvaluationCell[],
   CellTags]==="blue", RGBColor[0,0,1], Inherited]
 ]

or this, from @MB1965:

Cell[StyleData["MyInput"],
 Background -> FEPrivate`If[FEPrivate`SameQ[  
      FrontEnd`CurrentValue[FrontEnd`EvaluationCell[], CellTags],
      "blue"], 
    RGBColor[0,0,1],
    Inherited]
 ]
$\endgroup$
  • 1
    $\begingroup$ It isn't different enough to post a separate answer so maybe you can add it, if you want ofc. Background -> FEPrivate`If[CurrentValue[EvaluationCell[], CellTags]==="red", RGBColor[1,0,0], Inherited] $\endgroup$ – Kuba May 22 '17 at 21:14
  • $\begingroup$ That works, but what is ThisCell[]? $\endgroup$ – M.R. May 22 '17 at 21:26
  • 1
    $\begingroup$ @MichaelE2 it might be worth making a note that if people are using the second programmatically (e.g. with SetOptions) they'll want FEPrivate`SameQ[ FrontEnd`CurrentValue[FrontEnd`EvaluationCell[], CellTags], "blue"] $\endgroup$ – b3m2a1 May 23 '17 at 2:07
  • 3
    $\begingroup$ @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 System`If, System`CurrentValue, and System`SameQ. It does not know color names, though, so the usage of RGBColor is entirely appropriate here. $\endgroup$ – John Fultz May 23 '17 at 7:36
  • 1
    $\begingroup$ @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. $\endgroup$ – John Fultz Jul 1 '17 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.