What are best practices for including sections of Mathematica code in posts?

Mathematica differs from many programming languages because many of us use it in "REPL" (read-evaluate-print-line) mode, so output is interspersed with input, making it hard to copy and paste code. Are there any tricks to ameliorate this?

share
3  
Could we make a mode that would accommodate the REPL style of the Mathematica notebook? When you highlight cells and right click, you get a "Copy As" option, could we leverage the options it has in that submenu for display on the site? –  Tobi Lehman Jan 17 '12 at 21:52
add comment

4 Answers

Copying output from Mathematica can be tricky at times. Here are some tips on making it easier:

Copying:

  • Use Copy As -> Plain Text, or Copy As -> Input Text from the context menu to avoid copying (not really human readable) box expressions.

  • If you copy only input cells, the In/Out labels will not be copied. To select all input cells in a notebook, Alt-Click an input cell bracket. (You can copy the relevant section to a new notebook before selecting input cells.)

  • Whenever possible, make sure that the code posted can be copied back to Mathematica and evaluated. But in some special cases it's good to show the formatted expression too, as a screenshot. The image uploader palette makes this very easy.

Unfortunately formatting (indentation and newlines) will get lost when the code is copied. I do not know if there's a good solution for this (I'd be very interested!)

Pasting:

  • When pasting back code to Mathematica, it'll get pasted into a single cell. Cell ->Divide Cell (CTRL-SHIFT-D) is a convenient way to break it into parts again.

A note about In/Out labels:

When I post code to be copied as a whole, I always avoid In/Out labels, as this is inconvenient. But when I'm showing the process of doing something, together with the output, and commenting on each step, I prefer to keep In/Out labels to make it very clear what is the input and what is the output.

share
2  
I use the Code style (Alt/Cmd-8) for input cells. That way, the (manually applied) formatting is retained by copy-and-paste. –  WReach Jan 22 '12 at 17:52
    
How about extending the super-unbelievable-image-upload-palette for copying to the clipboard the selected input and output cells and comment out the output ones as in @Sjoerd's answer? –  belisarius Aug 15 '12 at 7:41
add comment

I usually edit away any In/Out labels that may be left during copying. I then comment out the output

So

In[108]:= D[Cos[x] Exp[x], x]

Out[108]= E^x Cos[x] - E^x Sin[x]

becomes:

D[Cos[x] Exp[x], x]

(*  
===> E^x Cos[x] - E^x Sin[x]
*)
share
    
I have not done this before, but I think this is a good pattern to follow! –  Szabolcs Jan 19 '12 at 18:08
    
I tried to do this, but I think I'm just too lazy. What about automating it? I'll try to do it eventually, but if anyone can do it now, please implement it and post back! –  Szabolcs Jan 22 '12 at 15:15
    
@Szabolcs Perhaps post this as a question? –  Sjoerd C. de Vries Jan 22 '12 at 15:24
    
OK, hope people won't get mad at me for a "give me the code" question :-) –  Szabolcs Jan 22 '12 at 15:29
    
@Szabolcs It might be interesting. I see two lines of attack: 1) Modify output printing (using $Post or so) or the Output form. 2) At Cell level, modifying cell contents. –  Sjoerd C. de Vries Jan 22 '12 at 15:59
    
I would first copy the selection to a new, invisible notebook, then modify the output cells, then copy the whole thing as "plain text", then close the notebook (all of this done programmatically, of course). –  Szabolcs Jan 22 '12 at 16:07
    
    
@Szabolcs and any graphics in the selection should be uploaded to SE of course ;-) you have done it before... –  Sjoerd C. de Vries Jan 22 '12 at 16:19
add comment

I have taken to using a combination of QUOTE and CODE blocks. That is > followed by five spaces before each line. It's quick to type by hand, and I think it both visually and logically carries the meaning of output.

Range@9
{1, 2, 3, 4, 5, 6, 7, 8, 9}
share
1  
And is easier to hit the shortcut for quote and code than typing (* and *). Heck, I don't even type that thing anymore in Mathematica, as there is Alt-/! –  István Zachar May 3 '12 at 10:20
add comment

I find Sjoerd's way of presenting code a bit bloated.

Why not simply use >> for input and << for output?

>> (*If comments are part of the pasted code, there will be no confusion*)
   Print["as to what's a comment and what's code (not that it was such a big problem...)"];

<< "as to what's a comment and what's code (not that it was such a big problem...)"

Because the code block is not executable, since << and >> are Mathematica functions.

share
    
The whole point of going through the trouble of stripping out the In/Out labels and commenting the output was so that the whole code block can be copied and run in mma without errors. Although I dislike a lot of the other options, they adhere to this important requirement. Yours, complicates things in several ways. In addition to this block being non-executable, >> and << mean specific functions in Mathematica, namely Put and Get. You can see that this is clearly unpopular. I urge you to delete this and remove these from your recent answers where you've been trying to popularize it. –  rm -rf Aug 15 '12 at 16:17
    
I understand. I don't see the urgency to remove this post, though. I've edited it. I'll edit my other answers. –  CHM Aug 16 '12 at 23:07
    
Sorry, I should've just said remove that from your answers. I had "delete this from your answers", which I changed to "remove these from your answers", but forgot to delete the earlier sentence. I can't edit it now to correct it, but please leave this post as is :) –  rm -rf Aug 16 '12 at 23:15
    
No problem, I see that you've already edited the troublesome answer. I'm out of work. –  CHM Aug 16 '12 at 23:19
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .