[Tex/LaTex] TikZ overwrites gnuplot .gnuplot file

angledegreesgnuplottikz-pgf

I do not know if what I am bringing up is a feature or a bug.

I am trying to get a cos x trig plot in degrees generated by gnuplot and plotted by TikZ. My .gnuplot file looks like this:

set table "trig.cosinex.table"; set format "%.5f"
set angles degrees; set samples 500; plot [x=0:360] cos(x)

When I compile my .tex file with Tikz/lualatex, the .gnuplot file becomes

set table "trig.cosinex.table"; set format "%.5f"
set samples 500; plot [x=0:360] cos(x)

The set angles degrees; part is deleted.

The gnuplot-relevant line in my .tex file is:

\draw [domain=0:360, samples=500, smooth] plot[id=cosinex] function {cos(x)}; % anm I missing a prefix here?

Kindly note that:

a. I hand generate the .table file from the .gnuplot file by running it through gnuplot.

b. TikZ/lualatex does not generate a .table file even if it does not exist (contrary to manual?) but only overwrites the .gnuplot file.

My questions are:

  1. How can I get TikZ to specify the angles to be degrees in the .gnuplot file that it overwrites? [If overwriting cannot be prevented.]
  2. How to prevent TikZ from overwriting my .gnuplot file in the first place? [After hand-compilation with gnuplot, the .table is left untouched by TikZ during multiple compilations as specified but the .gnuplot file is altered as above. So, repeat hand-compilation produces a wrong .table file and does not give correct results.]

Thanks.

Best Answer

When you use \draw ... plot ... function {<plot command>}, the first thing TikZ does is generate the .gnuplot file with the default options and the <plot command>. Then it tries to execute gnuplot with the .gnuplot file. This is a feature (it's what function is supposed to do), and works as described in the manual.

If all you want to do is plot the .plot file generated by calling gnuplot manually, then you shouldn't use the function command, but simply file. In your case, you could use

\draw [smooth] plot file {texse.cosinex.table};

Note that the domain, samples and id keys have no effect when plotting data from a file.


If you want to use the function command (i.e. you want to generate the .gnuplot file from within TikZ) with degrees, you can use raw gnuplot to provide the appropriate options:

\draw [smooth] plot[id=cosinex, raw gnuplot] function {set samples 500; set angles degrees; plot [0:360] cos(x)};

Or you could change the .gnuplot skeleton using

\makeatletter
{
  \catcode`\%=12
  \catcode`\"=12
  \xdef\pgf@gnuplot@head{set table \noexpand\pgf@plottablefile@quoted; set angles degrees; set format "%.5f"}
}
\makeatother

in your preamble. Then you can plot your function as desired:

\draw [domain=0:360, samples=500, smooth] plot[id=cosinex] function {cos(x)};
Related Question