I am trying to write a macro for doing quadratic regression in pgfplots. I have
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{gnuplottex}
\usepackage{pgfkeys}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}
\usepackage{xparse}
\usepackage{xstring} % for \IfStrEq, \IfInteger
\makeatletter
% https://tex.stackexchange.com/a/50113/2066
\newcommand{\IsInteger}[3]{%
\IfStrEq{#1}{ }{%
#3% is a blank string
}{%
\IfInteger{#1}{#2}{#3}%
}%
}%
\newcommand{\pgftognucolumnset}[2]{%
\IsInteger{\pgfkeysvalueof{#1}}{%
% pgf 0-indexes columns, while gnuplot 1-indexes columns, so we add 1 to adjust
\edef#2{\the\numexpr\pgfkeysvalueof{#1}+1\relax}%
}{%
\edef#2{(column("\pgfkeysvalueof{#1}"))}%
}%
}
\makeatletter
% \addplotquadraticregression[params for \addplot][default settings for a and b and c, also for x and y columns]{table file}
\NewDocumentCommand{\addplotquadraticregression}{ O{no markers} o m}{%
\pgfkeyssetvalue{/addplotquadraticregression/x}{0}
\pgfkeyssetvalue{/addplotquadraticregression/y}{1}
\pgfkeyssetvalue{/addplotquadraticregression/a}{1}
\pgfkeyssetvalue{/addplotquadraticregression/b}{1}
\pgfkeyssetvalue{/addplotquadraticregression/c}{1}
\pgfkeys{/addplotquadraticregression/.cd,#2}
\pgftognucolumnset{/addplotquadraticregression/x}{@addplotquadraticregression@colx}%
\pgftognucolumnset{/addplotquadraticregression/y}{@addplotquadraticregression@coly}%
\edef@addplotquadraticregression@inita{\pgfkeysvalueof{/addplotquadraticregression/a}}%
\edef@addplotquadraticregression@initb{\pgfkeysvalueof{/addplotquadraticregression/b}}%
\edef@addplotquadraticregression@initc{\pgfkeysvalueof{/addplotquadraticregression/c}}%
\addplot [#1] gnuplot [raw gnuplot] { % allows arbitrary gnuplot commands
f(x) = ax*2+bx+c; % Define the function to fit
% Set reasonable starting values here
a=@addplotquadraticregression@inita;
b=@addplotquadraticregression@initb;
c=@addplotquadraticregression@initc;
fit f(x) '#3' u @addplotquadraticregression@colx:@addplotquadraticregression@coly\space via a,b,c; % Select the file
stats '#3' u @addplotquadraticregression@colx;
plot [x=STATS_min:STATS_max] f(x); % Specify the range to plot
set print "#3-parameters.dat"; % Open a file to save the parameters
print a, b, c; % Write the parameters to file
};
\pgfplotstableread{#3-parameters.dat}\parameters % Open the file Gnuplot wrote
\pgfplotstablegetelem{0}{0}\of\parameters \xdef\pgfplotstableregressiona{\pgfplotsretval} % Get first element, save into \pgfplotstableregressiona
\pgfplotstablegetelem{0}{1}\of\parameters \xdef\pgfplotstableregressionb{\pgfplotsretval}
\pgfplotstablegetelem{0}{2}\of\parameters \xdef\pgfplotstableregressionc{\pgfplotsretval}
}
\makeatother
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{data.txt}
param-0-univ-count param-1-n abstract-regression-quadratic-regression-cubic-real abstract-regression-quadratic-regression-cubic-sys abstract-regression-quadratic-regression-cubic-user close-abstract-regression-quadratic-regression-cubic-real close-abstract-regression-quadratic-regression-cubic-sys close-abstract-regression-quadratic-regression-cubic-user exact-regression-quadratic-regression-cubic-regression-linear-real exact-regression-quadratic-regression-cubic-regression-linear-sys exact-regression-quadratic-regression-cubic-regression-linear-user
1 0 0. 0. 0. 0. 0. 0. 0. 0. 0.
2 1 0. 0. 0. 0. 0. 0. 0. 0. 0.
4 2 0. 0. 0. 0. 0. 0. 0. 0. 0.
8 3 0.001 0. 0.001 0. 0. 0. 0. 0. 0.
16 4 0.002 0.002 0. 0.001 0.001 0. 0. 0. 0.
32 5 0.005 0. 0.004 0.003 0. 0.003 0.001 0. 0.001
64 6 0.01 0.006 0.003 0.006 0.006 0. 0.003 0. 0.003
128 7 0.068 0.004 0.063 0.06 0.004 0.056 0.007 0. 0.007
256 8 0.036 0. 0.036 0.026 0. 0.026 0.009 0. 0.009
512 9 0.104 0. 0.104 0.084 0. 0.084 0.019 0. 0.019
1024 10 0.262 0.003 0.258 0.221 0.003 0.217 0.04 0. 0.04
2048 11 0.798 0.003 0.794 0.692 0.003 0.688 0.105 0. 0.105
4096 12 2.39 0.003 2.386 2.179 0. 2.179 0.211 0.003 0.207
8192 13 8.301 0.016 8.285 7.866 0.012 7.853 0.434 0.003 0.43
16384 14 54.838 0.031 54.805 53.947 0.015 53.93 0.89 0.015 0.875
\end{filecontents}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[xlabel=0-univ-count,
ylabel=time (s),
legend pos=outer north east,
width=0.95\textwidth,
axis lines=left,
xmin=0,
ymin=0,
scaled x ticks=false,
scaled y ticks=false]
\addplot[only marks,mark=o,color=red] table[x=param-0-univ-count,y=abstract-regression-quadratic-regression-cubic-real]{data.txt};
\addlegendentry{abstract-real}
\addplotquadraticregression[no markers, mark=o, color=red][x=param-0-univ-count,y=abstract-regression-quadratic-regression-cubic-real]{data.txt};
\addlegendentry{$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x^2 v\pgfmathprintnumber[print sign]{\pgfplotstableregressionb} \cdot x \pgfmathprintnumber[print sign]{\pgfplotstableregressionc}$}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
This works pretty well:
However, if I try to use an inline table from a macro rather than from a file, it fails; replacing the body of the document with
\begin{document}
\begin{figure}
\begin{tikzpicture}
\pgfplotstableread{
param-0-univ-count param-1-n abstract-regression-quadratic-regression-cubic-real abstract-regression-quadratic-regression-cubic-sys abstract-regression-quadratic-regression-cubic-user close-abstract-regression-quadratic-regression-cubic-real close-abstract-regression-quadratic-regression-cubic-sys close-abstract-regression-quadratic-regression-cubic-user exact-regression-quadratic-regression-cubic-regression-linear-real exact-regression-quadratic-regression-cubic-regression-linear-sys exact-regression-quadratic-regression-cubic-regression-linear-user
1 0 0. 0. 0. 0. 0. 0. 0. 0. 0.
2 1 0. 0. 0. 0. 0. 0. 0. 0. 0.
4 2 0. 0. 0. 0. 0. 0. 0. 0. 0.
8 3 0.001 0. 0.001 0. 0. 0. 0. 0. 0.
16 4 0.002 0.002 0. 0.001 0.001 0. 0. 0. 0.
32 5 0.005 0. 0.004 0.003 0. 0.003 0.001 0. 0.001
64 6 0.01 0.006 0.003 0.006 0.006 0. 0.003 0. 0.003
128 7 0.068 0.004 0.063 0.06 0.004 0.056 0.007 0. 0.007
256 8 0.036 0. 0.036 0.026 0. 0.026 0.009 0. 0.009
512 9 0.104 0. 0.104 0.084 0. 0.084 0.019 0. 0.019
1024 10 0.262 0.003 0.258 0.221 0.003 0.217 0.04 0. 0.04
2048 11 0.798 0.003 0.794 0.692 0.003 0.688 0.105 0. 0.105
4096 12 2.39 0.003 2.386 2.179 0. 2.179 0.211 0.003 0.207
8192 13 8.301 0.016 8.285 7.866 0.012 7.853 0.434 0.003 0.43
16384 14 54.838 0.031 54.805 53.947 0.015 53.93 0.89 0.015 0.875
}{\testdata}
\begin{axis}[xlabel=0-univ-count,
ylabel=time (s),
legend pos=outer north east,
width=0.95\textwidth,
axis lines=left,
xmin=0,
ymin=0,
scaled x ticks=false,
scaled y ticks=false]
\addplot[only marks,mark=o,color=red] table[x=param-0-univ-count,y=abstract-regression-quadratic-regression-cubic-real]{\testdata};
\addlegendentry{abstract-real}
\addplotquadraticregression[no markers, mark=o, color=red][x=param-0-univ-count,y=abstract-regression-quadratic-regression-cubic-real]{\testdata};
\addlegendentry{$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x^2 \pgfmathprintnumber[print sign]{\pgfplotstableregressionb} \cdot x \pgfmathprintnumber[print sign]{\pgfplotstableregressionc}$}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
leads to
! Undefined control sequence.
\testdata ->\pgfpl@@
{param-0-univ-count}\pgfpl@@ {param-1-n}\pgfpl@@ {abstr...
l.97 ...uadratic-regression-cubic-real]{\testdata}
;
?
This is because I can't just use the macro identifying the table in gnuplot (well, more accurately, I can't even write the macro to file, because it contains undefined submacros, namely, pgfpl@@).
If I can convert the table to plain text, I can insert it into a gnuplot inline datablock with $Mydata << EOD ... EOD. However, I can't seem to find a way to recover the raw text once pgfplotsdata has read the table in. For example, \pgfplotstabletypeset[begin table=,end table=]{\testdata} gives me something like
{ccccccccccc}%
param-0-univ-count¶m-1-n&abstract-regression-quadratic-regression-cubic-real&abstract-regression-quadratic-regression-cubic-sys&abstract-regression-quadratic-regression-cubic-user&close-abstract-regression-quadratic-regression-cubic-real&close-abstract-regression-quadratic-regression-cubic-sys&close-abstract-regression-quadratic-regression-cubic-user&exact-regression-quadratic-regression-cubic-regression-linear-real&exact-regression-quadratic-regression-cubic-regression-linear-sys&exact-regression-quadratic-regression-cubic-regression-linear-user\\%
\pgfutilensuremath {1}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}\\%
\pgfutilensuremath {2}&\pgfutilensuremath {1}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}\\%
\pgfutilensuremath {4}&\pgfutilensuremath {2}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}\\%
\pgfutilensuremath {8}&\pgfutilensuremath {3}&\pgfutilensuremath {1\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {1\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}\\%
\pgfutilensuremath {16}&\pgfutilensuremath {4}&\pgfutilensuremath {2\cdot 10^{-3}}&\pgfutilensuremath {2\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {1\cdot 10^{-3}}&\pgfutilensuremath {1\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}&\pgfutilensuremath {0}\\%
\pgfutilensuremath {32}&\pgfutilensuremath {5}&\pgfutilensuremath {5\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {4\cdot 10^{-3}}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {1\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {1\cdot 10^{-3}}\\%
\pgfutilensuremath {64}&\pgfutilensuremath {6}&\pgfutilensuremath {1\cdot 10^{-2}}&\pgfutilensuremath {6\cdot 10^{-3}}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {6\cdot 10^{-3}}&\pgfutilensuremath {6\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {3\cdot 10^{-3}}\\%
\pgfutilensuremath {128}&\pgfutilensuremath {7}&\pgfutilensuremath {6.8\cdot 10^{-2}}&\pgfutilensuremath {4\cdot 10^{-3}}&\pgfutilensuremath {6.3\cdot 10^{-2}}&\pgfutilensuremath {6\cdot 10^{-2}}&\pgfutilensuremath {4\cdot 10^{-3}}&\pgfutilensuremath {5.6\cdot 10^{-2}}&\pgfutilensuremath {7\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {7\cdot 10^{-3}}\\%
\pgfutilensuremath {256}&\pgfutilensuremath {8}&\pgfutilensuremath {3.6\cdot 10^{-2}}&\pgfutilensuremath {0}&\pgfutilensuremath {3.6\cdot 10^{-2}}&\pgfutilensuremath {2.6\cdot 10^{-2}}&\pgfutilensuremath {0}&\pgfutilensuremath {2.6\cdot 10^{-2}}&\pgfutilensuremath {9\cdot 10^{-3}}&\pgfutilensuremath {0}&\pgfutilensuremath {9\cdot 10^{-3}}\\%
\pgfutilensuremath {512}&\pgfutilensuremath {9}&\pgfutilensuremath {0.1}&\pgfutilensuremath {0}&\pgfutilensuremath {0.1}&\pgfutilensuremath {8.4\cdot 10^{-2}}&\pgfutilensuremath {0}&\pgfutilensuremath {8.4\cdot 10^{-2}}&\pgfutilensuremath {1.9\cdot 10^{-2}}&\pgfutilensuremath {0}&\pgfutilensuremath {1.9\cdot 10^{-2}}\\%
\pgfutilensuremath {1{,}024}&\pgfutilensuremath {10}&\pgfutilensuremath {0.26}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0.26}&\pgfutilensuremath {0.22}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0.22}&\pgfutilensuremath {4\cdot 10^{-2}}&\pgfutilensuremath {0}&\pgfutilensuremath {4\cdot 10^{-2}}\\%
\pgfutilensuremath {2{,}048}&\pgfutilensuremath {11}&\pgfutilensuremath {0.8}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0.79}&\pgfutilensuremath {0.69}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0.69}&\pgfutilensuremath {0.11}&\pgfutilensuremath {0}&\pgfutilensuremath {0.11}\\%
\pgfutilensuremath {4{,}096}&\pgfutilensuremath {12}&\pgfutilensuremath {2.39}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {2.39}&\pgfutilensuremath {2.18}&\pgfutilensuremath {0}&\pgfutilensuremath {2.18}&\pgfutilensuremath {0.21}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0.21}\\%
\pgfutilensuremath {8{,}192}&\pgfutilensuremath {13}&\pgfutilensuremath {8.3}&\pgfutilensuremath {1.6\cdot 10^{-2}}&\pgfutilensuremath {8.29}&\pgfutilensuremath {7.87}&\pgfutilensuremath {1.2\cdot 10^{-2}}&\pgfutilensuremath {7.85}&\pgfutilensuremath {0.43}&\pgfutilensuremath {3\cdot 10^{-3}}&\pgfutilensuremath {0.43}\\%
\pgfutilensuremath {16{,}384}&\pgfutilensuremath {14}&\pgfutilensuremath {54.84}&\pgfutilensuremath {3.1\cdot 10^{-2}}&\pgfutilensuremath {54.81}&\pgfutilensuremath {53.95}&\pgfutilensuremath {1.5\cdot 10^{-2}}&\pgfutilensuremath {53.93}&\pgfutilensuremath {0.89}&\pgfutilensuremath {1.5\cdot 10^{-2}}&\pgfutilensuremath {0.88}\\%
How do I just get the raw text of the table back out? Or how do I reprocess the cells to spit them out in a format gnuplot can handle?

