4
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Feb 5, 2014 at 21:15
  • $\begingroup$ You need to do SetOptions[$FrontEnd, ...] duplicate: Is there a way to programmatically set global preferences? $\endgroup$
    – rm -rf
    Commented Feb 5, 2014 at 21:21
  • 1
    $\begingroup$ 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. $\endgroup$
    – rm -rf
    Commented Feb 5, 2014 at 21:25
  • $\begingroup$ @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. $\endgroup$
    – preeti
    Commented Feb 6, 2014 at 21:03

1 Answer 1

5
$\begingroup$

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.

$\endgroup$

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.