5
$\begingroup$

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?

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

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

$\endgroup$
  • 1
    $\begingroup$ 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 $\endgroup$ – b3m2a1 Feb 21 at 22:39
  • $\begingroup$ you could also set it up so that the NotebookDynamicExpression has like FEPrivate`Set[FrontEnd`CurrentValue[FrontEnd`EvaluationNotebook[], {AutoStyleOptions, "SymbolContextStyles", FrontEnd`Value["$Context"]}], "CustomStyle"] (okay not entirely sure if that'll work but a variant might) $\endgroup$ – b3m2a1 Feb 21 at 23:05
  • $\begingroup$ @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 :) $\endgroup$ – Kuba Feb 22 at 8:28
  • $\begingroup$ 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 $\endgroup$ – b3m2a1 Feb 22 at 8:28
  • $\begingroup$ 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) $\endgroup$ – Edmund Feb 22 at 14:09

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.