7
votes

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

2
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? - acl
@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. thanks - Nasser
I just tried it with that stylesheet and it aligns just fine here (OS X). Don't know what else to suggest. - acl
@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... - Simon
@Nasser 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 X - acl

2 Answers

7
votes

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"]
2
votes

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"}}]