how to align comments in Mathematica notebook?
Asked Answered
E

2

7

I do not know if this was asked before or not. Simple formating issue. I noticed it is sometimes hard to align comments (----) on top of each others. Must be a font type issue. It just looks a little better on the screen if I can get things to align exactly on top of each other. Here is an example:

(*                        *)
(*  L E F T    P A N E L  *)
(*                        *)

But it actually looks like this in the notebook in front of me:

enter image description here

If i move the *) in the second line one space to the left it becomes like this:

enter image description here

So, can not get it to align. I am using cell of type Input (standard cell).

I can't use other cell type such as Code or such. I was wondering if someone knows of a trick to get this to align.

thanks

EDIT 1: Showing font used by Input cell in the style sheet Demonstration

enter image description here

EDIT 2: Using code shown below by Alexey Popkov, fixed the font issue for comments, and now they are easily aligned. Here is a screen shot enter image description here

EDIT 3:

Well, the party did not last for long. Using SetOptions is not allowed in a demo. Too bad, because the solution by Alexey worked well and made the comments align and look better.

Any way. Not a big deal really. can live with a little misaligned comments :)

enter image description here

Ellamaeellan answered 15/6, 2011 at 9:23 Comment(7)
this is most likely because you are using a proportional font for input cells; at least here (on OS X), if I do the same, changing the fonts between fixed-width and proportional shows/does not show this. curiously, however, the default seems to be fixed-width here. perhaps something else is going on on your notebooks. have you changed the default stylesheet?Faubion
@acl, I am using the standard WRI demonstration style sheet. Did not change anything. To use this style sheet, do: Style->Style sheet->Wolfram->Demo and you'll see it. Click on Example (on top), and this will load an example notebook, which one can use as template. It has in it the Manipulate cell, which is I use to enter the code. thanksEllamaeellan
I just tried it with that stylesheet and it aligns just fine here (OS X). Don't know what else to suggest.Faubion
@acl: I get the same behaviour as Nasser, using the Linux version of Mma. I tried changing some of the font options and properties for the Input cells... but nothing really worked. Which isn't to say that there isn't a good solution to be found by changing fonts...Mcnutt
@Ellamaeellan I just tried with Courier New. When I set the fontsize to 14, they do not align, while if it's 12 (my default) they do align... All this again on OS XFaubion
acl, thanks for trying. This is how the font setting came with the style sheet, set at 14. I did not change anything. I do not think I can change the font size for the code, since this will be for publication at WRI demo site and I do not think one can change these setting. Not sure if they will accept it. But I will ask. Thanks.Ellamaeellan
@Ellamaeellan You deleted your question about the Workbench. You should not hurry to delete ... it was a good question, the Workbench is in fact Eclipse, and there are tons of questions abut Eclipse here. See stackoverflow.com/questions/tagged/eclipse. There are just a lot of users that ignore what Mma is, but enjoy using the censorship privileges. Just ignore them. Delete a question only when there is an agreement between the Mma community (ie those who frequent this tag)Shrivel
C
2

I do not have such problem with my installation of Mathematica 7 since it uses monospaced font "Courier New" by default for the cells with style Input. But if for some reason a non-monospaced font is used in your installation by default you can redefine the style of comments inside Notebook for using a monospaced font:

SetOptions[EvaluationNotebook[], 
 AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Plain,
     FontColor -> GrayLevel[0.6`], ShowAutoStyles -> False, 
     ShowSyntaxStyles -> False, AutoNumberFormatting -> False, 
     FontFamily -> "Consolas"}}]
Condensation answered 15/6, 2011 at 12:7 Comment(9)
I added EDIT 1, showing the font used. I selected the cell first, then did Format->Font and got screen shot of result. It does say "Courier New" there? thanks,.Ellamaeellan
@Ellamaeellan When I evaluate the code given in my answer and then check the font used by the method that you use I get "Courier New" although in really "Consolas" is used as I see by eyes. So this method is not reliable. Instead try to select only the comment's contents (without (* and *)) and try then to check the font. I get "Consolas" in this case with my code.Condensation
I added the code you show above to the demonstration initialization code, and it did indeed fix the font for the comment so now I can align them with no problem. Thank you. Added EDIT 2 fyiEllamaeellan
so I entered (* *)( X X X X )( **) (with line breaks) in a cell, then in another cell I entered Manipulate[ SetOptions[EvaluationNotebook[], AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Plain, FontColor -> GrayLevel[0.6`], ShowAutoStyles -> False, ShowSyntaxStyles -> False, AutoNumberFormatting -> False, FontFamily -> "Monaco", FontSize [Rule] s}}]; s, {{s, 12}, 5, 20, 1}] and played with the slider. As I change the fontsize, the comments align/don't align randomly. Good thing I don't write comments :)Faubion
@Faubion "Monaco" is not a monospaced font.Condensation
@Alexey strange, I always thought it was. In any case, I tried Consolas, Monaco, Menlo, Courier, Times etc, and the behaviour persists (I realize that not all of these are monospaced). So perhaps the fontsize does have something to do with this after all.Faubion
@Faubion I am sorry, "Monaco" probably is monospaced but it is not installed in my system and "Arial" used instead it when I specify FontFamily -> "Monaco". In the cases of "Courier" and "Consolas" everything works well on my system (Mathematica 7.0.1 for Windows). "Times" is not a monospaced font, of course.Condensation
@Alexey I see, perhaps it is only a problem on OS X then.Faubion
@Faubion Some users had already been repeatedly pointed out that WRI seems to perceive non-Windows users as second-class...Condensation
M
7

