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

I work with histograms and would like to export it to EPS format. I've noticed that my exported image loses the PlotRangeClipping option that I set up when I export the plot. This is true for at least EPS, PNG and PDF.

Based on this question and its accepted answer, I defined the function


Now here is an example of histogram

data = Table[InverseCDF[NormalDistribution[0, 1], Random[]], {i, 1000}];
PlotB = Histogram[data, 20,
    PlotRange -> {{-2, 2}, Automatic},
OutlinedExport["output2.pdf", PlotB, ImageSize -> 600]

which appears to have lost the clipping during the export.

The questions are :

  • Is there a way to preserve clipping as defined through options like PlotRangeClipping and PlotRangePadding ?

  • If the problem is related with the OutlinedExport, is there a way to answer my question while also keeping the outline of fonts (which I need but have here removed for the sake of example) ?

share|improve this question
up vote 4 down vote accepted

Since there is clearly a bug in Import with the PDF format, there can only be workarounds and compromise solutions. By exporting as in your own answer, you will retain the setting of PlotRangeClipping because at no point is it necessary to re-import the exported PDF to Mathematica. However, this way you almost certainly will run into missing font problems when importing the PDF (or EPS) into other graphics programs. For example, Adobe Illustrator and likewise Inkscape will be unable to display your graphic with the correct fonts in general.

I tried this with Inkscape and your exported file test.pdf, and it doesn't work properly.

So you could resort to finger-painting as in Mr.Wizard's answer where the clipped parts are hidden behind added rectangles, so that the result of outlinedExport look correct.

Or you could retreat to another approach which I invented long ago, relying on an external program to convert fonts to outlines. That's what I used to do before Mathematica even had the ability to produce outlined fonts at all. The external dependence is ghostscript.

If you want to go that route, here is an updated version of that procedure:

gsExport[exportName_, ob_, opts : OptionsPattern[]] := Module[
   printOption = First@Options[$FrontEnd, PrintingStyleEnvironment],
       out = FileNameJoin[{$TemporaryDirectory, 
          "MathematicaOutput" <> StringJoin[Map[ToString, DateList[]]] <> 
      SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
   FilterRules[{opts}, Options[Export]]
  SetOptions[$FrontEnd, printOption];
  device = 
   If[ToLowerCase[FileExtension[exportName]] == "eps", "epswrite", 
    "pdfwrite", "pdfwrite"];
  Run["gs -sDEVICE=" <> device <> " -dNOCACHE -sOutputFile=" <> 
    exportName <> " -q -dbatch -dNOPAUSE " <> out <> " -c quit"];

It takes the export file name and the graphics object (ob) as the first two arguments, and also accepts additional options appropriate for PDF export. By default it exports to PDF with the provided name (exportName), but if you specify an en extension .eps you will get encapsulated postscript instead. The fonts are replaced y outlines by executing gs via Run. This assumes that gs is installed and in your system's executable PATH.

I thought this approach was obsolete by now, but apparently there are still enough bugs in Mathematica that open-source solutions must come to the rescue once again.


The above solution continues to work with gs version 9.16, but according to this answer on StackOverflow (and thanks to Szabolcs for pointing it out) there is a new option that you could use in the future, if you encounter problems: replace -dNOCACHE by -dNoOutputFonts.

share|improve this answer
Nice alternative. Thanks ! – Pschoofs Aug 3 '12 at 7:19

Since the clipping is lost, and I cannot find a way to make Import/Export retain it, I suggest adding it manually:

PlotC =
  Graphics[{White, Rectangle[Scaled[{-1, 0}], Scaled[{0, 1}]], 
                   Rectangle[Scaled[{ 1, 0}], Scaled[{2, 1}]]}]

OutlinedExport["output4.pdf", PlotC, ImageSize -> 600]

Mathematica graphics

This works by drawing a white rectangle on either side of the plot range behind the frame labels (here shown in pink instead):

 Graphics[{LightRed, Rectangle[Scaled[{-1, 0}], Scaled[{0, 1}]], 
                     Rectangle[Scaled[{ 1, 0}], Scaled[{2, 1}]]}]]

Mathematica graphics

You may need larger $x$ values inside Scaled to cover the full range, e.g.:

Rectangle[Scaled[{-100, 0}], Scaled[{0, 1}]]

share|improve this answer

Actually the problem comes from the OutlinedExport function defined above which does not keep any clipping options. Fortunately, it seems possible to do without and keep the font embedding in the pdf.

The second answer from the question referred above actually answers this one too.

SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
data = Table[InverseCDF[NormalDistribution[0, 1], Random[]], {i, 1000}];
PlotB = Histogram[data, 20, PlotRange -> {{-2, 2}, Automatic},
  FrameTicksStyle -> {
        FontFamily -> "Palatino Linotype",
        FontSize -> 14
        FontFamily -> "Palatino Linotype",
        FontSize -> 14
  Frame -> True,
  PlotRangeClipping -> True,
  ImagePadding -> {{80, 10}, {60, 5}},
  PlotRangePadding -> Scaled[0.02],
  FrameLabel -> {
        FontFamily -> "Palatino Linotype",
        FontSize -> 16
        FontFamily -> "Palatino Linotype",
        FontSize -> 16]
Export["test.pdf", PlotB, ImageSize -> 600]

The function Export keeps then the font embedding as well as the PlotRangeClipping. It works for EPS, PDF,...

share|improve this answer
This does indeed work for exporting with embedded fonts as requested (+1). Interestingly though, if I now re-import that file using Import["test.pdf"][[1]], the clipping is lost again! What this shows is that the problem is clearly with Mathematica's PDF import. In my outlinedExport function which you were using, this must also be the place where things go wrong. I'll think about another workaround to that workaround.. – Jens Aug 2 '12 at 20:51

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.