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

Mico
  • 506,678
  • 1
    You're forgetting tex.sprint. Lua's doing well, but TeX doesn't know your result unless you pass it from Lua. –  Jan 04 '21 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 04 '21 at 16:30
  • See my answer, please –  Jan 04 '21 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...)} –  Jan 04 '21 at 16:44
  • Yes, I did that. It is working in your standalone class but in my bigger .tex its just not and I don't know why. I just use the same packages stated below and plenty of others. Are there some packages that do not work with Tikz, Lua and pgfplots? – stephank Jan 04 '21 at 17:11
  • 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. –  Jan 04 '21 at 17:18
  • I added some packages. I inserted the same code you wrote down here in the code.tex but its not drawing. – stephank Jan 04 '21 at 18:02
  • How strange. It works fine in Overleaf –  Jan 04 '21 at 18:35

1 Answers1

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

  • Thank you very much! The Function doesn't look like I wanted but at least it's drawing something now. – stephank Jan 04 '21 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 https://www.lua.org/manual/5.3/manual.html#6.7 –  Jan 04 '21 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 04 '21 at 16:49