I have recently posted a question asking how we could create a palette for copying code in the format originally suggested by Sjoerd and since adopted by several people:

D[x^2, x]

(* ==> 2 x *)


==> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, \
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, \
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}

This format makes it very clear what is input and what is output, and it also makes it very easy to copy the code back into a Mathematica notebook in a single go.

The question has since borne fruit, as it received two great answers. A palette based on these answers is in the answer below.

Note: the purpose of this palette is simply to make it easier to use this style for those who want to follow it. This site does not have any policy about a style for distinguishing inputs from outputs in posts.

I don't really like that format myself. I feel it puts too many lines in the post yet looks cluttered. :-/ – Mr.Wizard Jan 26 '12 at 20:09
@Mr.Wizard The idea is that we shouldn't need to copy lines one-by-one. If the output is commented out, then one can copy in a single go. Personally I only use this when it is important that the output is shown (otherwise I prefer not to post the output at all). We can look into keeping single-line outputs on a single line (instead of three lines) – Szabolcs Jan 26 '12 at 20:14
@Mr.Wizard See my update. Also, what do you think about the single line idea? – Szabolcs Jan 26 '12 at 20:19
Do you mean (* ==> 2 x *) ? – Mr.Wizard Jan 26 '12 at 20:29
@Mr.Wizard Yes, I mean that. Also, if your concern is that this will become a pattern that people will use without thinking (the original reason is making it easier to paste code in a convenient-to-copy AND convenient-to-read way, and overuse will not help with this), then I think perhaps you are right. I'll put some explanations in the main post, and eventually the palette will include a little [?] button that'll bring up a documentation notebook which will also explain how and why to use this. – Szabolcs Jan 26 '12 at 21:28
That sounds good. If there is a reason to copy&paste then either form could be acceptable. I still like the quoted-code block for general output that is not likely to be copied along with the input. – Mr.Wizard Jan 26 '12 at 21:31

Thanks to Albert Retey and Rolf Mertig now we have an easy way to copy code from Mathematica. The source for the code copy palette is below (note that the core code is written by Albert Retey, not me).

Evaluate it to create the palette, pop it out from the notebook, and save it for permanent use with the Palettes -> Install Palette... menu item. (When you do this, older versions of the same palette will be overwritten.)

The latest version will always be available in this post.

The palette copies code already indented (for pasting as a code block), with output cells commented out.

Please test this palette, evaluate its usability, and post suggestions, ideas, bug reports in the comments below.

  Tooltip[Button["Copy for MSE",
  If[MemberQ[Hold[{}, $Failed, NotebookRead[$Failed]],


    FrontEndTokenExecute[SelectedNotebook[], "CopySpecial", "InputText"];
        Cell[BoxData[#], "Code", 
             Evaluatable -> False, InitializationCell -> False,
             Background -> White, CellMargins -> 10], 
        DockedCells -> Cell[BoxData@ToBoxes@Button["Copy all", 
              SelectionMove[EvaluationNotebook[], All, Notebook]; 
              FrontEndTokenExecute[SelectedNotebook[], "Copy"]], 
       WindowTitle -> "Code to copy (from " <> NotebookTools`NotebookName@SelectedNotebook[] <> ")",
       Saveable -> False] &, 

         Cell[c_String, ___] :> "    " <> 
               StringTrim@StringSplit[c, ("In" | "Out") ~~ "[" ~~ DigitCharacter .. ~~ "]"], 
               {StartOfString ~~ ":=" ~~ WhitespaceCharacter ~~ input__ :> input, 
                StartOfString ~~ "=" ~~ WhitespaceCharacter ~~ output__ :> 
                      If[StringFreeQ[output, "\n"],
                         "(* ==> " <> output <> " *)", 
                         "(*\n==> " <> output <> "\n*)"]
          {"\n" -> "\n    ",
           "\[Pi]" -> "Pi", "\[ExponentialE]" -> "E", 
           "\[ImaginaryI]" | "\[ImaginaryJ]" -> "I", 
           "\[UndirectedEdge]" -> "<->",
           "\[Infinity]" -> "Infinity"}], Infinity]]
    ], Appearance -> "Palette"],
  "Copy formatted for use on Mathematica.SE", TooltipDelay -> Automatic],  
  WindowTitle -> "Code Copy", Saveable -> False]
@Simon Why did you turn off windows resizing? :-( – Szabolcs Jan 27 '12 at 8:32
The reason I reformatted the code this way was to avoid the amount of line shuffling/breaking necessary when adding to the code. Now the built-in diff viewer should make it more clear what is being changed (as in the case of my last edit) – Szabolcs Jan 27 '12 at 9:21
Oops, sorry! I didn't notice I did that, I just wanted to get rid of the menu bar. – Simon Jan 27 '12 at 11:42
@Simon Oh, I see now. Windows only have a menu bar on Linux, not on Windows or Mac. – Szabolcs Jan 27 '12 at 11:44
@Szabolcs, I wonder, why don't you combine this with your neat upload-image-to-SE palette? – István Zachar Feb 16 '12 at 0:43
I think combining the two palettes would be a good idea. – Ajasja Mar 27 '12 at 10:54
@István and Ajasja I didn't really have the time lately to sit down and work on these. I'll consider it after Easter. – Szabolcs Mar 27 '12 at 11:07
@Szabolcs Ok, thanks! – Ajasja Mar 27 '12 at 11:30
This is great, very useful. I've noticed that using this on selections from the Documentation Center seems to crash Mathematica. (Anyone else experience this?) Not a tremendous problem though, just copy/paste into a normal notebook and it works. – billisphere Apr 3 '12 at 18:13
@Szabolcs Thanks for the palette. In my case 4 character indention at the beginning is missing. – Markus Roellig Sep 30 '12 at 14:32

You must log in to answer this question.

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