# How to set “SymbolContextStyles” for $Context symbols? How to change the font colour of symbols defined in the current context (e.g. $Context) from black to some other colour, say purple?

There is a "SymbolContextStyles" setting in Options Inspector under Editing Options | Private Editing Options | AutoStyleOptions. However, while you can set "System","Global", and Automatic there does not seem to be a way to set $Context. In Options Inspector I tried adding $Context -> {RGBColor[0.5, 0, 0.5]} to the list and then "$Context" -> {RGBColor[0.5, 0, 0.5]} for the selected notebook but that made no change. Adding Dynamic[$Context] -> {RGBColor[0.5, 0, 0.5]} works but the notebook is near unusable when editing it; becomes very laggy.

I want the symbols defined in the notebook to have purple font colour. I only use notebook contexts so the Global setting is of no use to me.

Is there a way to do this?

• I've wondered this often myself. I wonder if you can hack a Dynamic version of $Context with some front-end trick like FrontEndValue["$Context"] or whatnot. – b3m2a1 Feb 21 '19 at 20:18
• @b3m2a1 I tried Dynamic[$Context] -> {RGBColor[0.5, 0, 0.5]}. It works but it kills the performance of the notebook just when editing. – Edmund Feb 21 '19 at 20:28 • Dynamic isn't really what you want. You'll want something purely in the FE like FrontEndValue["$Context"] if it'll work. – b3m2a1 Feb 21 '19 at 20:29

It is far from being perfect but maybe it can help. Just put this line on top of the notebook, in an initalization cell or wherever.

CurrentValue[$$FrontEndSession, {AutoStyleOptions, "SymbolContextStyles",$$Context}] = Purple;


Unfortunately you need to have it for each notebook. But the bright side is it works and it is cleaned when you close MMA and that context does not exist anymore.

Moreover, you can make it appear automatically in new notebooks by doing:

SetOptions[$$FrontEnd, NotebookEventActions :> { {"MenuCommand", "New"} :> CreateDocument[ ExpressionCell[ Defer[ CurrentValue[FrontEndSession, {AutoStyleOptions, "SymbolContextStyles",$$Context}] = Purple
]
, "Code"]
]
}
, CellContext -> Notebook
]


Code adapted from Create new notebook at fixed size

• A little more efficient, I think, is to have the "SymbolContextStyles" point to a custom style, like "ContextStyle" and then you can just update that "ContextStyle" rather than having to cook all your directives into the notebook themselves – b3m2a1 Feb 21 '19 at 22:39
• you could also set it up so that the NotebookDynamicExpression has like FEPrivateSet[FrontEndCurrentValue[FrontEndEvaluationNotebook[], {AutoStyleOptions, "SymbolContextStyles", FrontEndValue["$Context"]}], "CustomStyle"] (okay not entirely sure if that'll work but a variant might) – b3m2a1 Feb 21 '19 at 23:05 • @b3m2a1 separate style is good idea if you want to commit to that solution full scale. Otherwise you'd need to edit default styles which adds a layer that one may want to avoid. I don't trust NotebookDynamicExpression and working with it is ugly so I ignored it on purpose :) – Kuba Feb 22 '19 at 8:28 • true I’ve failed to find a good FE programming solution no matter what I’ve tried and that’s the only case in which NotebookDynamicExpression should be used m – b3m2a1 Feb 22 '19 at 8:28 • This is promising. I would go with EvaluationNotebook[] instead of $FrontEndSession so the global preferences do not fill up. Is there a way to add a rule the "SymbolContextStyles" that uses EvaluataionNotebook[]` to resolve the context such that it could be placed in the global preferences? (+1) – Edmund Feb 22 '19 at 14:09