1

Copy and paste the following two lines into Mathematica.

1 \[Alpha] \[NewLine] 2
f[ 1 \[Alpha] \[NewLine] 2]

The first posts \[NewLine] while the second doesn't. This doesn't duplicate the behavior as if you would have typed the same expression.

This should definitely be a bug IMO because it is both not displayed or interpreted. The same holds true for \[RawReturn] and \[IndentingNewLine]

Cell[BoxData[{
 RowBox[{"1", " ", "\[Alpha]"}], "\[NewLine]", "2", "\n", 
 RowBox[{"f", "[", 
  RowBox[{"1", " ", "\[Alpha]", " ", "2"}], "]"}]}], "Input"]

It doesn't copy the same behavior as if you would have typed the expression straight into Mathematica.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
William
  • 7,595
  • 2
  • 22
  • 70
  • I don't think you can call it a bug unless you can point to where the docs say what to expect when you stick a non printing special character in the middle of an expression. – george2079 Sep 03 '15 at 15:00
  • @george2079 Does bribery work? I'll give rep points if I can mark it as a bug. :) Just Kidding. Although you are right it isn't documented but at the same time or don't think it should be the intended behavior. They are both multiplication \[Alpha] \[IndentingNewLine] working but not inside a function is inconsistent. – William Sep 03 '15 at 15:02
  • @george2079 Each character has a name and a number of shortcut aliases. They are fully supported by the standard Wolfram System fonts. It doesn't matter to me if it displayed, but it should be interpreted. Check BoxData post above. – William Sep 03 '15 at 15:13
  • 1
    @george2079 There should be an inconsistent-bug tag. I'm not sure about your original example it is debatable if Symbol@"\[IndentingNewLine]" should work. – William Sep 03 '15 at 15:46
  • Ok, I see what you are saying. Its worth pointing out a regular "\[Newline]" works fine (displaying a line break within your function argument). ref: http://reference.wolfram.com/language/ref/character/IndentingNewLine.html – george2079 Sep 03 '15 at 15:46
  • @george2079 Not in version 10 1 \[Alpha] \[NewLine] 2 f[ 1 \[Alpha] \[NewLine] 2] – William Sep 03 '15 at 15:52
  • Now I see, this is all about pasting in not being consistent with what you get if you actually type it. Perhaps a little work-around, it seems if you type an opening quote ", then paste your special character is preserved (and you can delete the quote to end up with what you are after ) – george2079 Sep 03 '15 at 16:00

1 Answers1

1

It isn't a bug but probably the intended behavior of automatic code formatter for "Input" Cells. Such Cells have AutoSpacing->True by default and so aren't intended to hold newlines, spaces etc. appearing in a string which you paste into them. If you wish to hold original appearance of the code you should use "Code" Cells (Alt+8) which has AutoSpacing->False or you could create custom stylesheet where "Input" Cells will have AutoSpacing->False by default.

When pasting your code into a "Code" Cell everything works as you expected:

