Code samples are easy enough to recognise, but I wonder if there is a more convenient way to copy all contents of a gray sample box into the clipboard for pasting into Mathematica.

Right now I select all content in a given cell with the mouse (which can get fiddly for long scrollable cells)...

... but I´d very much like a StackExchange interface element/widget that either marks all content or - even better - pastes this into the clipboard.

Something like in this, where the blue circle can be clicked and marks or copies the contents:

Mathematica graphics

For direct import of whole answers into Mathematica, a number of nifty Mathematica-based solutions were already proposed, but I find myself doing the whole paste&copy thing over and over, not least because sometimes you only want to try out a code segment and not the whole thing.

  • 1
    You've got my vote. More than once I've tried to run a copied section of code only to see the syntax error bracket light up. The cause? I missed the last character or two of the code box.
    – Mr.Wizard
    Commented Aug 22, 2012 at 12:48
  • I cannot imagine this has not been done at some StackExchange site already, especially for interpreter-type environments...
    – Yves Klett
    Commented Aug 22, 2012 at 12:51
  • 1
    Yeah, I think this would be a very good feature to have. A lot of programming blogs have this and it's very easy to copy code by clicking a + sign or something. This can also remain hidden unless hovered over, so that normal viewing experience isn't altered.
    – rm -rf Mod
    Commented Aug 22, 2012 at 13:00
  • You might even make it hierarchic and have another button on top for all code samples in a selected question/answer or even (carpe diem!) the whole thread.
    – Yves Klett
    Commented Aug 22, 2012 at 13:03
  • 2
    The most likely reason why it was never implemented before is that Stack Overflow and Code Review are the only other sites that come close to us in volume of code used in Q&A. In nearly all other sites, a code block is only incidental. Given the volume of traffic that Stack Overflow has, this little addition might result in a drop in performance and hence was not considered. However, on our site it shouldn't be an issue at all.
    – rm -rf Mod
    Commented Aug 22, 2012 at 13:06
  • Will the feature-request tag trigger some SE attention (perhaps together with sufficient upvotes)?
    – Yves Klett
    Commented Aug 22, 2012 at 13:09
  • 1
    Probably... I'm sure they look at it, but where it lies on their priority list, I do not know. Perhaps after a week has passed, we can ping/pester the devs in chat... Szabolcs' constant badgering and followup with the devs is how we got our custom syntax highlighter installed as quickly as we did (in a couple of months)
    – rm -rf Mod
    Commented Aug 22, 2012 at 13:19
  • 1
  • 1
    @SjoerdC.deVries The first is unrelated. In fact, the 'less related' one is more related. This is a request to facilitate copying from SE to Mma, not the other way round
    – rm -rf Mod
    Commented Aug 22, 2012 at 15:54
  • @r.m I meant to mention this one, but got mixed up. I see now that there's fresh activity going on there, so this is a bit superfluous. Commented Aug 22, 2012 at 21:59
  • @R.M do you have a suitable link to a site that has this feature? - if yes, please augment the question...
    – Yves Klett
    Commented Aug 24, 2012 at 4:59

4 Answers 4


Well, I have come up with something slightly different. Please tell me what you think about it and I'll elaborate on it.

The basic idea is the following. Below you see a picture with some code in it (a piece of mr.wizard's code from here). It's apparently an inert picture and normally you couldn't do much with it, but in this case there is something special going on. If you paste its hyperlink in the following Mathematica code (I've done that already in this example) you will get a cell with the code itself, ready to be executed.

picLink = "https://i.sstatic.net/79TZu.gif"; 
importImage = Import[picLink, { "ImageList", 2}];
imdata = Flatten@ImageData[importImage, "Byte"][[All, All, 1]];
imdata = imdata[[3 ;; imdata[[1 ;; 2]].{1, 256} + 2]];
CellPrint@Uncompress[StringJoin @@ FromCharacterCode@imdata]

Of course, this can be easily wrapped into a palette. In this example, picture and code are the same, but that isn't really necessary.

enter image description here

Another useful application of this technique is that you can abuse it to add attachments to posts. As an example: Executing the following single line of Mathematica code will give you a demo notebook of 31 pages (solely contained in the bar picture below):

    StringJoin @@ FromCharacterCode@#[[3 ;; #[[1 ;; 2]].{1, 256} + 2]] &[
         "Byte"][[All, All, 1]]]], "NB"];

