Gnuplot is a great tool for creating plots. LaTeX is a great tool for creating documents. Both are (or at least can be) created using a text editor and compiling the source. Both work with Windows, linux/unix and Mac. Wouldn’t it be great if you could include a gnuplot plot in a LaTeX document without having to worry about image formats, compression, sizes, etc?

Well you can, actually. Gnuplot is able to generate code which can be included in your document without any additional programs, plugins or extras. All that needs to be done is to set the gnuplot terminal to latex, output gnuplot to a .tex file and add \include to your LaTeX code, with the graphicx package included in the preamble. Too simple? Let’s see an example by plotting y=x^2 and showing it in a LaTeX document.

plot.gnuplot

set terminal latex
set output "plot.tex"
plot x**2
unset output

document.tex

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\input plot
\end{document}

From here we simply need to run:

gnuplot plot.gnuplot
pdflatex document

And we’re done! We now have a document with the y=x^2 function plotted, like so:

LaTeX gnuplot example

  1. Philipp says:

    Nice and easy. All the other documents I found were hundrets of years old. Thanks for publishing it. LaTeX is still magic to me 🙂

    Regards,
    Philipp

  2. Paul says:

    Very cool. Thanks for sharing this.

  3. johannes says:

    nice stuff. thanks for the hint

  4. Alireza says:

    Hi, I’m new to latex and I’m using Mac OS X which Is also new to me. Would you give me a simple step by step instruction on how to implement this?? I tried a few times but failed.
    thank you.

  5. Sriiram says:

    Thank you very much its very good

  6. lego says:

    The latex terminal from gnuplot is very limited. It can give problems with large amounts of data, or complex graphics stuff (transparencies, …)

    You may want to take a look a this:
    tug.org/pracjourn/2010-1/sunol/sunol.pdf

    It has a link to a very useful script to automatize the process.

  7. Phil Huffman says:

    Very cool. I’ve used sageTeX and TiKZ before, with good results. This appears to be far simpler. Thanks.