With Format -> Edit Stylesheet... it is possible to adjust text-properties, text-colors, formatting of equations, etc of a notebook and to use this style-definitions again by installing it as Stylesheet.

Lately, I saw a very nice screen-shot of a notebook and I noticed, that not only the text-properties were adjusted, but the style of the input code too. Using the style-sheet editor notebook it is absolutely not obvious to me, how I could achieve the coloring of the input code.

Question: Can someone explain what steps are necessary to set up my own code style?

enter image description here

  • 1
    $\begingroup$ Just to clarify: you don't want to change the highlighting colours globally (Edit -> Preferences -> Apperance -> Syntax Coloring). You want these changes to be part of a stylesheet (and thus notebook-local). Is this correct? $\endgroup$
    – Szabolcs
    May 23, 2012 at 8:08
  • $\begingroup$ @Szabolcs Yes and no. Yes, because the question above was basically a follow-up question of this here mathematica.stackexchange.com/q/5846/187 and it was mainly about styles for talkes and documents and not for hack/work notebooks. But I must admit, I wasn't aware that this is possible in general until I saw it in the screenshot. Therefore, it is in very nice to know, that I can change it globally with the Appearance settings. $\endgroup$
    – halirutan
    May 23, 2012 at 9:06

2 Answers 2


The colors used by the syntax highlighter can be set by changing the styles for StandardForm. The following is a how-to that explains how I styled the input cell in the screenshot in the question. This should be a starting point to get your own custom highlighting scheme up and running. Note that you can also do the same by choosing the colours in Preferences > Appearance, but this is not easily portable and gets wiped away with a reinstall/corrupted/deleted FE/init.m file. Controlling via stylesheets is preferable.

1: Adding a new style cell and editing it

To add new definitions for StandardForm, open the stylesheet that you wish to change, and enter StandardForm in the box next to the "Choose a style"

enter image description here

Next, select the cell and press CmdShiftE to show the cell contents (if you already have an existing style, edit that instead of creating a new one). You should see something like:


2: Setting up the different colours

The syntax highlighting colours are set via nested rules for AutoStyleOptions, with individual style tokens corresponding to patterns, errors, undefined variables, etc. The basic syntax for this is

    AutoStyleOptions -> {
        "StyleToken1" -> {FontColor -> RGBColor[...], FontSlant -> ...},
        "StyleToken2" -> {FontColor -> RGBColor[...]},

Here's a dummy example that I put together to show the different style tokens and what they each are responsible for

enter image description here

In addition to colours, you can set each of these to have different slants/weights, etc. A full list of style tokens is

{"CommentStyle", "EmphasizedSyntaxErrorStyle", "ExcessArgumentStyle",
 "FunctionLocalVariableStyle", "LocalScopeConflictStyle", 
 "LocalVariableStyle", "MissingArgumentStyle", "OrderOfEvaluationConflictStyle", 
 "PatternVariableStyle", "StringStyle", "SymbolShadowingStyle", "SyntaxErrorStyle", 
 "UndefinedSymbolStyle", "UnknownOptionStyle", "UnwantedAssignmentStyle"}

I personally do not like using all possible tokens and only set the ones shown in the dummy example. Too many colours makes it jarring, but to each his own.

3: Changing the main font and background colors

Finally, you can set the main font properties. This is what controls the colour of the "defined" variables. For example (include the styles from above in the ...),

    FontFamily -> ...,
    FontSize   -> 12,
    FontWeight -> "Plain",
    FontSlant  -> "Plain"

When you put all of these together, close the cell contents by pressing CmdShiftE again. Now you're all set to use the new styles. These styles will be set only for that particular notebook. If you want to set them as default for all notebooks, you should save the stylesheet in $UserBaseDirectory/SystemFiles/FrontEnd/StyleSheets/

  • $\begingroup$ Big +1. Where did you find the style tokens? I could have sworn I had came into some of them in core.nb but I was clearly dreaming $\endgroup$
    – Rojo
    May 23, 2012 at 1:10
  • $\begingroup$ What a great answer. I will wait with accepting this because in my opinion users rather look into an unaccepted question (with only one answer!) to see whether they can contribute something. When they see this, they'll definitely vote for it. Awsome. Thank you. $\endgroup$
    – halirutan
    May 23, 2012 at 1:17
  • $\begingroup$ @Rojo There are apparently some more style tokens (8 more), which you can find by going to the options inspector and typing AutoStyleOptions in the search bar (probably would've been simpler to look here in the first place). $\endgroup$
    – rm -rf
    May 23, 2012 at 1:46
  • $\begingroup$ Looking at AutoStyleOptions, I'm trying to figure out what "GlobalSymbolStyle" highlights. Any thoughts? $\endgroup$
    – rcollyer
    May 23, 2012 at 2:18
  • 3
    $\begingroup$ @rcollyer It doesn't seem to do anything. I'm guessing it was grandfathered in for backwards compatibility. You can style any particular context with "SymbolContextStyles" $\endgroup$
    – rm -rf
    May 23, 2012 at 2:30

As @R.M. explained, the styling rules are stored in the AutoStyleOptions setting.

There is already a built-in editor for the code highlighting style, that can be accessed through Edit -> Preferences... -> Apperance -> Syntax Coloring. This changes the styles globally though.

My suggestion is to first edit the global styling (because the built-in editor is so much easier to use than editing cell expressions manually), then transfer them to a notebook, like this:

SetOptions[EvaluationNotebook[], Options[$FrontEnd, AutoStyleOptions]]

You can transfer them to a style sheet notebook as well. When you're done, you can reset the global styles using the Defaults button in the preferences GUI.

If you're comfortable doing everything through editing cell expressions, then all this isn't really necessary, but I find editing cell expressions tedious, so I thought it would be worth pointing out this possibility.

  • $\begingroup$ I changed the "is" into an "isn't" in the last sentence because I think this is what you meant. +1, because I was not aware that this is possible. $\endgroup$
    – halirutan
    May 23, 2012 at 9:15
  • $\begingroup$ @halirutan Thanks for the edit! $\endgroup$
    – Szabolcs
    May 23, 2012 at 9:20
  • $\begingroup$ @Szabolcs Adding this code and running it in a stylesheet does not move the syntax coloring. Am I missing something? $\endgroup$ Oct 1, 2013 at 23:27
  • $\begingroup$ @RHall I don't work with stylesheets much ... instead of running it in the stylesheet, can you try to add it directly? You can still extract the styles using Options[$FrontEnd, AutoStyleOptions]. $\endgroup$
    – Szabolcs
    Oct 1, 2013 at 23:38
  • $\begingroup$ @RHall Actually it does seem to work. I opened a notebook and looked at its stylesheet using CurrentValue[EvaluationNotebook[], "StyleDefinitions"]. Then I went to Format -> Edit Stylesheet, and closed the stylesheet window. I tried CurrentValue[EvaluationNotebook[], "StyleDefinitions"] again and now it gives me a Notebook[...] expression. Finally I went to Format -> Stylesheet again, and evaluated the line from the answer in that window. I checked CurrentValue[EvaluationNotebook[], "StyleDefinitions"] the third time, and the information was there. However, I do not actually ... $\endgroup$
    – Szabolcs
    Oct 1, 2013 at 23:41

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.