Sunday, September 25, 2011

Fixing TiKZ/PGF to Work with Gnuplot

While working on a new presentation today (in Beamer), I grabbed a slide from a previous presentation (one I reuse annually). The slide contains PGF code that uses Gnuplot to plot functions (which I then annotate using PGF commands). Small problem: the transplanted slide didn't work (no curves on the plot) ... which led me to attempt to recompile the original presentation and discover that it, too, no longer worked. Grrr!

Poking in the files in the /tmp directory showed me that Gnuplot was crabbing about a command (set terminal table) that PGF uses to tell it to create a text file with the points to be plotted. A bit of searching led me to a blog post by
Toine Bogers containing the fix. Instructions for Linux follow (I haven't tried it on Windows yet):
  1. Open a terminal and run 'kpsewhich pgfmoduleplot.code.tex' to locate the file you need to edit. (The location varies with the LaTeX distribution you use; this takes the work out of finding it.)
  2. Issue 'sudo gedit <path>/pgfmoduleplot.code.tex' (where <path> is the path you found in the previous step) and give your password when prompted. (Feel free to use a different editor if gedit is not your cup of tea.)
  3. Find the line containing 'set terminal table' and delete the word 'terminal', so that it becomes 'set table' (the rest of the line staying unchanged).
  4. Save the modified file, and you should be good to go
In LyX, you also have to go to Tools > Preferences... > File Handling > Converters, locate the converter for LaTeX (pdflatex) -> PDF (pdflatex), and tweak the Converter: entry to read 'pdflatex -shell-escape $$i'. Then click Modify and Save. If you use other formats besides PDF (pdflatex) to view/export Beamer presentations, you may have to add the -shell-escape flag (or something similar) to them as well. The flag allows pdflatex to run an external program (in this case gnuplot) in a shell.  Users of other other LaTeX editing software will likely need a similar tweak.

I love open source software, but one of the challenges of using lots of separate pieces of software is that developers will change something in the latest version of one program or library, and that change will break some interaction with another program/library from an entirely separate developer. To modify a saying from my misspent youth: "You pays your money [or, in this case, you don't] and you takes your chances."

No comments:

Post a Comment

If this is your first time commenting on the blog, please read the Ground Rules for Comments. In particular, if you want to ask an operations research-related question not relevant to this post, consider asking it on OR-Exchange.