5
$\begingroup$

I'm trying to syntax highlight this expression: x=$\sqrt{\pi }$//N and from various sources I've learned that x (the assigned symbol) belongs to context Global while the Pi and N belongs to System. So I set out to change the style of both using a hodgepodge of tips I gleaned from this StackExchange as well as from StackOverflow.

However, the weird thing is that the style changes do not add to each other. If I change the style of System, any previous style change to Global would be wiped away, and vice versa. In other words, I can have syntax highlighting for either System, or Global, but not both. This could be seen from these screenshots (sorry for the small font, code added):


First line:

SetOptions[$FrontEndSession, 
 "AutoStyleOptions" -> {"SymbolContextStyles" -> {"System`" -> 
      RGBColor[211./255, 54./255, 130./255]}}] (*magenta*)

System Magenta Setting System style: magenta


Second line:

SetOptions[$FrontEndSession, 
 "AutoStyleOptions" -> {"SymbolContextStyles" -> {"Global`" -> 
      RGBColor[181./255, 137./255, 0./255]}}] (*yellow*)

Global Yellow Setting Global style: yellow; when evaluated, previously set System style was wiped out


Could anyone guide me toward getting syntax highlighting for both of them? I really hope MMA has better syntax highlighting on their future releases.

$\endgroup$
2
  • $\begingroup$ BTW I got the RGB color from the Solarized theme here: zovirl.com/2011/07/22/solarized_cheat_sheet $\endgroup$ Commented Jun 1, 2014 at 7:39
  • 1
    $\begingroup$ Since no one has mentioned it, you can set this option for the "Input" style by editing the stylesheet. Enter/choose the Input style, select the cell, use menu command Edit > Show Expression, and add the option so that the cell expression looks like this: Cell[StyleData["Input"], AutoStyleOptions->{"SymbolContextStyles"->{"System`" -> RGBColor[211./255, 54./255, 130./255], "Global`" -> RGBColor[181./255, 137./255, 0./255], Automatic -> {}}}]. Then the style will persist from session to session. $\endgroup$
    – Michael E2
    Commented Jun 23, 2014 at 0:35

2 Answers 2

9
$\begingroup$

To prevent your second invocation of SetOptions from resetting the value of sub-option "SymbolContextStyles", you need to set both "System" and "Global" sub-sub-option values at once:

SetOptions[EvaluationNotebook[],   
    "AutoStyleOptions" -> {"SymbolContextStyles" -> 
                             {"System`" -> RGBColor[211./255, 54./255, 130./255], 
                               "Global`" -> RGBColor[181./255, 137./255, 0./255]}}]

Note: Replace EvaluationNotebook[] with $FrontEndSession to make the new setting effective for all notebooks in the current MMA session, and with $FrontEnd to make them apply to current and later sessions.

This setting works as expected on my Windows 8 (64bit) system with Version 9.0.1.0.

enter image description here

.

$\endgroup$
6
  • $\begingroup$ OMG you don't know how happy you're making me right now! I wanna kiss you! $\endgroup$ Commented May 31, 2014 at 19:52
  • 1
    $\begingroup$ @seismatica, glad it made you so happy.. $\endgroup$
    – kglr
    Commented May 31, 2014 at 20:04
  • $\begingroup$ Just one comment for those who are trying to follow this: use EvaluationNotebook[] if you want to apply the highlighting to your current notebook. Replace it with FrontEndSession (with a dollar sign in front--I can't type the dollar sign without messing up the formatting of the comment*) to apply to currently opened notebooks, or FrontEnd (with a dollar sign in front) for all notebooks from then on. That's what my understanding of this is. Please correct me if I'm wrong in any part. *Also see the code in my question. $\endgroup$ Commented Jun 1, 2014 at 7:44
  • $\begingroup$ @seismatica, good point. Just added a note in the post. $\endgroup$
    – kglr
    Commented Jun 1, 2014 at 9:12
  • 1
    $\begingroup$ The docs have material on CurrentValue, SetOptions, $FrontEndSession, and the like. Options is a sparsely charted - and expanding - territory. My main source for undocumented or rare functions or options is this site and random exploration of the links in this Q/A. $\endgroup$
    – kglr
    Commented Jun 1, 2014 at 13:09
5
$\begingroup$

Note that while the accepted answer obviously answers the question it is also possible to set values of System` and Global` individually.

CurrentValue[EvaluationNotebook[], {"AutoStyleOptions", "SymbolContextStyles"}]
(* {"System`" -> {}, "Global`" -> {}, Automatic -> {}} *)

enter image description here

So to set only System`:

CurrentValue[EvaluationNotebook[], {"AutoStyleOptions", "SymbolContextStyles",   
"System`"}] = Orange

enter image description here

And to set only Global`:

CurrentValue[EvaluationNotebook[], {"AutoStyleOptions", "SymbolContextStyles", 
   "Global`"}] = Green

enter image description here

Use EvaluationNotebook[], FrontEnd, or $FrontEndSession as required as per discussion above.

$\endgroup$
2
  • $\begingroup$ Thank you so much for this as well; it shows the error that I made to set SymbolContextStyles to just contain System` or Global`. I have a question: when you use CurrentValue on the EvaluationNotebook, what exactly is {"AutoStyleOptions", "SymbolContextStyles"} supposed to return? Is SymbolContextStyles a subset of AutoStyleOptions? I've been trying to find detailed documentation on this to no avail, so I'd love it if you could tell us where you learned about all this. PS: I checked out your website and was very impressed by all the front-end stuff. I hope I could reach that level one day. $\endgroup$ Commented Jun 1, 2014 at 12:09
  • 1
    $\begingroup$ @seismatica the best place to see how these syntax options are nested is to go to the options inspector and search for e.g. "AutoStyleOptions" and you will see that SymbolContextStyles is a subset. $\endgroup$ Commented Jun 1, 2014 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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