# LuaLaTeX:PGFplots can't read data passed by Lua

I wanted to give a LuaLaTeX answer to this question

Here it is the listing I wrote:

% !TEX encoding   = UTF-8
% !TEX program    = LuaLaTeX
% !TEX spellcheck = en_GB
\documentclass{standalone}

\usepackage{pgfplots}

\usepackage{luacode}

\begin{luacode*}
-- Code taken by
-- http://rosettacode.org/wiki/Gamma_function#Lua
function recigamma(z)
local gamma =  0.577215664901
local coeff = -0.65587807152056
local quad  = -0.042002635033944
local qui   =  0.16653861138228
local set   = -0.042197734555571
end

function gamma(z)
if z == 1 then
return 1
elseif math.abs(z) <= 0.5 then
return 1/recigamma(z)
else
return (z-1)*gamma(z-1)
end
end

-- From Wikipedia
-- http://en.wikipedia.org/wiki/Chi-squared_distribution
function chi_PF(x,k)
if x<0 then
return 0
else
return (1/(2^(k/2)*gamma(k/2)))*x^(k/2-1)*math.exp(-x/2)
end
end

-- Code to write PGFplots data as coordinates
function printData(xMin,xMax,n,k)
local delta = (xMax-xMin)/(n-1)
local     x = xMin
--tex.sprint("{")
for i=1,n do
y = chi_PF(x,k)
tex.print("("..x..","..y..")\n")
x = x+delta
end
--tex.sprint("}")

end
\end{luacode*}

\def\printData#1#2#3#4{\directlua{printData(#1,#2,#3,#4)}}

\begin{document}
\begin{tikzpicture}
\begin{axis}

\end{axis}
\end{tikzpicture}
\end{document}


But the error encountered is:

! Package pgfplots Error: Sorry, I could not read the plot coordinates near '(0,0)
(1.1111111111111,0.2412971123005)
(2.2222222222222,0.19579085829747)
(3.3333333333333,0.13758254145982)
(4.4444444444444,0.091150275097439)
(5.5555555555556,0.058470697966872)
(6.6666666666667,0.036749733066543)
(7.7777777777778,0.022774710835611)
(8.8888888888889,0.013969283552714)
(10,0.0085011109038277)
'. Please check for format mistakes..


Why? How can I avoid it?

-

There are two problems: the first is the \n in the line

tex.print("("..x..","..y..")\n")


and the second is to expand \PrintData before \addplot starts its scanning; so remove \n and write

\begin{tikzpicture}
\begin{axis}
\begingroup\edef\x{\endgroup
\end{axis}
\end{tikzpicture}


so that \addplot will find the coordinates already expanded.

-

My final solution acts "upstream" TeX code. I changed the Lua printData function in print_chiPF where the tex.print pass also the command \addplot.

% !TEX encoding   = UTF-8
% !TEX program    = LuaLaTeX
% !TEX spellcheck = en_GB
\documentclass{standalone}

\usepackage{pgfplots}

\usepackage{luacode}

\begin{luacode*}
-- Code taken by
-- http://rosettacode.org/wiki/Gamma_function#Lua
function recigamma(z)
local gamma =  0.577215664901
local coeff = -0.65587807152056
local quad  = -0.042002635033944
local qui   =  0.16653861138228
local set   = -0.042197734555571
end

function gamma(z)
if z == 1 then
return 1
elseif math.abs(z) <= 0.5 then
return 1/recigamma(z)
else
return (z-1)*gamma(z-1)
end
end

-- From Wikipedia
-- http://en.wikipedia.org/wiki/Chi-squared_distribution
function chi_PF(x,k)
if x<0 then
return 0
else
return (1/(2^(k/2)*gamma(k/2)))*x^(k/2-1)*math.exp(-x/2)
end
end

-- Code to write PGFplots data as coordinates
function print_chiPF(xMin,xMax,n,k,option)
local delta  = (xMax-xMin)/(n-1)
local     x  = xMin
if option~=[[]] then
else
end
for i=1,n do
y = chi_PF(x,k)
tex.sprint("("..x..","..y..")")
x = x+delta
end
tex.sprint("};")

end
\end{luacode*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[no marks,
xmin = 0,
xmax = 6,
ymin = 0,
ymax = 1]

\foreach \k in {1,2,3,4}{