[NTG-context] Sending data from lua to context and back to lua

Rudolf Bahr quasi at quasi.de
Thu Feb 8 06:51:58 CET 2018


On Wed, Feb 07, 2018 at 06:39:53PM +0100, Bahr Rudolf wrote:
> 
> Hello Contexteers!
> 
> Sometimes the necessity could arise to send data from lua to context
> and answers back to lua. I tried this by using "read" and
> "immediate\write" commands in context. These commands are described
> in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
> 
> My question is: Do there exist better and more simple solutions?
> Here is my WE which is admittedly a little bit ugly, but it works.
> Appended the pdf-output:
> 
> ========================================================================
> \starttext
> 
> \startluacode
> 
> tex.print("Here is lua.")
> tex.print(" ")
> tex.print("I'm sending some picture names to Context in order to get back their sizes.")
> 
> local imageA = "2007-01-01---Intro.png"
> local imageB = "2007-01-01---Neujahrsballade---1.png"
> local imageC = "2007-01-01---Neujahrsballade---2.png"
> 
> local f = assert(io.open("contextin.txt", "w"))
> f:write(imageA .. "\n" .. imageB .. "\n" .. imageC)
> f:close()
> 
> \stopluacode
> 
> % -------------------------------------------------------------
> 
> \openin15=contextin.txt
> 
> \read15 to \zeileA
> \read15 to \zeileB
> \read15 to \zeileC
> 
> \closein15
> 
> \vskip\lineheight
> =====================================================
> \vskip\lineheight
> Here is \CONTEXT\ and what I got from Lua:
> \vskip\lineheight
> 
> \zeileA \\
> \zeileB \\
> \zeileC
> 
> \vskip\lineheight
> Now I'm working ... and write my answers to a file for lua: \\
> 
> \zeileA\ width=3000pt height=2000pt \\
> \zeileB\ width=1200pt height=900pt  \\
> \zeileC\ width=1600pt height=1200pt \\
> 
> \immediate\openout7=contextout.txt
> 
> \immediate\write7{\zeileA\ width=3000pt height=2000pt \\}
> \immediate\write7{\zeileB\ width=1200pt height=900pt  \\}
> \immediate\write7{\zeileC\ width=1600pt height=1200pt \\}
> 
> \immediate\closeout7
> 
> \vskip\lineheight
> =====================================================
> \vskip\lineheight
> 
> % -------------------------------------------------------------
> 
> \startluacode
> 
> tex.print("Here is lua again and what I got back from Context:")
> tex.print(" ")
> tex.print(" ")
> 
> local f = assert(io.open("contextout.txt", "r"))
> local contexts_answer = f:read("*a")
> f:close()
> 
> tex.print(contexts_answer)
> 
> \stopluacode
> 
> 
> \stoptext
> 
> ========================================================================
> 
> Best regards,
> 
> Rudolf
> 

Sorry, Donald E. Knuth's macros aren't in chapters 21 and 22, but in
chapters 20 and 21!

Rudolf 



More information about the ntg-context mailing list