Gnuplot’s epslatex terminal for PDF LaTeX plots

As I mentioned in Fun with Gnuplot, you can use Gnuplot's epslatex terminal to create really nice vector graphics having fonts that exactly match your LaTeX documents. This post describes the general process I follow to accomplish this.

There are quite a few steps involved, but I think the results are worth it. If you read the Gnuplot epslatex documentation, you'll see that there are some techniques that could be used simplify the process (e.g., setting font information in gnuplot.cfg, but this in how I'm doing things for now:

Step #1 - Get plot working with a simple terminal, e.g., x11

The first thing I do is make a standalone script to do the plot with the x11 terminal. This allows me to very rapidly experiment with things like axis labels and the general form of the plot. Here's an example that I put in an executable script file - something like myplot.gnuplot:

#!/usr/bin/gnuplot -persist
# myplot.gnuplot
set term x11
set xlabel "x"
set ylabel "y"
set xrange [-1000 : 3000 ]
set yrange [-1000 : 3000 ]
set title  "Positions of the three object types in FlatWorld"
set grid
plot "type1.dat" title "Type 1: delta = -0.1" with points 11, \
     "type2.dat" title "Type 2: delta =  0.0" with points 12, \
     "type3.dat" title "Type 3: delta = +0.1" with points 9

Once this is in place, I go through an edit file, run script (./myplot.gnuplot), and repeat process, until I converge on a plot that does what I want. Then I move on to the next step. Here's what the x11 step looks like:


Gnuplot with x11 terminal

Download type1.dat, type2.dat, and type3.dat, if you want to follow along.

Step #2 - Get your plot working with epslatex terminal

Next I take the x11 terminal script and convert it to epslatex terminal. Something like the following saved to plot.eps.gnuplot:

#!/usr/bin/gnuplot
# plot.eps.gnuplot
set term epslatex color
set format "$%g$"
set xlabel "$x$"
set ylabel "$y$"
set key box
set xrange [-1000 : 2500 ]
set yrange [-1000 : 3500 ]
set grid
set title  "Positions of the three object types in FlatWorld"
set output "./position.tex"
plot "type1.dat" title "$\\Delta = -0.1$" with points 11, \
     "type2.dat" title "$\\Delta =  0.0$" with points 12, \
     "type3.dat" title "$\\Delta =  0.1$" with points  9

Note that the strings used to set labels, titles, etc. take LaTeX syntax, e.g., $x$ for an 'x' in the math font.

When you run this new script, two files are produced - an EPS file that contains lines, ticks, etc, but no text (position.eps), and a LaTeX file that includes all the text (position.tex).

Step #3 - Convert EPS file to PDF

To take advantage of packages like microtype, and hyperref, I use pdflatex to compile my LaTeX documents, so the EPS file must be converted to a PDF, before I can use it:

$ epstopdf position.eps

The resultant PDF (position.pdf) will look something like the following. Note that there's no text. That won't come until later:


PDF with no text

Step #4 - Create a LaTeX file for your personal settings

Next, I create a LaTeX file (positions.tex) to encapsulate position.eps and position.tex and make the LaTeX settings I want, e.g., font type:

% positions.tex
\documentclass{article}
\usepackage{graphics}
\usepackage{nopageno}
%\usepackage{mathpazo}
\usepackage{txfonts}
\usepackage[usenames]{color}
	
\begin{document}
	
\begin{center}
  \input{./position.tex}
\end{center}
	
\end{document}

Step #5 - Compile and Crop Custom LaTeX File

Finally, we're able to combine the vector plot with text and symbols in the correct LaTeX font:

$ pdflatex  positions.tex

The result is positions.pdf, which is almost complete, except that it needs to be cropped down to just the plot and not a letter sized page. I use the pdfcrop package for this:

$ pdfcrop.pl positions.pdf
$ mv positions-crop.pdf positions.pdf


positions of objects in a 2D space

There you have it! A vector plot with fonts that will exactly match the rest of the text in your LaTeX document.

If you wanted to make the plot have a different font (say LaTeX's default Computer Modern font), go back to step #4 and comment out the \usepackage{txfonts} line in positions.tex which sets up a Times font.

Of course, all these steps should be put into a Makefile or shell script so you don't have to repeat yourself when making changes to the plot or its data.

9 Responses to “Gnuplot’s epslatex terminal for PDF LaTeX plots”

  1. Vic Viper Says:

    Thank you so much! I've tested it and the results are perfect. The method may be arcane, but it's easily scriptable. Really good tip

  2. Matthew Bohnsack Says:

    You're welcome.

  3. Philipp K Janert Says:

    I see that you are a dedicated Gnuplot user - figuring out the epslatex terminal takes true skill and stamina!

    I thought you might be interested to know that there is now a book on it: "Gnuplot in Action". You can pre-order it directly from the publisher: Manning: Gnuplot in Action.

    The book assumes no previous familiarity with Gnuplot. It introduces the most basic concepts rapidly, and then moves on to explain Gnuplot's advanced concepts and power features in detail.

    If you want to learn more about the book and the author, check out my book page at Principal Value - Gnuplot in Action.

    Let me know if you are interested in a review copy.

  4. Matthew Bohnsack Says:

    Philipp,

    I was browsing the Manning site a month or so ago, saw the book, and thought to myself that your book would be really valuable. The online documentation is not so much for learning - more of a reference, once you already know what you're doing and what's possible. I would be interested in a review copy.

  5. Ron Herrick Says:

    Matthew,
    I don't know if you remember me, but I am working with Kraig Snodgrass at Pioneer Hi-Bred in Johnston Iowa to upgrade their HPC cluster. I wanted to run a couple of things by you for a sanity check. Would you email me or call 515-778-7192? They are looking for a Linux Guru if you are interested in moving back to Iowa?

  6. Matthew Bohnsack Says:

    Ron. I remember you. I sent you am email with my phone numbers. Please fee free to call. -Matthew

  7. Helano Says:

    Good job!

    Thanks a lot!

  8. Sam Says:

    Hi,
    I'm looking for a way to produce eps plots with Gnuplot using TeX to compile the labels etc. but without all the fiddle described above. Is it possible to make an external call to the TeX compiler from Gnuplot? Ideally I'd like to call TeX every time Gnuplot processes a number/label/key and then save the eps output from the compiler to where Gnuplot would otherwise put the non processed simple text.
    Any help/comments on this?

    Thanks, Sam

  9. Henning Says:

    Hi,

    nice example. You may give an offset to the ylable in order to avoid the not so nice distance between the axis and the letter `y'. Just type

    set ylabel 'y' offset +3,0 # you need to adjust the number by try and error.

    But how do you control the width of the legend? It is quite wide, which is obviously not needed.

    Regards,

    Henning

Leave a Reply


Comment away... However, I reserve the right to remove SPAM without notice. I also reserve the right to edit stupid, offensive, or hateful comments. Alterations of this type will always be accompanied by a clear indication that censorship has occurred and a reason for its occurrence.