# gnuplot in LaTeX, without gnuplottex

I'm using the cairolatex terminal in gnuplot to create some surface plots (pgfplots is failing the task because of its hunger for memory). As the number of plots increase, I would like to include the gnuplot compilation into my tex compilation to ensure the plots are up-to-date. I came across the gnuplottex package, which seems to handle the task, though it's slow.

I have a large, nested document. Tex root is thesis.tex where I include, amongst others, theory.tex. In theory.tex I have put

\begin{figure}[htbp]
\centering
\end{figure}


where gnuplot/cascade.texcontains the gnuplot code

\begin{gnuplot}[terminal=cairolatex]
set samp 100;
set iso 40;
set xrange [0:1];
set yrange [0:1];
set zrange [0.8:2];
unset key;
unset colorbox;
set hidden3d front;
a = 1;
set xyplane at a;
f(x,y) = (x+y)/(2*sqrt(x*y));
set xlabel "$\\tau_I$";
set ylabel "$\\tau_D$";
set zlabel "$\\zeta$";
splot f(x,y) with pm3d at b, f(x,y) with lines;
unset output
\end{gnuplot}


The output of this setup is awesome. But whenever I change something in theory.tex, the entire gnuplottex routine recompiles. This takes quite some time, and is very annoying.

How come latexmk doesn't recognise that the gnuplot code hasn't changed and leave it? Could this dependency be specified, and in that case, how?

EDIT: gnuplottex is fairly slow on my system, although gnuplot is really quick. How can I avoid using gnuplottex, it's long compilation time and thus avoid having gnuplottex recompiling every time?

• Because the gnuplot command is being executed by the gnuplottex package, latexmk doesn't control whether gnuplot is invoked. You can change things so that latexmk is in control of gnuplot by turning off -shell-escape and defining a custom dependency for latexmk to invoke gnuplot. Could you try this on your present document? (One would need a modification to gnuplottex to make this solution fully general.) – John Collins Apr 3 '13 at 15:37
• I thought maybe latexmk would control whether or not gnuplottex would be invoked. But yeah, this could be a solution. I don't have the skills to make such a dependency though, could you please help me out? – Holene Apr 4 '13 at 6:47
• Come to think of it, what i really would like is to avoid using gnuplottex at all, and have latexmk invoke gnuplot when needed. Something like this maybe (though is doesn't work, of course): add_cus_dep('gnu','eps', 0, 'makegnu2eps'); sub makegnu2eps { system("gnuplot \"$_[0].gnu\"") ; } – Holene Apr 4 '13 at 7:26 • What doesn't work about the custom dependency you tried? If you put your gnuplot code in a file with extension .gnu, e.g., figure.gnu, and have a corresponding \includegraphics{figure} in your LaTeX file, then it should work. This assumes you are using latex, not pdflatex, to compile your LaTeX file, since you are generating an .eps file. When you run latexmk, there will be a missing-file error the first time it uses latex; latexmk should detect that and use gnuplot. Then there should be a successful second use of latex. – John Collins Apr 4 '13 at 13:50 • I made a mistake, it's actually working! I changed the dependency to yield: add_cus_dep('gnu','tex', 0, 'makegnu2tex'); sub makegnu2tex { system("gnuplot \"$_[0].gnu\"") ; }. The gnuplot code now outputs gnuplot/cascade.tex and my LaTeX file requests \include{gnuplot/cascade.tex}. The gnuplot code is compiled (as fast as you would expect gnuplot to compile, way faster than gnuplottex), the .eps and .tex files from cairolatex is produced, and everything is fine! – Holene Apr 5 '13 at 11:45

I have avoided using gnuplottex at all by adding the following custom dependency to .latexmkrc:

add_cus_dep('gnu','tex', 0, 'makegnu2tex');
sub makegnu2tex {
system("gnuplot \"$_[0].gnu\"") ; }  My gnuplot file uses the cairolatex terminal (or any other terminal who produce a .tex file). My latex document contains \input{gnuplot_output.tex} which, because of the newly defined dependency, causes a system call to gnuplot upon tex compilation. The .gnu file and .tex files are now a normal dependency, and gnuplot will not recompile unless the time stamp is changed. It's important to notice that the path specified in set output "path/gnuplot_output.tex" in the .gnu file is relative to the tex root file as long as the gnuplot system call is induced by latexmk. • Should one worry about a possible missing dependency between eps/pdf figure and the gnuplot file? Two files are generated by one command ($ gnuplot file.gnu -> file.tex & file.eps). – Hotschke Apr 5 '13 at 14:23
• I don't know if a non-one-to-one dependency in general should be avoided, but unless your .eps/.pdf file somehow should update without its corresponding .gnu file being updated, it shouldn't pose a problem. – Holene Apr 6 '13 at 7:45

My current solution without using gnuplottex is a modification of one I found a while ago (How to include an SVG image in LaTeX) for the inclusion of SVGs (with an automatic call to Inkscape if necessary).

\newcommand{\executeiffilenewer}[3]{%
\ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
{\pdffilemoddate{#2}}>0%
{\immediate\write18{#3}}\fi%
}
\newcommand{\includegp}[2]{%
{gnuplot #1#2.gp}%
\import{tmp/img/}{#2.tex}%
}


I have my gnuplot files in a subfolder, e.g. img/ch01/foo.gp, and I like to have all temporary files in tmp/ and all temporary image files in tmp/img/ (since I use git to track my tex files, but do not want do track any derived files, i.e. the ones in tmp/). The gnuplot file can then be embedded in a document like this:

\begin{figure}
\centering
\includegp{img/ch01/}{foo}
\end{figure}


I call miktex-pdftex.exe from TeXworks with the -aux-directory=tmp and -shell-escape options for this to work. Addionally, the folder containing the gnuplot binary must be in the PATH variable or in my case set in the TeXworks settings (e.g. D:/gnuplot/bin).