[luatex] writing and reading a file

Philipp Gesang philipp.gesang at alumni.uni-heidelberg.de
Sun Feb 2 20:38:26 CET 2014


Hi Herbert!

···<date: 2014-02-02, Sunday>···<from: Herbert Voss>···

> 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.
> 
> \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()

   out:close()

The close() function must be called with the file as the first
argument. out.close() means calling it with no argument; use
out:close() or out.close(out) instead.

http://www.lua.org/manual/5.2/manual.html#pdf-io.close

Best,
Philipp




> end
> \end{luacode}
> \begin{document}
> 
> \directlua{weierstrass(-2, 2, 5000, 0.3 ,5 ,1.e-12)}
> 
> \begin{tikzpicture}
> \begin{axis}[axis lines=middle]
>    \addplot [thick] table {tmp.data};
> \end{axis}
> \end{tikzpicture}
> 
> \end{document}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://tug.org/pipermail/luatex/attachments/20140202/29cd3e39/attachment.bin>


More information about the luatex mailing list