Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Known issues with copying code from Mathematica to other platforms?

I just noticed that if you have this in MMA (8.0.1 / win7-64):

enter image description here

and you copy it to SO (just ctrl-c ctrl-v), you get this:

(maxY - minY)/stepy/(maxX - minX)/stepx  

which is not mathematically equivalent. It should be this:

((maxY - minY)/stepy)/((maxX - minX)/stepx)

or this (the InputForm of the above):

((maxY - minY)*stepx)/((maxX - minX)*stepy)

It's not caused by StackOverflow's internals as the same happens with a copy to NotePad.

Are there more issues like this (especially when working with SO, but also in general) that we should be aware of?

What causes this, can it be fixed on our side, and if not, what is the best work-around?

like image 516
Sjoerd C. de Vries Avatar asked Apr 15 '11 12:04

Sjoerd C. de Vries


People also ask

Can I copy code from stack overflow?

Is it OK to copy code from StackOverflow? Copying code from Stack Overflow is not a problem. But the creative commons licensing used by the platform requires you to credit the code you take from the original author.

What can I do with Mathematica?

Building on three decades of development, Mathematica excels across all areas of technical computing—including neural networks, machine learning, image processing, geometry, data science, visualizations and much more.


2 Answers

Copying Mathematica code to the Usenet Mathematica group sometimes scatters all kinds of weird characters throughout your code (you never knew when it would happen to you). The workaround would be either:

  1. Cell > Convert To > InputForm and then do a copy

  2. Edit > Copy As > Plain Text or Input Text

The latter doesn't work in this case. It's still a mathematically incorrect conversion that you'll get. The former does seem to work.

Update
If you ever encounter strange characters in posts in the Mathematica newsgroup (e.g., as in here) the following function will decode it (in all the cases that I tested).

translateQuotedPrintable[str_String] := 
  StringReplace[str, {"=" ~~ c1:HexadecimalCharacter~~c2:HexadecimalCharacter :> 
    FromCharacterCode[FromDigits[c1 <> c2, 16], "Math1"],"=" ~~ EndOfLine -> ""}]

Just paste the whole posting between quotes in translateQuotedPrintable[" ... "] and the post will be cleaned up.

For the curious: the encoding that you may see in these usenet postings is called Quoted Printable. It is used to convert character encodings that differ from the standard 7-bit ASCII set to ASCII. Many common MMA symbols, like the Rule symbol, are encoded this way if not previously converted to ASCII by InputForm cs. To decode a code one needs to know the original character set because QP only yields the hexadecimal position in the character set. In MMA's case this will most commonly be Math1.

like image 77
Sjoerd C. de Vries Avatar answered Sep 20 '22 17:09

Sjoerd C. de Vries


Copying the example to e-mail ALSO fails, and that -- if it carries over to other examples -- seems very bad for Mathgroup, SO, and any other text-based discussion forum. Copy As>Plain Text and Copy As>Input Form both work properly, but I've done it the easy way thousands of times, and never knew it could fail this way, WITHOUT strange hexadecimal characters.

like image 30
Bobby Treat Avatar answered Sep 20 '22 17:09

Bobby Treat