Gnuplottex: Pass latex command to gnuplot script

I want to access and plot the same data from different LaTeX documents (a paper and a beamer presentation) using gnuplottex. The data and the gnuplot script is stored at some individual position relative to the two documents. To re-use as much code as possible, I would like to define the relative path to the data in each document in a macro, here \datapath.

My question is: How can I pass the value of this command to the gnuplot script? I would basically like to use the following in my MWE:

plot \datapath'/data.csv' using 1:2 with lines


I did find this thread. However I was unable to modify it to my needs. Can someone help me, please?

MWE

\documentclass{article}

\usepackage[latin1]{inputenx}
\usepackage{filecontents}
\usepackage[
miktex,    %
subfolder, % generated graphs in a ”gnuplottex” subfolder
cleanup,   % Delete the .gnuplot files after conversion
]{gnuplottex}

\newcommand{\datapath}{./ZZZ}

\begin{document}

% This is the data file to be plotted from
\begin{filecontents*}{\datapath/data.csv}
Col1,Col2
0,0
1,1
\end{filecontents*}

% This is the gnuplot script I would like to use the value of \datapath in
\begin{filecontents*}{\datapath/script.gnuplot}
set datafile separator "," # for csv-file
plot './ZZZ/data.csv' using 1:2 with lines
\end{filecontents*}

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

\end{document}

• Your MWE works fine for me on Linux with pdflatex. – Lars Kotthoff Feb 15 '18 at 17:22
• Sure, it works great, no question. But I want to be able to use plot \datapath'/data.csv' using 1:2 with lines instead of plot './ZZZ/data.csv' using 1:2 with lines. But how can I pass and expand the value of \datapath inside the gnuplot document or store the content of \datapath in a variable inside the gnuplot script as done in the post I linked to in my question? – raedma Feb 15 '18 at 17:30
• See stackoverflow.com/q/12328603/923955 I guess you need to do \renewcommand{\gnuplotexe}{gnuplot -e "dirname=./ZZZ"} and use dirname in the script file. I don't know about quoting, though, perhaps you don't need them. – egreg Feb 15 '18 at 18:25

You can do it with \immediate\write instead of filecontents*:

\newwrite\tempfile

\immediate\openout\tempfile=\datapath/script.gnuplot

• Thanks, but the data files and the gnuplot script exist as files at a defined location. filecontents* was only used here to provide a complete MWE. – raedma Feb 15 '18 at 17:56
• That exactly is my question. In this post the value of the line color is stored in a macro and expanded into a variable s inside the gnuplot script without writing it. So I guess it is possible. I just wasn't able to modify the approach to my problem. I always get the error that the files cannot be found. – raedma Feb 15 '18 at 18:02