# Plotting function defined with Lua doesn't do anything

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}

\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 n*factorial(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},
]
\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.

• You're forgetting tex.sprint. Lua's doing well, but TeX doesn't know your result unless you pass it from Lua.
– user226564
Jan 4, 2021 at 16:28
• If I either write {return tex.sprint(res)} or just {tex.spring(res)} it still plots nothing. How do I have to pass it? Jan 4, 2021 at 16:30
– user226564
Jan 4, 2021 at 16:35
• BTW it's tex.sprint(myfunction...). First define your functions in Lua and then send their results to TeX via \directlua{tex.sprint(myfunction...)}
– user226564
Jan 4, 2021 at 16:44
• Please read this and then post a minimal example. I've guessed your settings, but if you want better feedback, either edit your question or make a new question.
– user226564
Jan 4, 2021 at 17:18

You're not passing Lua results to TeX. You have to use tex.sprint (see this question by Henri Menke for some ideas and common mistakes). I won't discuss your approach to factorials, but I think you should use locals in Lua functions:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{luacode}
\begin{luacode*}

function factorial(n)
if (n == 0) then
return 1
else
return n*factorial(n-1)
end
end

function binom(n,k)
return factorial(n) / (factorial(n-k) * factorial(k))
end

function nbin(t)
local 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}
\begin{tikzpicture}[
declare function={nbin(\t) = \directlua{tex.sprint(nbin(\t))};}
]
\begin{axis}[
use fpu=false,
height=8 cm,
width = 8 cm,
samples at={0,1,...,40},
]