Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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
show 2 more comments

1 Answer

up vote 6 down vote accepted

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:

The resulting plot with a LaTeX legend title.

Note: Backslashes need to be doubled for some reason. I guess they have a special meaning as well in gnuplot.

share|improve this answer
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
show 4 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.