enter image description here hairline

  • 2
    How would this be useful? Anyone who doesn't know about the existence of a palette to decode it will be stuck with an image. This makes things worse for everyone because now they can't even copy the code, whereas earlier it was only slightly inconvenient
    – rm -rf Mod
    Commented Aug 25, 2012 at 23:35
  • 1
    I have to say that it is a very clever idea/implementation, but, IMO not practical at all. Besides @R.M. comment I can also add that search engines are not capable of seeing it. Also, from time to time, the image flashes its second layer (at least on Chrome 21), which is annoying...
    – P. Fonseca
    Commented Aug 25, 2012 at 23:58
  • @P.Fonseca I saw that too on safari! I thought that it was just my screen refreshing or a poor connection...
    – rm -rf Mod
    Commented Aug 26, 2012 at 0:01
  • You would also need to consider compatibility issues between MMA versions (image processing capabilities on older MMA versions...).
    – P. Fonseca
    Commented Aug 26, 2012 at 0:08
  • 1
    @R.M. actualy, the gif has two frames, one that lasts 169.6 seconds, and another (the info frame), that lasts for 1 s. I'm almost sure this flashy thing could easily be resolved with a different gif config (or even simpler, encode it on a white vs almost white first lines of pixels of the visible layer...), but still invisible to search engines.
    – P. Fonseca
    Commented Aug 26, 2012 at 0:12
  • 4
    Notwithstanding the problems mentioned above, you deserve a big +1 for a fresh and laterally thought idea Commented Aug 26, 2012 at 4:28
  • @R.M The flash thing can be easily resolved. I picked two values for frame durations (10000 and 1) with which I can play (I put it on 65535 and o now). About usefulness: the current example is just that, you have to think beyond that. What about an unobtrusive figure at the bottom of the page, that contains all the code of the question? Those not in the know can still pick up the code in the boxes as before, the others can fire up their palette. Commented Aug 26, 2012 at 5:57
  • 1
    @SjoerdC.deVries I like the idea for other reasons, not for the purposes of this question. I did think about something like this at one point... my idea was to encapsulate all the code in a QR image, for example. That was about when I boldly claimed that I was going to make a QR encoder/decoder in mma. I have the specs and the docs downloaded and half read, but haven't found the time to implement it yet (actually, I forgot about it...) Now that I've reminded myself of it, I should consider getting back to it
    – rm -rf Mod
    Commented Aug 26, 2012 at 6:07
  • @R.M w|a has QR capability. I put some code somewhere in an answer. Will try to find it. Commented Aug 26, 2012 at 6:10
  • 1
    @R.M found it: stackoverflow.com/questions/5592819/… Commented Aug 26, 2012 at 6:13
  • @SjoerdC.deVries That's neat! I did not know W|A could do QR. This will be useful
    – rm -rf Mod
    Commented Aug 26, 2012 at 6:19
  • @r.m. However, a QR has less data density/pixel than a gif like above, because it's designed for optical pickup. That's fine if youre going to print it, but is just overhead in digital media. Commented Aug 26, 2012 at 6:20
  • True... in any case, from an effort point of view, I don't see any difference in this (as a palette) and using the API. In fact, the solution in my answer dumps the code with a different cell for each code block. So one can simply evaluate the appropriate code block and not the others, which is one way of achieving Yves' goal.
    – rm -rf Mod
    Commented Aug 26, 2012 at 6:25
  • 3
    I love the ingenuity of this idea - it's very cool. Reluctantly downvoting it because I don't want this to be the solution. A solution should work on all devices with standard web browsers with or without Mathematica installed.
    – cormullion
    Commented Aug 27, 2012 at 9:46
  • 2
    A few possibilities spring to mind. I can't be the only one that doesn't have Mathematica installed on every computing device I use. Suppose I'm on another computer, or at work without access to a Mathematica license, or at a friend's house, or on an iPhone or iPad, or wish to save code temporarily in Dropbox or an email or text message or save it in a scrap book like Evernote or into a blog post ... eventually perhaps I will need Mathematica, but not necessarily immediately...
    – cormullion
    Commented Aug 27, 2012 at 14:07

Just a test we are doing with Sjoerd. Will delete it later

Sjoerd: try this:

(* Generate encoding image for THIS*)
i1 = Image[{Partition[#, 3, 3, 1, 0] &@
     ToCharacterCode@Compress@ExportString[NotebookGet@InputNotebook[], "NB"]/256}];
(*Clone Notebook *)
                                   Flatten@ImageData[i1, "Byte"] , # != 0 &], "NB"]

Here is the (Framed) image :)

Mathematica graphics

