# Comment color in a notebook

My requirement:

1. Comments should appear within a notebook in blue and font size 12 and in Courier by default.
2. This should work on not just my machine (by tweaking the preference of MY Mathematica)
3. I could use this code but the problem is when I open the notebook, the comments are still in gray.

SetOptions[EvaluationNotebook[], AutoStyleOptions -> {"CommentStyle" -> {FontColor -> Blue,
FontFamily -> "Courier", FontSize -> 12, FontWeight -> Bold}}];


Once I evaluate this cell, it turns blue. I would like to have it in this format even before I evaluate anything. I am looking for something where just by opening this notebook, it will by default evaluate this cell or something like that, but machine independent?

4. I could simply use cell style as "Text" and have it formatted. However, the problem is, I cannot divide cells within a function and if I need to put in comments within a function is not possible to use the cell style as "Text".

-
In version 7 the code you provided produces a persistent change, meaning that after closing and reopening Mathematica and the Notebook, the comment is still in blue. –  Mr.Wizard Feb 5 '14 at 21:15
You need to do SetOptions[\$FrontEnd, ...] duplicate: Is there a way to programmatically set global preferences? –  The Toad Feb 5 '14 at 21:21
In addition to my above comment, what you really need is a stylesheet for your notebook. See my answer here: Is it possible to change the colors of the highlighted code in the Mathematica frontend? on designing one. –  The Toad Feb 5 '14 at 21:25
@rm -rf : I don't think this is duplicate because I want to not set it globally but only for that notebook. I think using a stylesheet (which I had not heard of before) is the suitable way. Thanks. –  preeti Feb 6 '14 at 21:03

You are almost there. What you need to do is "lock" this style preference into your notebook rather than do an evaluation every time you open the notebook. To do this create a private style. You can do this via Format > Edit Stylesheet or programmatically:

SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"],
AutoStyleOptions -> {"CommentStyle" -> {FontColor -> Blue,
FontFamily -> "Courier", FontSize -> 12,
FontWeight -> Bold}}]},
Visible -> False,
StyleDefinitions -> "PrivateStylesheetFormatting.nb"]];


Which dos the same thing:

So now these styling preferences are embedded in your notebook and you do not need to set the styling each time you use the notebook.

For more versatility make your own stylesheet and use it with multiple notebooks.

-