5

Is it possible to read a number from data files?

How to read a file with luatex

I found the example above but I don't need the whole line at once.

I need somthing like:

read(file)
get(A)
get(B)
X=A+B
print(X)

Sorry for my bad English. :)


Here is my example but it doesnt work :/

\documentclass{scrartcl}
\usepackage{luatextra}
\usepackage{filecontents}

 \begin{luacode}
  function addplot()
  filename = "testdata.dat"
  tex.print('\\addplot coordinates {')
  for line in io.lines(filename) do
  local tab = string.explode(line)
  x=tab[1]
  y=math.log10(tab[2])
  tex.print('('..x..','..y..')')
 end
 tex.print('};')
 end
\end{luacode}

\begin{document}
\begin{tikzpicture}
\begin{axis}

\directlua{addplot()}

\end{axis}
\end{tikzpicture}
\end{document}

Removing the \\ doesnt work.

  • The readarray package reads files and allows recall of individual data entries from the file. – Steven B. Segletes Apr 22 '13 at 15:52
  • I'm trying to understand exactly what you want. Then, I would try an answer. Do you want a file filled with two columns of numbers (A and B), and be able to arbitrarily specify recall of those numbers from a file so they can be added? – Steven B. Segletes Apr 22 '13 at 16:00
8

\usepackage{filecontents} should be replaced by \usepackage{pgfplots}. \usepackage{luatextra} should be replaced by \usepackage{luacode}. You should also remove the two backslashes at {:

\documentclass{scrartcl}
\usepackage{pgfplots}
\usepackage{luacode}

\begin{filecontents*}{testdata.dat}
  1.0 20
  1.1 21
  1.2 22
\end{filecontents*}

\begin{luacode*}
  function addplot()
  filename = "testdata.dat"
  tex.print('\\addplot coordinates {')
  for line in io.lines(filename) do
    local tab = string.explode(line)
    x=tab[1]
    y=math.log10(tab[2])
    tex.print('('..x..','..y..')')
 end
 tex.print('};')
 end
\end{luacode*}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\directlua{addplot()}
\end{axis}
\end{tikzpicture}
\end{document}

And the output is:

plot

  • Oh i forgot the pgfplots package -.- thank you very much! :) – user29463 Apr 23 '13 at 11:54
  • @LukasSchulig If it works for you, you should accept my answer, so that future users know that your problem is solved. – topskip Apr 23 '13 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy