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.

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.

share|improve this question
    
BTW I got the RGB color from the Solarized theme here: zovirl.com/2011/07/22/solarized_cheat_sheet –  seismatica Jun 1 at 7:39
1  
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. –  Michael E2 Jun 23 at 0:35

2 Answers 2

up vote 9 down vote accepted

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

.

share|improve this answer
    
OMG you don't know how happy you're making me right now! I wanna kiss you! –  seismatica May 31 at 19:52
1  
@seismatica, glad it made you so happy.. –  kguler May 31 at 20:04
    
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. –  seismatica Jun 1 at 7:44
    
@seismatica, good point. Just added a note in the post. –  kguler Jun 1 at 9:12
1  
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. –  kguler Jun 1 at 13:09

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.

share|improve this answer
    
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. –  seismatica Jun 1 at 12:09
1  
@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. –  Mike Honeychurch Jun 1 at 22:07

Your Answer

 
discard

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

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