Question

I am using Mathematica to plot a number of figures I require and then using them as pdfs in Latex.

The issue is that Mathematica exports the figures using Acrobat 5.x settings compatibility and I think this results in figures that are large, not optimized and when I have a couple of them in the final pdf, sometimes it takes minutes just to print a single page of the final document.

Is there a way to change the output settings for pdfs in Mathematica? Should I export as pdfs, convert to postscripts and then create new pdfs from the .ps with new settings? Should I export .eps and convert to pdf? Is there an one step way to create optimized pdfs through Mathematica?

Quick example (and a bit exaggerated so the difference is quite obvious): The pdf I export from Mathematica (Export["C:/figure.pdf", %]) is 1065kb in size, .pdf ->.ps ->.pdf gives a final file of 652kb and .eps->.pdf a file of 610kb. I would not use any of these in a latex but it shows the difference in the settings.

Was it helpful?

Solution

It is a myth that pdf->ps->pdf will always give you a smaller file size. For e.g., consider

(in mathematica)

fileName = "test.pdf";
p = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}];
Export[fileName, p];

(in the shell)

pdf2ps test.pdf && ps2pdf test.ps test1.pdf

test.pdf is 446kB, whereas test1.pdf is a whopping 11.5MB on my machine!

What happens normally, is that when you export a PDF file using Mathematica, it stores the text (axes tick marks, labels, etc), fonts and images, all of which add to the bulk. If you have vector graphics with lots of tick marks/labels, these can be quite significant. Doing pdf->ps->pdf will strip the file of font data and text data, at the same time compressing the image, which is why you see a decrease in file size. Although not observable at small magnifications, you will notice the difference when you zoom in considerably. For e.g., replacing p with

p = Plot[Sin[t], {t, 0, 2 Pi}];

produces a 37kB file for test.pdf and an 8kB file for test1.pdf on my machine. The quality of the 8kb file is poorer than the original (zooming to 600%+ shows this). You can see the plain text information that mathematica stores in the PDF file by doing

Import[fileName,"Plaintext"]

However, for more complicated PDF files, converting to ps and then back to pdf is not necessarily a good option and can result in the file size blowing up.

You can try playing with the options for PDF in Export, such as "AllowRasterization" and ImageResolution, or even rasterize the image before saving, but I guess you already knew that.

Here's a back-alley quick and dirty way of reducing file size from within mathematica

Export[fileName, First@Import@Export[fileName, p]]

The file size is now only 12kB! It's only a third of the original, only slightly larger than that obtained from pdf->ps->pdf, and is of a much better quality than the 8kB one (although not exactly the same as the original. The differences can be seen at magnifications of 1200% and above).

What happens is that upon importing and re-exporting, only the stored image is saved. The plain text part of it is thrown away, and in this small example, that happened to be significant. For the DensityPlot example, the reduction was only to 425kB (still, went down rather than blow up to 11MB).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top