5
$\begingroup$

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. enter image description here

$\endgroup$
4
  • $\begingroup$ (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. $\endgroup$
    – Michael E2
    Commented Oct 6, 2017 at 11:25
  • $\begingroup$ @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. $\endgroup$ Commented Oct 6, 2017 at 12:18
  • $\begingroup$ @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.sstatic.net/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. $\endgroup$
    – Michael E2
    Commented Oct 6, 2017 at 12:26
  • $\begingroup$ @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... $\endgroup$ Commented Oct 6, 2017 at 12:32

4 Answers 4

4
+100
$\begingroup$

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"]]
]
$\endgroup$
12
  • $\begingroup$ 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. $\endgroup$
    – emnha
    Commented Oct 6, 2017 at 16:57
  • $\begingroup$ 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. $\endgroup$
    – Carl Woll
    Commented Oct 6, 2017 at 17:04
  • $\begingroup$ This is what I get by evaluating that command {StyleDefinitions->FrontEndFileName[{$RootDirectory,C:,Users,Administrator,Desktop,Mathematica,EssentialMathematicaArchive},TutorialJJK.nb,CharacterEncoding->UTF-8]} ` $\endgroup$
    – emnha
    Commented Oct 6, 2017 at 17:09
  • $\begingroup$ @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. $\endgroup$
    – Carl Woll
    Commented Oct 6, 2017 at 17:27
  • $\begingroup$ Can you be a little bit more specific? How to do that? I am completely new to stylesheet. $\endgroup$
    – emnha
    Commented Oct 6, 2017 at 17:33
7
$\begingroup$

You can do this by modifying AutoStyleOptions programmatically:

SetOptions[EvaluationNotebook[], 
 AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Bold,
     FontColor -> Blue, ShowAutoStyles -> False, ShowSyntaxStyles -> False, 
     AutoNumberFormatting -> False}}]
$\endgroup$
8
  • $\begingroup$ Could you tell me where to add this code? $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:16
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Nov 7, 2016 at 8:18
  • $\begingroup$ Thank you. I tried to evaluate that but it doesn't work. Maybe my stylesheet overdrives this and do not allow me to change. $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:32
  • $\begingroup$ I am using stylesheet from the course below. Hope you could help me out. physics.umd.edu/courses/CourseWare/EssentialMathematica $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:36
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Nov 7, 2016 at 8:47
6
$\begingroup$

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

enter image description here

$\endgroup$
8
  • $\begingroup$ 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? $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:11
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Nov 7, 2016 at 8:25
  • $\begingroup$ I am using stylesheet from the course below. Hope you could help me out. physics.umd.edu/courses/CourseWare/EssentialMathematica $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:37
  • $\begingroup$ 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]] $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:39
  • $\begingroup$ @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. $\endgroup$ Commented Nov 7, 2016 at 23:37
4
$\begingroup$

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

Appearance > Syntax Coloring > Other

enter image description here

$\endgroup$
1
  • $\begingroup$ 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? $\endgroup$
    – emnha
    Commented Nov 7, 2016 at 8:11

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.