21
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ I have used Mathematica 8,9,10 on Linux, Windows, Mac and I have never seen this! ;) $\endgroup$ – Mahdi Apr 10 '15 at 3:23
  • $\begingroup$ 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. $\endgroup$ – Jens Apr 10 '15 at 5:22
  • $\begingroup$ Doesn't happen for me on 10.0.2 or 10.1 on Mavericks. $\endgroup$ – Gordon Coale Apr 10 '15 at 5:41
  • 1
    $\begingroup$ 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. $\endgroup$ – Shredderroy Apr 10 '15 at 20:06
  • 1
    $\begingroup$ 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. $\endgroup$ – Michael E2 Apr 11 '15 at 3:32
9
$\begingroup$

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:

option inspector linebreak options

Solution 2

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

enter image description here

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}

From the docs of LinebreakAdjustments:

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"],
LinebreakAdjustments->{0.5, 10, 100, 1, 0.5}]

change stylesheet

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Nasser Apr 10 '15 at 19:07
  • $\begingroup$ @Nasser see my new solution based on Stylesheets and LinebreakAdjustments $\endgroup$ – sacratus Apr 10 '15 at 19:50
4
$\begingroup$

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

enter image description here

Gets even stranger:

enter image description here

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Ralph Dratman Apr 11 '15 at 1:20
  • $\begingroup$ 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. $\endgroup$ – Ralph Dratman Apr 12 '15 at 16:14
1
$\begingroup$

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

enter image description here

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:

enter image description here

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.

| improve this answer | |
$\endgroup$
1
$\begingroup$

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

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

Instead of:

(* Text; Text, Text *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you! Is there a known logical reason why the extra parens eliminate the problem? $\endgroup$ – Ralph Dratman Sep 24 '16 at 2:44
  • $\begingroup$ Parens like that don't work for me but quotes do! $\endgroup$ – dreeves Feb 13 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.