Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Inserting a Dynamic object in a boxform expression messes up syntax colouring. See the following example. where the variable local is dynamically replaced at the box level, and the syntax highlighter cannot keep up with it.

on = True;
     Checkbox@Dynamic@on, Spacer@10,
     ExpressionCell[RawBoxes@RowBox@{"Block", "[", RowBox@{
      RowBox@{"{", "local", "}"}, ",",

        (* autostyling applies fine *)
        "local", ",",
        ToBoxes@ToExpression["local", StandardForm, HoldForm], ",",
        ToBoxes@Dynamic@ToExpression["local", StandardForm, HoldForm], ",",
        ToBoxes@Dynamic@If[on, ToExpression["local", StandardForm, HoldForm],
            ToExpression["local", StandardForm, HoldForm]], ",",

        (* autostyling applies only for initial display *)
        ToBoxes@Dynamic@If[on, ToExpression["local", StandardForm, HoldForm],
            ToExpression["global", StandardForm, HoldForm]], ",",

        (* autostyling does not apply *)
        ToBoxes@"local", ",",
        ToBoxes@Dynamic@"local", ",",
        ToBoxes@Dynamic@If[on, "local", "global"],
      }, "]"}, "Input", ShowAutoStyles -> True, LanguageCategory -> "Mathematica"]}

When the checkbox is clicked, it switches between:

Mathematica graphics Mathematica graphics Mathematica graphics

and so on... Note, that the 5th local in the body of Block stays blue. How to overcome this?

share|improve this question
I may be mistaken, I don't think the If statement in your last example can be converted to boxes and still be interpreted as code. I also might not be understanding the exact goal. Using HoldForm@undefined instead of "undefined" yields a blue symbol in the output. But I suspect that won't help your actual case. –  Michael E2 Mar 16 at 17:03
@MichaelE2 You are right, I made a mistake with the stringified variable name, let me think it over again. –  István Zachar Mar 16 at 17:06
Deleting until problem is better understood. –  István Zachar Mar 16 at 18:31
@MichaelE2 I have successfully narrowed down my original problem to this minimal working example - question has been rewritten entirely (though title remained as it still applies). I have no idea how to solve this. –  István Zachar Apr 3 at 9:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.