LuaLaTeX:PGFplots 无法读取 Lua 传递的数据

LuaLaTeX:PGFplots 无法读取 Lua 传递的数据

我想用 LuaLaTeX 来回答这个问题

这是我写的清单:

% !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}

但遇到的错误是:

! 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..

为什么?我该如何避免?

答案1

有两个问题:第一个\n

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

第二个是在开始扫描\PrintData之前进行扩展;因此删除并写入\addplot\n

\begin{tikzpicture}
\begin{axis}
\begingroup\edef\x{\endgroup
   \noexpand\addplot coordinates{\printData{0}{10}{10}{3}};}\x
\end{axis}
\end{tikzpicture}

这样\addplot就会找到已经扩展的坐标。

答案2

我的最终解决方案是“上游”TeX 代码。我更改了传递命令的LuaprintData函数。print_chiPFtex.print\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}

相关内容