# LaTeX formulas in gnuplot plot legends?

I would like to have my gnuplot legends set in LaTeX math mode, because they often are described by a formula. I use wxMaxima to generate the plots, but I could switch to using gnuplot directly, if that makes things simpler. I am also using pdflatex, and would like to stick to that.

-
If you use the tikz or the epslatex terminal in gnuplot and then include the resulting files in a LaTeX document, all the text will be typeset using LaTeX, so you can just use math mode directly. Would that be a feasible workflow? –  Jake Aug 25 '11 at 11:09
Hm, I just tried using the epslatex terminal, but I just get an empty grid, when trying to plot x**2. Got to debug this. But that would be a usable workflow. –  Arne Aug 25 '11 at 11:19
Hm, for some reason the .tex file produced by gnuplot is cut off. the \begingroup is never being closed. No idea why that should be broken... –  Arne Aug 25 '11 at 11:26
You might also want to look at using pgfplots, which creates the plots directly in the LaTeX documents and can easily use gnuplot as a backend for doing the calculations. This usually leads to much more nicely integrated plots. –  Jake Aug 25 '11 at 11:27
Ah, yes, you need to call set out <filename.tex>, do the plotting, and then call set out (without a file name) again, to close the file. –  Jake Aug 25 '11 at 11:29

Concerning Andrew's wish for a short tutorial: Here it is. First, we set up a gnuplot called test.plt:

plot [-5:5] [-1.5:1.5] sin(x+pi) title "$\\sin(x+\\pi)$"

Then we also set up a small Makefile:

.SUFFIXES: .plt .tex .pdf

%.tex: %.plt
gnuplot -e " \
set format '$$%g$$' ; \
set terminal epslatex standalone color ; \
set output '$@' \ "$<

%.pdf: %.tex
pdflatex $< all: test.pdf Running "make all" will produce this plot: Note: Backslashes need to be doubled for some reason. I guess they have a special meaning as well in gnuplot. - Three comments: 1. You need not call set output, because Gnuplot will close the output file when it exits; 2. Use set format '$%g$' to typeset numbers in mathmode (needed for minus sign); 3. Do you really need to crop the file? I have never used it. – Andrey Vihrov Aug 25 '11 at 14:48 1. Alright. It was necessary when I was trying it interactively. 2. Can you elaborate? I tried typesetting a "$-2$" and it worked just fine, I think. 3. Without cropping, I get an A4 page. However, I'd like a tightly fitting PDF that I can use wherever I want. :) – Arne Aug 25 '11 at 14:51 Plot sin(x) from -5 to 5 with and without the alternate format and look at the x axis labels. This is the same as when you type$-1\$, not -1 to get minus one in TeX. –  Andrey Vihrov Aug 25 '11 at 14:55
If you don't plan to include the plot in a document and want to typeset it separately, consider using the standalone option of the epslatex terminal. It will give the correct size without any cropping. –  Andrey Vihrov Aug 25 '11 at 14:58
Good hint! I will try that option PS: which texlive package contains the minimal documentclass? My installation does not have it, yet... –  Arne Aug 25 '11 at 15:04