hopefully you can help me.
I tried to plot a function with Tikz and Lua in the following way
\documentclass[14pt]{book}
\usepackage{marginnote}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{matrix}
\usetikzlibrary{calc}
\usetikzlibrary{positioning,shapes}
\usetikzlibrary{tikzmark}
\usetikzlibrary{arrows}
\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\tikzstyle{abstract}=[rectangle, draw=black, rounded corners,
text centered, anchor=north, text=black, text width=2.7 cm]
\tikzstyle{comment}=[rectangle, draw=black, rounded corners, fill=green,
text centered, anchor=north, text=white, text width=3cm]
\tikzstyle{myarrow}=[->, >=open triangle 90, thick]
\tikzstyle{line}=[-, thick]
\usepackage{luacode}
\begin{luacode}
function factorial(n)
if (n == 0) then
return 1
else
return nfactorial(n-1)
end
end
function binom(n,k)
return factorial(n) / (factorial(n-k) * factorial(k))
end
function nbin(t)
res = 0
for k = 0, math.floor(t), 1 do
res = res + (binom(k+2,2) * 1/8 * (1/2)^k)
end
return res
end
\end{luacode*}
\begin{document}
\input{code.tex}
\sloppy
\end{document}
And the picture is in code.tex using
\begin{tikzpicture}[
declare function={nbin(\t) = \directlua{nbin(\t)};}
]
\begin{axis}[
use fpu=false,
height=8 cm,
width = 8 cm,
samples at={0,1,...,40},
]
\addplot{nbin(x)};
\end{axis}
\end{tikzpicture}
I'm kind of new to all that Tikz stuff, but I can't find solutions here :-/ This prints just a white box with 0, 0.2, ..., 1 at the axis and no errors. What's wrong here?
I edited my question because it is running in the standalone environment but not in my setting.

tex.sprint. Lua's doing well, but TeX doesn't know your result unless you pass it from Lua. – Jan 04 '21 at 16:28tex.sprint(myfunction...). First define your functions in Lua and then send their results to TeX via\directlua{tex.sprint(myfunction...)}– Jan 04 '21 at 16:44