Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here I produce a Raster image with large uniformly colored rectangles and put it in an Inset:

With[{n = 5},
       List @@ ColorConvert[ColorData["LakeColors"][#], 
          RGBColor] &, (Range[n - 1] - 1)/(n - 2)]}
   Automatic, Automatic, {.45, .5}


Now export this to PDF:

Export["p.pdf", %]

ramp artefacts

The uniform regions have been replaced by a series of discrete bands that seem to try an interpolate between the rectangles. This happens if I choose fewer rectangles too (e.g., n = 3).

If I generate the same thing without using Raster, the exported PDF looks fine - e.g., try this:

With[{n = 5},
       Translate[Rectangle[], {#2[[1]], 0}]} &, (Range[n - 1] - 
        1)/(n - 2)]
   Automatic, Automatic, {.45, .5}

So it's the conversion of the rasterized image in Export that's responsible for this effect. Interestingly, one can work around this (on Mac) by selecting the graphic, going to Edit > Copy As > PDF and pasting the result into Preview.

Although I have some workarounds now, it still seems mysterious to me why this banding effect happens with Raster. And it would of course be nice if someone knew how to use Raster in an Inset inside a Graphics without triggering the effect that causes the blocks of the raster to lose their uniform color.

Also - the arguments to Inset can be left out (in particular the scaling {.45, .5}). But it seemed to me that this problem didn't always happen with Raster, and that it probably has something to do with the size of the bitmap being scaled up relative to some assumed coordinate system used in Export.

share|improve this question
Yes, I can confirm that this happens on Mac.The problem is Preview. If you load the file in Adobe Reader you will see the PDF graphic as expected. – Matariki Jul 10 '12 at 0:39
Yes, I've seen this problem too on both Mathematica and MATLAB outputs, and as Matariki said, it's due to Preview. The PDF/EPS files look fine when viewed with Adobe Reader or GhostView – R. M. Jul 10 '12 at 0:56
@Matariki Thanks for the pointer - I guess this means it's Mac specific. Then I'll probably have a look at the PDF source and see if I can come up with a fix along the lines of the MediaBox replacement I did on this page - it didn't occur to me that I'd have to revisit that in this context... – Jens Jul 10 '12 at 1:35
@Jens I don't know if it is Mac specific but more Preview specific as the effect is not visible using PDF Reader on the Mac. This might have to do with OSX PDF rendering engine. I never looked into it myself. I couldn't observe the effect on Windows but there I used PDF Reader as the default. – Matariki Jul 10 '12 at 2:04
@Matariki Yes indeed - it's WebKit specific. ghostscript and Inkscape work perfectly with the file. I meanwhile figured out a good work-around: apply ColorConvert[..., "CMYK"] to the Graphics[Raster[...]] expression, and the output looks fine. I want to try and see if one can do even better, though... – Jens Jul 10 '12 at 2:13
up vote 7 down vote accepted

This issue seems to affect all PDFKit based viewers on OS X, but it doesn't appear to point to anything intrinsically wrong with Mathematica's PDF export. A work-around that allows you to view the exported PDF without color bands in PDFKit viewers such as Preview would be to use the following command when creating the PDF in Mathematica:

 p /. Graphics[Raster[i__]] :> 

Alternatively, if you know exactly where the Raster is being created, you may as well do the conversion immediately at that point. Then my example code above would have to be modified as follows:

With[{n = 5},
        List @@ ColorConvert[ColorData["LakeColors"][#], 
           RGBColor] &, (Range[n - 1] - 1)/(n - 2)]}

After doing this, I see no more trace of the discrete bands.

Edit Originally I had inserted ColorConvert but then realized it's simply the conversion to Image that is required to make it work.

Edit 2

To make this more generally applicable: whenever your exported PDF shows banding in gradients, you could try to export the plot (let's call it plot) using this command:

Export["plot.pdf", plot /. Raster[i__] :>

Edit 3

This is an old post, but in the meantime I encountered similar issues in many other contexts and came up with a different work-around which also applies here:

rasterTrick[plot_] := 
  Prolog -> {Opacity[0], Texture[{{{0, 0, 0, 0}}}], 
    VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}}, 
    Polygon[{{0, 0}, {.1, 0}, {.1, .1}}]}]

Export["p.pdf", plot // rasterTrick]

The function rasterTrick was recently useful in answering this question (where you can also find a link to further explanations of how it works): Export MatrixPlot graphics to PDF

share|improve this answer
Well done, Jens. Dafuer gibt's +1 – Matariki Jul 10 '12 at 2:37
@Matariki And thanks for your help finding the culprit! – Jens Jul 10 '12 at 2:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.