5

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.

3
  • You can go directly to .ps if it helps (dont know that it will but worth a shot).
    – soandos
    Commented May 16, 2011 at 3:09
  • Export::infer: "Cannot infer format of file "figure.ps" You cannot set the export option to postscript, .eps or .pdf are acceptable but not .ps
    – o4tlulz
    Commented May 16, 2011 at 3:14
  • Don't know, just know it is an option. Btw, there are options when you save. They dont look like they do very much, but could be worth a shot.
    – soandos
    Commented May 16, 2011 at 3:18

1 Answer 1

4

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).

4
  • Is it possible to explain the quality part a bit more? I have four files open in front of me from the second example with p = Plot[Sin[t], {t, 0, 2 Pi}]; zoomed at 3200% and I cannot spot any difference between them. The are the pdf from Mathematica (32kb), the export-import-export file (10kb) the pdf->ps->pdf (14kb) and the eps->pdf (10kb) and they seem identical. Should I be looking at something in particular to spot the difference in quality?
    – o4tlulz
    Commented May 16, 2011 at 7:01
  • @o4tlulz: Try looking at the tick marks (for e.g., the 0.5 on the y-axis or the 3 on the x-axis) instead of the sine curve. The curve will be reproduced perfectly, as it is vectorized. It is the text that loses its quality.
    – abcd
    Commented May 16, 2011 at 13:56
  • I have to be honest, all pdfs with the sine function created with what was discussed previously give me exactly the same quality and even at 6400% zoom they look identical to me. I think that for the time being I will stay with the exporting to eps and then converting the eps to a pdf through the distiller to use it in the finaly document.
    – o4tlulz
    Commented May 18, 2011 at 1:36
  • 1
    @o4tlulz: Thanks for the accept. These conversion processes can be machine and algorithm dependent, so I'm not surprised that you and I get differing results. BTW, are you aware of the epstopdf package for latex? You can use eps figures and run pdflatex and it will convert the eps to pdf automatically using GhostScript. This might save you some manual conversions, if that's how you're doing it right now.
    – abcd
    Commented May 18, 2011 at 2:01

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