Verde: this can be much shorter. In the 1D case, Partition doesn't need to pad, and you don't have to test for padding. I also don't like the division. Let's keep it in bytes.

(*Generate encoding image for THIS*)
i1 = Image[{Partition[#, 3, 3] &@
     ToCharacterCode@Compress@ExportString[NotebookGet@InputNotebook[], "NB"]},"Byte"];
(*Clone Notebook*)
 ImportString[Uncompress@FromCharacterCode@Flatten@ImageData[i1, "Byte"], "NB"]

Saving as PNG instead of required now.

  • see update above Commented Aug 28, 2012 at 17:32
  • @SjoerdC.deVries The partition mod works, but I don't undesrstand why. It chops zero, one or two bytes at the end of the string ... Commented Aug 29, 2012 at 12:15
  • @SjoerdC.deVries Now, here you have a nice application for something like this. The OP cites a certain (large) piece of code from another answer, but the cite is somewhat vague (it takes a few minutes to understand). Instead, he could cite the answer and post an image representing a notebook with the proper code. That way you save some time while answering and if the targeted cite is changed or deleted, this question continues to be relevant. Commented Aug 29, 2012 at 12:20
  • True. About Partition: my remark was about how you wouldn't need padding if you don't do 2D pictures. But in this case we're gathering color triplets which again would introduce the need for padding if our data is not a multiple of three. Still it works. I'll be looking at that. Commented Aug 29, 2012 at 12:25
  • Interesting: execute Compress /@ {"1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789"} // Column and you will understand it as well as I do ;-) Commented Aug 29, 2012 at 12:51
  • @SjoerdC.deVries Uncompress /@ (StringTake[Compress@"123", {1, -#}] & /@ Range[10]) ... damn :) Commented Aug 29, 2012 at 13:01
  • Quite a bit of redundancy built into Compress Commented Aug 29, 2012 at 13:10
  • It's probably better code design not to base your code on that, though. Commented Aug 29, 2012 at 13:11
  • @SjoerdC.deVries yeap. the current implementation of Compress seems to start with a minimum string length of 22, and then increase in 4 bytes steps Commented Aug 29, 2012 at 13:39
  • @SjoerdC.deVries See my answer here ... using it :) mathematica.stackexchange.com/a/10166/193 Commented Sep 3, 2012 at 17:29
  • @SjoerdC.deVries Another usage example mathematica.stackexchange.com/a/11206/193. Also, many questions posted recently that uploaded large data files to public storage may benefit from this kind of thing Commented Sep 28, 2012 at 7:42
  • True, but we should take care not to be packing tens of megabytes in a single image. Putting notebooks with images themselves in an image, might not be the best idea (though it is feasible). It might annoy certain people. Commented Sep 28, 2012 at 8:17
  • @SjoerdC.deVries I understand the limitations. But I still believe there is not a better alternative yet to share relatively large data/code. The whole thing linked above is under 100KB while the nb is 134 KB Commented Sep 28, 2012 at 8:24
  • Well, let's start using it. Will you be making a convenient palette? Perhaps we should delete this post too? We can always amend it while deleted. Commented Sep 28, 2012 at 11:41
  • @SjoerdC.deVries Only if you don't mind waiting until I learn how. I never made one :( Commented Sep 28, 2012 at 11:44

This has been suggested on Meta Stack Overflow, but it looks like it is an unpopular suggestion, presumably because StackOverflow regulars generally don't approve of copy/paste or including large chunks of code. On our site, I think copy/paste for testing, evaluation, timing, saving as code snippet, etc, would justify this addition, and, as an iPad user, I'd welcome it too (because copy paste on iPad isn't always infallible on web pages.

  • I get the impression that it is popular with the posters...
    – Yves Klett
    Commented Aug 22, 2012 at 16:04
  • Yes, with the posters on this answer, but it doesn't look like its popular with the SO staffers/regulars. Hard to say, I'm not a regular at SO. :)
    – cormullion
    Commented Aug 22, 2012 at 16:23

I know this question is old, but Sjoerd brought it to my attention. I have mentioned a very nice solution to this problem in my What is your mathematica.stackexchange toolbag? question.

When I try example code that was given by a poster, the Select Code Block user-script is extremely helpful. When you hover a code-block, then a button appears which let's you select all code instantly. Since the above link doesn't have a downloadable version, you can use the selectCodeBlock.js from my GitHub page.

  • Very useful! Thanks for adding the reference here!
    – Yves Klett
    Commented Mar 21, 2015 at 7:19

You must log in to answer this question.

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