4
$\begingroup$

There are 3 kinds of line breaks

  1. \r
  2. \n
  3. \r\n

For windows, \r\n is standard.

Suppose that there is a file called document.txt, using \r or \r\n as line breaks.
Suppose that I open document.txt with a notepad program and select all text and Ctrl+C,
then create a string variable named mystring, in mathematica.

In[1]  mystring="paste, I mean ctrl+V here."

(note that the content of mystring is several lines.)

Then

In[2] StringCount[mystring, "\r"]
Out[2] 0

Looks like \r or \r\n are all replaced with \n automatically.

Q1) Does mathematica automatically change line breaks to \n?

It is a little surprising that mathematica, doesn't prohibit to use \r.

If you type '\r' with your fingers, when defining a string variable, it works.

Looks like unwanted line breaks change (every line break to \n) happen only when pasting clipboard content to mathematica.

Q2) Why does mathematica behave like this ? Is there an option to correct this phenomenon?

$\endgroup$

1 Answer 1

5
$\begingroup$

To test your case I'd done the following in Windows 10 with Mathematica 12.3:

Use Python to generate 3 files, each with a different form (\n, \r and \r\n):

with open('file1.txt','w',newline='') as f:
    f.write('sample\n')

with open('file2.txt','w',newline='') as f:
    f.write('sample\r')

with open('file3.txt','w',newline='') as f:
    f.write('sample\r\n')

I used Windows notepad and Notepad++, which both copied the exact same content that was written.

Also in Mathematica, if we read those file using ReadString, we'll see the original content:

ReadString["file1.txt"] // FullForm
(*Out: "sample\n" *)

ReadString["file2.txt"] // FullForm
(*Out: "sample\r" *)

ReadString["file3.txt"] // FullForm
(*Out: "sample\r\n" *)

After pasting in Mathematica, all forms (\n, \r and \r\n) becomes \n:

(* copied from file1.txt content*)
file1 = "sample
";

(* copied from file2.txt content*)
file2 = "sample
";

(* copied from file3.txt content*)
file3 = "sample
";


FullForm[file1]
(*Out: "sample\n" *)

FullForm[file2]
(*Out: "sample\n" *)

FullForm[file3]
(*Out: "sample\n" *)


file1 == file2 == file3
(*Out: True *)

Solution

We can change the paste function in the menu bar to insert the raw format like sample\r instead of sample with a new line. If you're on Windows, open MenuSetup.tr in the Mathematica_Directory\12.3\SystemFiles\FrontEnd\TextResources\Windows.

If you're using other languages in Mathematica, go into the language folder for example for Spanish the file exists in Mathematica_Directory\12.3\SystemFiles\FrontEnd\TextResources\Spanish\Windows

If you're a Mac user, we face some problems in this post. If you find a workaround, please comment it, so all of us could enjoy it.

In the file, search for paste to reach this line (Windows and Mac seem to have little difference):

MenuItem["&Paste", FrontEnd`Paste[Automatic], MenuKey["v", Modifiers->{"Control"}]],

Add this code which was inspired by @kglr post after the above line to have two methods of pasting in Mathematica:

MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringReplace[StringTake[RunProcess[{"powershell", "(get-clipboard -raw) -replace '\\r','\\r' -replace '\\n','\\n' -replace '\\t','\\t'"}, "StandardOutput"], {1, -3}], {RegularExpression["(?<!\\\\)\n"] -> "\\n", RegularExpression["(?<!\\\\)\r"] -> "\\r", RegularExpression["(?<!\\\\)\t"] -> "\\t"}]]],MenuEvaluator -> Automatic],

Save the file, restart the Mathematica and use the new Paste 2:

enter image description here

Because Paste 2 runs a PowerShell code to get the clipboard content, it's a little bit slow.

$\endgroup$
6
  • $\begingroup$ Excellent! But, not solved completely ; In GIF file, you are testing with file3.txt. It has content 'sample\r\r\n' that is, double \r followed by single \n. The result of excellent paste2 feature, is 'sample\r\n' that is, single \r followed by single \n. They are different. $\endgroup$
    – imida k
    Commented Jun 20, 2021 at 13:05
  • $\begingroup$ @imidak file3.txt contains sample\r\n, when we used Mathematica ReadString, it replace \n with \r\n, so it shows the content as sample\r\r\n. In the GIF we copied from Notepad++ and it copied the original content we'd put. If you want to Paste 2 behaves like ReadString, we could wrap the ToString by StringReplace[ToString[..., "\n" -> "\r\n"]. $\endgroup$
    – Ben Izd
    Commented Jun 20, 2021 at 13:43
  • $\begingroup$ file1.txt : 73 61 6D 70 6C 65 0A file2.txt : 73 61 6D 70 6C 65 0D file3.txt : 73 61 6D 70 6C 65 0D 0A Is it correct when opened in hex editor? $\endgroup$
    – imida k
    Commented Jun 20, 2021 at 14:22
  • $\begingroup$ I use windows 10 x64bit, mathematica is v 12.2.0.0. If the hex codes of three files are correct, the very first part of the thing I can't agree with your answer is 'ReadString, it will replace \n with \r\n'. Because on my PC, Readstring just gives the exact content of files. $\endgroup$
    – imida k
    Commented Jun 20, 2021 at 14:35
  • 1
    $\begingroup$ @imidak Yes, you're right, it's not 3 lines. I've checked the hex codes and find out that python didn't write those files correctly. newline='' needs to be added. The answer is now updated. $\endgroup$
    – Ben Izd
    Commented Jun 22, 2021 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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