开发者

how to align comments in Mathematica notebook?

开发者 https://www.devze.com 2023-03-12 22:08 出处:网络
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 b

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:

how to align comments in Mathematica notebook?

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

how to align comments in Mathematica notebook?

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

how to align comments in Mathematica notebook?

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

how to align comments in Mathematica notebook?

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 :)

how to align comments in Mathematica notebook?


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

how to align comments in Mathematica notebook?


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.

how to align comments in Mathematica notebook?

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号