Hybrid bitmap/vector plots with gnuplot

I have a LaTeX document that includes a plot of a large dataset generated with gnuplot.  Everything’s fine, except that the resulting PDF is huge because of the enormous number of points in the plot.  A quick fix for this would be to switch the gnuplot terminal from epslatex to png.  Unfortunately, that also affects the text in the plot, which no longer matches the text in the document.  What is really needed is a pnglatex terminal.  Since that doesn’t exist, I hacked it.

The idea is to take advantage of the fact that the epslatex terminal already splits the text from the graphics.  First, we convert the .eps file to a .png with ImageMagick:

convert -density 288 myplot.eps myplot.png

The density argument is the DPI at which to generate the PNG.  This may need to be tweaked to minimize pixelation and aliasing artifacts.  Depending on your PDF viewer, higher may not necessarily be better.  Next, we change the generated .tex file to include our .png instead of the .eps:

sed -r 's:\\includegraphics\{([^}]+)\}:\\includegraphics[scale=0.25]{\1.png}:g' < myplot.tex > myplot-png.tex

The default DPI seems to be 72, so the scale argument should be 72 divided by the density argument to ImageMagick.  Finally, include myplot-png.tex instead of myplot.tex in the main document.

Note that rasterization necessarily loses information, and the result is of lower quality than using the epslatex terminal.  The resulting PDF can be much smaller, though, so the tradeoff may be worth it.

One Comment

  1. Posted March 7, 2016 at 3:24 am | Permalink

    Hi,

    thanks for sharing this interesting method. The only flaw I see is that since your approach requires pdflatex to implement the generated .png file, there is no (simple) way to automatically get rid of unwanted white space around the plots. When dealing with .eps files, there at least exists the fixbb script from the gnuplot website.

    Do you have also a solution for this problem?

    Best,
    Ingo

Post a Comment

Your email is never shared. Required fields are marked *

*
*