When I need to draw datapoints from a external data file I’m using lua, like this:

\starttext

\startluacode

Elemento = {}
x = {}
y ={}
z= {}
carga = {}
i=1
context("\\startMPcode”)

context("numeric u; u := 1.5cm;")
for line in io.lines("molecule.xyz") do
   local a, b, c, d, e = string.match(line, '(%a+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)')
   Elemento[i]= a
   x[i]= tonumber(b)
   y[i]= tonumber(c)
   z[i]= tonumber(d)
   carga[i]= tonumber(e)

   context("label(\"%s\", (%0.6f u, %0.6f u));", a, x[i],y[i])

   i=i+1 
end

context("\\stopMPcode”)

\stopluacode


\stoptext

Is there a way to pass directly the external data to MetaPost without using Lua?

I know that I can use a similar function to gdata from graph.mp. I try to reuse this piece of code, but not works.

\startMPcode

vardef Grdln_@#(expr f) =
  save n_, s_; string s_;
  s_ = readfrom f;
  string @#[];
  if s_<>EOF:
    @#1 := loptok s_;
    n_ = if @#1="%": 0 else: 1 fi;
    forever:
      @#[incr n_] := loptok s_;
      exitif @#[n_]="  ";
    endfor
    @#1<>"  "
  else: false
  fi
enddef;

Grdln_$("molecule.xyz");

\stopMPcode


Attachment: molecule.xyz
Description: Binary data

Attachment: read_external.tex
Description: Binary data

Jorge


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to