# [luatex] writing and reading a file

Herbert Voss Herbert.Voss at FU-Berlin.DE
Sun Feb 2 20:16:06 CET 2014

Hello all,

I have a problem with the following example. It creates the
external data file "tmp.data" with the \directlua call. This
file is then read by \addplot. You can choose any kind of
values, e.g. 4000, 500, or whatever instead of the 5000 steps,
\addplot is missing some of the rightmost data.

For a second run, comment the \directlua call (the data file
is already created by the preceding run). The output is now
correct.

I suppose that the data file is closed too late.
But I am not sure.

Herbert

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{luacode}
\begin{luacode}
function weierstrass(x0, x1, n, a, b, epsilon)
local dx = (x1-x0)/n
local x = x0
local out=assert(io.open("tmp.data","w"))
local y,k,dy
while (x <= x1) do
y = 0
k = 0
repeat
dy = math.pow(a,k)*math.cos(math.pow(b,k)*math.pi*x)
y = y + dy
k = k + 1
until (math.abs(dy) < epsilon)
out:write( x, " ", y ,"\string\n")
x = x + dx
end
out.close()
end
\end{luacode}
\begin{document}

\directlua{weierstrass(-2, 2, 5000, 0.3 ,5 ,1.e-12)}

\begin{tikzpicture}
\begin{axis}[axis lines=middle]