# Why does a comma insert a line break in a comment?

I like to include capital letters and punctuation in my comments. But when I put a comma into a Mathematica comment of the (* ... *) type, it usually causes the comment line to break, with a new line starting after the comma. I find this both annoying and time-consuming. Is this behavior desirable in some context? More importantly, can I turn it off?

Here is an example. Copy the two lines below and paste them into a new, empty Mathematica document.

(* Complement of dihedral angles of a regular *)
(* icosahedron in degrees. *)


After the above text has been pasted in, insert a comma after the word "icosahedron". In my setup, this breaks the line in two.

I am using Mathematica 10.0.2.0 under the Mavericks version of Mac OS. I will be happy to provide more details of my setup if needed, but as far as I know, there is nothing special in this installation. I have also seen this happen on other versions of Mathematica.

• I have used Mathematica 8,9,10 on Linux, Windows, Mac and I have never seen this! ;) – Mahdi Apr 10 '15 at 3:23
• I see this too. Also happens with semicolon. No idea why anyone would deliberately want this behavior... I guess you could work around it by opening an inline cell with Ctrl-9 and typing the comma in that. – Jens Apr 10 '15 at 5:22
• Doesn't happen for me on 10.0.2 or 10.1 on Mavericks. – Gordon Coale Apr 10 '15 at 5:41
• I just checked, and it happens to me in both 10.0.2 and 10.1. It appears to be a function of the width of the notebook window. When I maximise the window, everything becomes aligned in a single line. – Shredderroy Apr 10 '15 at 20:06
• The formatting of comments has always been irritating to me. I can imagine two classes of comments, explanation of the code and inactivated code blocks. All comments seem to format as inactivated code blocks. But it's been going on for so long, I just say "whatever" and move on. – Michael E2 Apr 11 '15 at 3:32

I can not really explain why this is happening, but I can offer you 3 solutions that will stop this.

Solution 1

You can manually turn off automatic linepraking in the option inspector:

Solution 2

Another aproach to prevent this is to use a \[NonBreakingSpace] right after the comma character: ",\[NonBreakingSpace]". Type it in with EscnbsEsc.

I would prefer the second solution, because generally turning of the linebreaking might not be such a good idea.

Solution 3

Since typing in EscnbsEsc is indeed not very practical, you can use this setting for all input cells:

LinebreakAdjustments->{0.5, 10, 100, 1, 0.5}


The default setting LinebreakAdjustments->{0.85, 2, 12, 1, 1} represents an empirical choice that works well for most Mathematica input. An alternative setting that gives good results for program code is LinebreakAdjustments->{0.5, 10, 100, 1, 0.5}.

It can be set globally in the Option Inspector right below "LineBreakWithin", but I would suggest to edit the default stylesheet and add this option for all input cells.

How to: Edit a Stylesheet (see last edit for quick solution)

Open a fresh notebook. Go to Format -> Edit Stylesheet...

Add a new cell to the stylesheet notebook. Chose Cell -> Show Expression, replace the cell-content with:

Cell[StyleData["Input"],


This will add the modificated linebreak adjustment to the default options of any input cells.

Go to Install Stylesheet... and type in an instal name e.g. NonBreakingDefault.

From now on you can chose this stylesheet from the menu Format -> StyleSheet. You can also set this stylesheet as the default stylesheet wich is loaded, when you open a new notebook. You can do this also in the option inspector: Global Options -> File Locations -> DefaultStyleDefinitions. Chose the stylesheet. You will find it in FileNameJoin[{\$UserBaseDirectory, "SystemFiles", "FrontEnd", "StyleSheets"}]

EDIT2:

This code will automatically create the needed stylesheet for you:

CreateDocument[
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"],
LinebreakAdjustments -> {0.5, 10, 100, 1, 0.5}]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb",
WindowTitle -> "NonBreakingDefault"]]


Just evaluate the cell and after it opens, you can instal the stylesheet.

• yes, turning off line breaking, will make big mess of the code formatting? is it like writing in a code cell basically, instead of input cell. I think. Never tried it. But the second option you show is not really practical. Having to keep pressing ESCnbsESC each time one wants to make a , or ;. These are comments, and they should just be written freely and as is. This problem should be fixed at the source by WRI. – Nasser Apr 10 '15 at 19:07
• @Nasser see my new solution based on Stylesheets and LinebreakAdjustments – sacratus Apr 10 '15 at 19:50

Working on figuring out cause, but here's an example:

Gets even stranger:

Turning off word-wrap seems to remedy these... as does using (*" for start of a comment.

• Thank you for your additional experiments. My main question was supposed to be: is there a logical reason for this behavior? I would think every character ought to be neutral and, so to say, meaningless inside a comment. Is this line-wrapping behavior intended -- or is it a misplaced feature -- or even an outright bug? – Ralph Dratman Apr 11 '15 at 1:20
• sacratus, considering your observation about (*" maybe the solution is for the front end's word-wrap code to treat comment text as if it were literal string input. – Ralph Dratman Apr 12 '15 at 16:14

@sacratus has provided solutions for this. So I'll just provide my version of explanation here.

I guess the reason for this behavior is that the commented part is actually treated as code. In other word, the (**) comment is intended for commenting out code, not for arbitrary text.

Hence a "comma" is not a usual textual comma, it still is the separator between list elements. And the text before and after comma is treated as list element. Hence line is breaked for increased clarity when editing (potentially) long list elements. Note that the behavior is the same if you remove the (**) comment symbols, like so:

As you may have seen, if the part of line after the comma can fit in the original line, it will go eventually back to where it was after you hit <Enter> at the end of the line (which signifies your editing of the list element is finished). See the following screenshot:

This is exactly the same behavior when you editing a list consisting of several long elements.

By the way, I think Wolfram may want arbitrary text comments to be put into separate text cells, as part of notebook document flow that they imagined.

A much easier solution is to simply wrap your comment in parentheses. Use:

(*( Text; Text, Text. )*)


(* Text; Text, Text *)