5

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 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},
    ]
    \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.

9
  • 1
    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?
    – stephank
    Jan 4, 2021 at 16:30
  • See my answer, please
    – 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
  • 1
    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

1 Answer 1

7

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}
\pgfplotsset{compat=newest}
\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},
    ]
    \addplot{nbin(x)};
  \end{axis}
\end{tikzpicture}
\end{document}

I think PGF provides some math functions, but I'm not aware of them.

enter image description here

3
  • Thank you very much! The Function doesn't look like I wanted but at least it's drawing something now.
    – stephank
    Jan 4, 2021 at 16:38
  • @stephank Oh, well, that's not my fault :) If you want to know how do math functions work in Lua, please see lua.org/manual/5.3/manual.html#6.7
    – user226564
    Jan 4, 2021 at 16:41
  • 1
    yeah I didn't mean that its your fault, but I couldn't see the result before ;-). Thanks for that link!
    – stephank
    Jan 4, 2021 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .