4

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".

rm -rf
  • 88,781
  • 21
  • 293
  • 472
preeti
  • 833
  • 1
  • 7
  • 17

1 Answers1

5

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:

enter image description here

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.

enter image description here

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

Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158