Gnuplot offers many output terminals; amongst the more useful ones for scientific publishing are the LaTeX terminals which embed EPS graphics in a TeX file which adds labels. However, sometimes you need a standalone figure and this becomes a hassle. Here, I introduce a tool to extract the figures from rendered TeX output without littering your working directory with temporary files.

I’ve been using Gnuplot as my primary plotting tool for years. Writing most of my code in C++, I find it inconvenient to use matplotlib and its offshoots for plotting. One advantage of the latter, though, is its ability to display LaTeX math in axes labels, keys, or annotations in figures out of the box. Gnuplot would be lacking here (where users’ had to mess with Postscript’s enhanced mode and the like, just to get Greek symbols) if it wasn’t for the LaTeX terminals. Even better, using the Cairo and Poppler backends (latexcairo terminal) produces some of the better looking plots as far as Gnuplot terminals go. The resulting figure.tex file can then be included in the LaTeX document using the

\input{figure.tex}

command. This has the added benefit of rendering text consistent with the style (e.g. font face) of the parent document. You can even use custom TeX commands in figure labels and changes to those are immediately reflected without the need to even run Gnuplot again.

So, all great? Not quite. Sometimes the above workflow is just not an option, for example when submitting journal articles, or writing blog posts, emails, etc. — basically every time the final medium is not a LaTeX document or you just need a self-contained standalone figure.

Gnuplot’s LaTeX terminals do in fact offer a standalone option but this still requires you to run LaTeX and is not very customizable but uses a static template. So you loose the consistent appearence with the rest of the document.

Enter latex-gnuplot: a small shell script which automates the embedding of the figure, allows for customizable templates and preamble files, runs your favourite TeX engine and handles conversions to third-party formats — all in the solitude of a temporary directory, i.e. without leaving behind tons of *.tex, *.aux*, or *.eps files. And it’s mature enough that I made it available for public use:

latex-gnuplot on GitHub

As an example, consider this figure from a recent publication:

Figure 1 of [arxiv:1804.08557](/publications/1804.08557/).

Figure 1 of arxiv:1804.08557.

It was generated by running

$ latex-gnuplot Td_op_curve.gp *.dat --svg --template=libertine

This post is not meant to replace the README or man page, but there’re some things to notice here.

Besides the actual Gnuplot script Td_op_curve.gp, also any dependent files (data, color palettes, …) have to be specified, since the script will copy everything in a temporary location and run Gnuplot and LaTeX there. We then specify output formats, here --svg. SVG is actually a really nice format for scientific graphics on the web. SVG support is widespread nowadays; it preserves the scalability of vector graphics (hence the name ;-) ), so you can zoom in really close to see those small deviations in the three curves.

Figure 5 of [arxiv:1804.08557](/publications/1804.08557/).

Figure 5 of arxiv:1804.08557.

The last option specifies a LaTeX template to use. The default one is called article and based on the article document class, giving it the ‘vanilla’ LaTeX look. libertine uses the Linux Libertine typeface which is professional, but lacks the somewhat dusted look of Computer Modern, LaTeX’s default font. The third template that comes preinstalled is one based on beamer to allow seemless inclusion in presentations made using the beamer document class.

Custom templates can easily be installed in various places:

  • ~/.latex-gnuplot/
  • $PREFIX/share/latex-gnuplot/
  • /usr/local/share/latex-gnuplot/
  • /usr/share/latex-gnuplot/

But it’s also possible to supply a custom preamble file to insert your definitions and \usepackage directives for the current document, as well as injecting individual commands, e.g. to change the font size.

Figure S1 of [arxiv:1804.08557](/publications/1804.08557/).

Figure S1 of arxiv:1804.08557.

So I hope this tool will be useful to some. It’s not anything fancy but I found it useful enough in the past to expand its capabilities and now share it with the public.