I never really worried about this before, since I only use inline (* -- *) comments for small comments or for commenting-out code. If I need a multiline comment, I'd split my code into smaller parts and use text cells.

That said, you could try using \[AlignmentMarker]s on the right-most *):

1 + 2 + 3 +     (* First three  \[AlignmentMarker]*)
 4 + 5 + 6 +    (* Next three   \[AlignmentMarker]*)
 7 + 8 + 9      (* Last three   \[AlignmentMarker]*)

Displays as

screenshot


Edit in response to comment:

If you have comments in between the code (without code to the left), then maybe put it in a GridBox (created using Ctrl-Enter and Ctrl-,) which is commented-out using (* ... *) in a white font.

GridBox

It's hard to give code to make the above... but here's the Box form:

Cell[BoxData[
 RowBox[{"Manipulate", "[", "\[IndentingNewLine]", 
  RowBox[{RowBox[{RowBox[{"a", " ", "=", " ", 
   RowBox[{"x", "+", "1"}]}], ";", "\[IndentingNewLine]", " ", 
    RowBox[{StyleBox["(*", FontColor->GrayLevel[1]], "", 
     GridBox[{{"(*", RowBox[{"Inter", "-", RowBox[{"code", Cell[""]}]}], "*)"},
              {"(*", "comments", "*)"}},
      GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, 
      "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}], " ", 
     StyleBox["*)", FontColor->GrayLevel[1]]}], "\[IndentingNewLine]", 
    RowBox[{"{", RowBox[{"a", ",", SuperscriptBox["a", "2"]}], "}"}]}], ",", 
   "\[IndentingNewLine]", 
   RowBox[{"{", RowBox[{"x", ",", "0", ",", "1"}], "}"}]}], 
  "]"}]], "Input"]
Mcnutt answered 15/6, 2011 at 9:54 Comment(3)
+1 The other solution is to realize that you'll not understand your Mma code three months later, no matter how many comments you wrote :)Shrivel
Thanks. But I forgot to mention that I can't split the cell and use text cells for comments. For demo, the style sheet to use only allows one cell for the Manipulate[] function and can't be split. I really need to make the comments separate on top, not to the right as your example. To make a comment on a large code block below it. thanksEllamaeellan
@Nasser: You can split your code up into the "Initialization Code" where you can use text cells. That can make the final Manipulate simple enough for just small comments...Mcnutt
C
2

I do not have such problem with my installation of Mathematica 7 since it uses monospaced font "Courier New" by default for the cells with style Input. But if for some reason a non-monospaced font is used in your installation by default you can redefine the style of comments inside Notebook for using a monospaced font:

SetOptions[EvaluationNotebook[], 
 AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Plain,
     FontColor -> GrayLevel[0.6`], ShowAutoStyles -> False, 
     ShowSyntaxStyles -> False, AutoNumberFormatting -> False, 
     FontFamily -> "Consolas"}}]
Condensation answered 15/6, 2011 at 12:7 Comment(9)
I added EDIT 1, showing the font used. I selected the cell first, then did Format->Font and got screen shot of result. It does say "Courier New" there? thanks,.Ellamaeellan
@Ellamaeellan When I evaluate the code given in my answer and then check the font used by the method that you use I get "Courier New" although in really "Consolas" is used as I see by eyes. So this method is not reliable. Instead try to select only the comment's contents (without (* and *)) and try then to check the font. I get "Consolas" in this case with my code.Condensation
I added the code you show above to the demonstration initialization code, and it did indeed fix the font for the comment so now I can align them with no problem. Thank you. Added EDIT 2 fyiEllamaeellan
so I entered (* *)( X X X X )( **) (with line breaks) in a cell, then in another cell I entered Manipulate[ SetOptions[EvaluationNotebook[], AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Plain, FontColor -> GrayLevel[0.6`], ShowAutoStyles -> False, ShowSyntaxStyles -> False, AutoNumberFormatting -> False, FontFamily -> "Monaco", FontSize [Rule] s}}]; s, {{s, 12}, 5, 20, 1}] and played with the slider. As I change the fontsize, the comments align/don't align randomly. Good thing I don't write comments :)Faubion
@Faubion "Monaco" is not a monospaced font.Condensation
@Alexey strange, I always thought it was. In any case, I tried Consolas, Monaco, Menlo, Courier, Times etc, and the behaviour persists (I realize that not all of these are monospaced). So perhaps the fontsize does have something to do with this after all.Faubion
@Faubion I am sorry, "Monaco" probably is monospaced but it is not installed in my system and "Arial" used instead it when I specify FontFamily -> "Monaco". In the cases of "Courier" and "Consolas" everything works well on my system (Mathematica 7.0.1 for Windows). "Times" is not a monospaced font, of course.Condensation
@Alexey I see, perhaps it is only a problem on OS X then.Faubion
@Faubion Some users had already been repeatedly pointed out that WRI seems to perceive non-Windows users as second-class...Condensation

© 2022 - 2024 — McMap. All rights reserved.