screenshot

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • My issue is that if you take a valid box form of the data and type it in exactly it produces a different result then if you paste the same data into the cell. NewLine characters appear to be the only that have this happen. They are very much intended to hold new lines. It has been patched so paste works properly http://mathematica.stackexchange.com/questions/30604/paste-data-into-mathematica-with-formatting I'm ok with the \n but \[RawReturn] and \[IndentingNewLine] and \[NewLine] should not all be stripped. They are different characters. – William Feb 25 '16 at 06:59
  • When you paste a string into an "Input" cell it is parsed having AutoSpacing->True "in mind". As I showed in my answer, it is sufficient to set AutoSpacing->False (even for "Input" cell) to prevent this. Try to add this option at the end of FullForm (Ctrl+E) of "Input" cell and then paste your code. You will see, it works! – Alexey Popkov Feb 25 '16 at 07:04
  • I was not aware of this it definitely deserves a plus one. I'm surprised no body mentioned this before because it definitely works very similar to the other question. The one downside is it still doesn't distinguish between \[RawReturn] and \[IndentingNewLine] and \[NewLine]. After seeing this I kinda wish there was a hybrid AutoSpaces interpret NewLines but not spaces. That is essentially what the other question does. – William Feb 25 '16 at 07:16
  • For what actually do you need \[NewLine], \[IndentingNewLine] and \[RawReturn] to be parsed differently when you paste a string? And in what way? With AutoSpacing->False they are all preserved and I see this as expected and correct behavior. – Alexey Popkov Feb 25 '16 at 07:27
  • I'm fine with this not being marked as a bug although I still would still personally like to distinguish between them. Here is a very similar although slightly simpler example. If you export Mathematica code it wraps at lets say 80 characters...great. Well If you plan to reimport the code it is quit difficult to determine the original formatting because additional line breaks have been added for wrapping. Now if you wrap at 80 characters but include the \[LineBreak] and distinguish between the two you can essentially go back to the formatted code. Let me know if that doesn't make sense. – William Feb 25 '16 at 07:42
  • Allows for a readable simplified code only BoxData in some regards. – William Feb 25 '16 at 07:45
  • Do you mean exporting Mathematica code into an .m file (a Package)? If so, you could just open this .m file directly in Mathematica and all the code will appear as it was in .m file because all cells will have style "Code" with AutoSpacing->False. – Alexey Popkov Feb 25 '16 at 07:46
  • My intent was a pdf that has human readable runnable mathematica code that is copy and pastable into Mathematica that preserves the linebreaks of the original code. You could use BoxData but isn't human readable. Adding \[LineBreak] to my code and changing to AutoSpacing on would be nice but doesn't work. I had a couple different intents honestly. EDIT: My Mathematica code was longer then pdfs size. Therefore it was not having the same formatting as the original code even with your AutoSpacing option and interpreting all linebreaks as \n. – William Feb 25 '16 at 07:51
  • Then AutoSpacing->False should be sufficient for you. If you are concerned with excessive space characters which can appear when you copy code from a PDF reader, you should publish your code in PDF without extra spaces what you can do by copying it as "Plain Text" (Ctrl+Shift+C) from Notebook. In this case however you will have to add the line breaks manually. Another option is to copy as usual, then manually remove all space characters (you can even create a Palette for this action in Mathematica). – Alexey Popkov Feb 25 '16 at 07:56
  • If you need to change the PageWidth of the copied code, this answer shows you how you can do it. – Alexey Popkov Feb 25 '16 at 08:02
  • I was aware of the PageWith option. Interesting how would I do something without the extra spaces and line breaks in html as an example? I'm still not convinced I can do an 80 character word wrap and have linebreaks only fall in mathematica where I choose. I'm trying to do something like this Print[\n a \\[LineBreak] b \n] where th e pdf interpets the \n characters and mathematica interprets the \[LineBreak] character so they essentially get different spacing. – William Feb 25 '16 at 08:12
  • What you are saying might theoretically be possible although it would be a lot easier if mathematica could distinguish between the 2 – William Feb 25 '16 at 08:18
  • There is no \[LineBreak] character in Mathematica. And you shouldn't publish code with verbatim \[NewLine], \[IndentingNewLine] or \[RawReturn] because they make the code unreadable. They are low-level structural elements which normally shouldn't be needed for a user at all. When publishing code you should always use simple line breaks. – Alexey Popkov Feb 25 '16 at 08:20
  • I understand what you are saying but they are different unicode characters they should not be interpreted the same regardless if I use \[NewLine].... If we were discussing \[Alpha] and \[Beta] we would be in agreement. – William Feb 25 '16 at 08:23
  • Are you publishing a code or a text with special Unicode characters? For code you need only line breaks, nothing more. If you are trying to do some non-standard formatting for Unicode text, it is completely another question and probably off-topic here. – Alexey Popkov Feb 25 '16 at 08:27