Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
    return z+gamma*z^2+coeff*z^3+quad*z^4+qui*z^5+set*z^6
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}


\addplot coordinates{\printData{0}{10}{10}{3}};

\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?

share|improve this question

2 Answers 2

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
   \noexpand\addplot coordinates{\printData{0}{10}{10}{3}};}\x
\end{axis}
\end{tikzpicture}

so that \addplot will find the coordinates already expanded.

share|improve this answer
up vote 7 down vote accepted

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
    return z+gamma*z^2+coeff*z^3+quad*z^4+qui*z^5+set*z^6
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
        tex.sprint("\\addplot["..option.."] coordinates{")
    else
        tex.sprint("\\addplot coordinates{")
    end
    for i=1,n do
        y = chi_PF(x,k)
        tex.sprint("("..x..","..y..")")
        x = x+delta
    end
    tex.sprint("};")

end
\end{luacode*}

\newcommand\addplotCHIpf[5][]{\directlua{print_chiPF(#2,#3,#4,#5,[[#1]])}}

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

\foreach \k in {1,2,3,4}{
\addplotCHIpf{0.001}{10}{500}{\k}
}

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.