Notes on using GNUplot

Outputting from GNUplot to PDFLaTeX

In gnuplot:

set format "$%g$";
set terminal epslatex colour solid size 15cm, 10cm
fileBase = "graph" # DO NOT USE UNDERSCORES!!!
texFileName = fileBase.".tex"
epsFileName = fileBase.".eps"
set output texFileName
… plot etc...
unset output # forces buffer to flush
# Convert to PDF and delete the EPS file
system(sprintf("epstopdf %s && rm %s", epsFileName, epsFileName))

This will output 2 files: graph.tex and graph.pdf. (The “standard” technique is to let GNUplot output an EPS file and then use \usepackage{epstopdf} in the LaTeX preamble to convert the EPS file to PDF. But this appears rather broken in LyX >= 2.0.1. The tidiest solution would probably be to use the cairolatex output terminal in GNUplot 4.6 (released March 2012) but GNUplot 4.6 isn’t available on our college machines yet (I could manually install it to my own directory but that feels rather ugly). It looks like GNUplot 4.6 will be included in Ubuntu 12.10.) Exit gnuplot before opening the .tex file or run unset output! (Otherwise it won’t have finished emptying buffer!) (many more options listed in the epslatex section of the gnuplot documentation) In the LaTeX document:


Compile with: pdflatex --shell-escape LaTeXdoc.tex There’s some more help here:

Settings in Lyx

  1. Insert the \input{graph.tex} using Insert > TeX code (ctrl-L)
  2. See this forum thread for how to set --shell-escape
  3. Set the \usepackage lines in Document > Settings > LaTeX Preamble