# Automatically use blue color for comments in input cell

Currently, when I type comments into input cell the comment has the same color or style as the function color/style as shown in figure number 1.

I would like to automatically use blue color for my comment in input cell as illustrated in figure number 2.

Could anyone tell me how to do that?

Here is the stylesheet I used. I got it from the course below: http://www.physics.umd.edu/courses/CourseWare/EssentialMathematica/

Now, I am using Mathematica Student Edition version 10.4.0.0.

• (1) Each notebook appears to have a private stylesheet (I checked only two), so you'll have to reset the stylesheet. (2) There is no syntax coloring in input cells at all. If someone knows how it is turned on/off, they might be able to answer. – Michael E2 Oct 6 '17 at 11:25
• @MichaelE2 "There is no syntax coloring in input cells at all." - This statement is not true, obviously. The syntax coloring is controlled by the ShowAutoStyles option. – Alexey Popkov Oct 6 '17 at 12:18
• @AlexeyPopkov There is no syntax coloring at all on my machine, then (V11.2, MacOS). Everything is a nice, bold, black in the input cells of the notebooks obtained from the site linked by the OP. It's "obvious" :) -- i.stack.imgur.com/drFYJ.png -- Also, I had already checked ShowAutoStyles, which is set to True according to the Options Inspector, so that is not (the only thing) controlling the styling in these notebooks. Also, the comment color is supposed to be non-black color I can't describe, again according to the Options Inspector. – Michael E2 Oct 6 '17 at 12:26
• @MichaelE2 I meant in general there is syntax coloring in Mathematica. That old notebook is quite strange, need to investigate why it switches off the syntax coloring. I suppose that some old (and unsupported) options interfere with new settings in an unexpected way... – Alexey Popkov Oct 6 '17 at 12:32

You're using an ancient stylesheet, that doesn't take advantage of stylesheet inheritance. In particular, it doesn't inherit from the default/core stylesheet where autostyles are controlled. You can fix autostyles by inheriting the default stylesheet. You can use Format | Edit Stylesheet... from the menu and edit the stylesheet used by your notebook by adding the cell Cell[StyleData[StyleDefinitions -> "Default.nb"]] at the top. Or, you can open the stylesheet notebook itself and add that cell, at the top.

A programmatic method, given the file location of the stylesheet, is to use:

NotebookSave @ NotebookWrite[
NotebookOpen@file,
Cell[StyleData[StyleDefinitions->"Default.nb"]]
]

• Thanks. I like that ancient stylesheet but I do not have ability to create my own now. Where to put that code above? I tried evaluating that code in the notebook but the input cell now is similar to default. – anhnha Oct 6 '17 at 16:57
• What does Options[EvaluationNotebook[], StyleDefinitions] return? If it isn't something like {StyleDefinitions -> Notebook[{Cell[..], ...}, ...]} then you need to go to select Format | Edit Stylesheet.. from the menu, and then go back to the notebook and make sure the Options call above returns the desired StyleDefinitions output. – Carl Woll Oct 6 '17 at 17:04
• This is what I get by evaluating that command {StyleDefinitions->FrontEndFileName[{$RootDirectory,C:,Users,Administrator,Desktop,Mathematica,EssentialMathematicaArchive},TutorialJJK.nb,CharacterEncoding->UTF-8]}  – anhnha Oct 6 '17 at 17:09 • @anhnha If you can get the file location (as indicated in your message above), then you can use NotebookSave/NotebookWrite/NotebookOpen to add the inherited stylesheet. – Carl Woll Oct 6 '17 at 17:27 • Can you be a little bit more specific? How to do that? I am completely new to stylesheet. – anhnha Oct 6 '17 at 17:33 You can do this by modifying AutoStyleOptions programmatically: SetOptions[EvaluationNotebook[], AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Bold, FontColor -> Blue, ShowAutoStyles -> False, ShowSyntaxStyles -> False, AutoNumberFormatting -> False}}]  • Could you tell me where to add this code? – anhnha Nov 7 '16 at 8:16 • @BlueSky You should simply evaluate this in the Notebook where you wish to change the formatting of comments. If you wish to make this default formatting, just replace EvaluationNotebook[] with $FrontEnd. – Alexey Popkov Nov 7 '16 at 8:18
• Thank you. I tried to evaluate that but it doesn't work. Maybe my stylesheet overdrives this and do not allow me to change. – anhnha Nov 7 '16 at 8:32
• I am using stylesheet from the course below. Hope you could help me out. physics.umd.edu/courses/CourseWare/EssentialMathematica – anhnha Nov 7 '16 at 8:36
• @BlueSky In the "TutorialJJK.nb" stylesheet I haven't found the AutoStyleOptions, so probably there is another reason. Note that the stylesheet is very old, it is created with version 4.0, hence it potentially can contain some obsolete options which can produce unexpected results in new versions. – Alexey Popkov Nov 7 '16 at 8:47

While you can do this via a stylesheet, and also programmatically, the quickest way to do it is via the preferences menu:

• Thanks. I tried this and this works. But the problem is that it only work with default stylesheet. However, I am using a stylesheet from other and it doesn't work when I load this stylesheet. Could you tell me how to make it work with the stylesheet? – anhnha Nov 7 '16 at 8:11
• @BlueSky Probably your custom stylesheet has the AutoStyleOptions option set for the "Input" style, and this option overrides the global option. Please check this: open the stylesheet, find the "Input" style, select the cell and press Shift+Ctrl+E. – Alexey Popkov Nov 7 '16 at 8:25
• I am using stylesheet from the course below. Hope you could help me out. physics.umd.edu/courses/CourseWare/EssentialMathematica – anhnha Nov 7 '16 at 8:37
• I have just check it and get this for input cell: Cell[StyleData["Input"], CellFrame->0.5, CellMargins->{{25, 10}, {8, 8}}, Evaluatable->True, CellGroupingRules->"InputGrouping", CellHorizontalScrolling->True, PageBreakWithin->False, GroupPageBreakWithin->False, CellLabelMargins->{{5, Inherited}, {Inherited, Inherited}}, DefaultFormatType->DefaultInputFormatType, FormatType->InputForm, ShowStringCharacters->True, NumberMarks->True, CounterIncrements->"Input", FontSize->18, FontWeight->"Bold", Background->RGBColor[1, 0.700008, 0.4]] – anhnha Nov 7 '16 at 8:39
• @BlueSky you can add the AutoStyleOptions that Alexey shows in his answer to the Input` style for your stylesheet. In comments to Alexey's answer it appears the stylesheet is very old. Maybe you are using an old version of Mathematica too. Can you please update your question with the version number you are using. – Mike Honeychurch Nov 7 '16 at 23:37

You will find this in the Preferences (or Settings) for Mathematica; follow the tabs in the Preferences window:

Appearance > Syntax Coloring > Other

• Thanks. I tried this and this works. But the problem is that it only work with default stylesheet. However, I am using a stylesheet from other and it doesn't work when I load this stylesheet. Could you tell me how to make it work with the stylesheet? – anhnha Nov 7 '16 at 8:11