Wolfram Blog uses PNG images to display input and output cells in a notebook. For example, the following is an image from Samuel Chen's post:

enter image description here

I should be able to produce a similar image by selecting a group of cells and selecting File -> Save Selection from the pull-down menu. When I tried that on the cell

In[1]:= Series[Log[1+x],{x,0,6}]
Out[1]= x-x^2/2+x^3/3-x^4/4+x^5/5-x^6/6+O[x]^7

I get the garbled image

enter image description here

This does not happen when I save selection to PDF or BMP. In the case of BMP, the In[1] and Out[1] goes missing:

enter image description here

Do you know of a way to fix the problem with PNG export and/or include the In[1] in BMP export? I am running Mathematica on Linux x86 (64-bit), which I think is part of the problem.

  • $\begingroup$ How do you know they didn't take a screenshot? $\endgroup$
    – rm -rf
    Commented Jun 3, 2012 at 7:17
  • $\begingroup$ This works correctly for me with v7 on Win7: i.sstatic.net/dzJJ1.png $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 7:21
  • 1
    $\begingroup$ Since I am not seeing this problem, please check your setting for "PageWidth" in the Option Inspector, especially the one for ExportMultipleCellsOptions. Mine is set for Infinity. $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 7:26
  • 1
    $\begingroup$ @Mr.Wizard The image Szabolcs's uploader produces looks great: i.sstatic.net/jRg2e.png. I will look at his code. Thanks! $\endgroup$ Commented Jun 3, 2012 at 7:38
  • 2
    $\begingroup$ I ran into a similar problem when making the uploader. The system was using the palette's width to rasterize the selection, not the width of the notebook where the expression was. It's still not completely clear to me how the width is determined. You can google this site for ExportPacket and see this too for some hints. ("The front end can be unpredictable about what line width it uses to compute breaks, so it is usually wise to specify an explicit value for the PageWidth option." $\endgroup$
    – Szabolcs
    Commented Jun 3, 2012 at 8:18

2 Answers 2


First, I cannot reproduce the problem by using File -> Save As..., but I have seen similar things while developing the first (now defunct) version of the image uploader.

This is not a full answer, but perhaps it'll help you fix the problem.

The first approach I tried for rasterizing parts of a notebook was something along the lines of Rasterize[NotebookSelection[]]. I discovered that if I put this into a button, and put the button into a palette, everything will be reformatted to the width of the palette for rasterization.

I used two workarounds:

  1. Copy the selection into a new hidden notebook (CreateDocument[..., Visible -> False]) and Rasterize the whole notebook. The width of the hidden notebook defines the rasterization width.

  2. Use Edit -> Copy As -> Bitmap (use it programmatically through FrontEndTokenExecute), then paste back the bitmap into a notebook for exporting. This is available only on Windows (any version) or Mac (version 9 only).

  3. There's a third possibility using ExportPacket, described in this answer and its comments. I couldn't use this in the image uploader because it only works if a complete cell is selected by clicking on the cell bracket. It can't be used if a cell is only partially selected.

You can start along these lines to fix the problem. If you get stuck, let me know, and I'll go into details.


I just saw the question now. To add to Szabolcs' very detailed and general answer something practical, which works under Linux with Mathematica 8.0.4, let me give you code (a function from Szabolcs' image uploader), which works directly:

rasterize[maxWidth_: 650] := 
  Module[{target, selection, image}, 
   selection = NotebookRead[SelectedNotebook[]];
   If[MemberQ[Hold[{}, $Failed, NotebookRead[$Failed]], 
     selection], $Failed,(*there was nothing selected*)
    target = 
     CreateDocument[{}, WindowSelected -> False, Visible -> False, 
      WindowSize -> maxWidth];
    NotebookWrite[target, selection];
    image = Rasterize[target, "Image"];

Button["Rasterize Selection", rasterize[] &]

Basically, you can do whatever you like with the image at the end, but for simplicity, I just print it so you get it in the notebook. When you select your complete cell and press the button you get the following image

Mathematica graphics

  • $\begingroup$ How would you change this to control the resolution? $\endgroup$
    – M.R.
    Commented Nov 28, 2015 at 23:47

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.