[luatex] TeX-Lua interaction

Dirk Laurie dirk.laurie at gmail.com
Tue Sep 11 21:35:50 CEST 2012


2012/9/11 Ralf Meyer <rameyer at uni-math.gwdg.de>:
> I am using LuaTeX to write a couple of letters to several people whose
> addressses I have in a csv file, which I process using Lua.  The way I
> do this, all the work is done in lua.  The LaTeX file just loads a
> couple of packages and then use dofile to let Lua take over.  In
> particular, the text of the letters is contained in the Lua file and
> only given back to TeX through tex.print.  This works, but is somewhat
> inconvenient because of the need to double backslashes, etc.
>
> I am wondering whether this is the best way to do this.  Is there a way
> to write, say, a for-loop in TeX that runs over a Lua variable?  In my
> case, the letters have identical text and only differ in the address field.
>

I would certainly do all looping, logic etc in Lua,
not in TeX.

---- file demo.lua
return {"This is an example",
"with a table of several strings",
[[in which {\bf{only}} single backslashes]],
"are needed."}
----

%%%% file demo.tex
\directlua{ tex.print(dofile"demo.lua") }
\end
%%%%

Actual application (not tested):

---- file letters.lua

letter = [[
%s

Dear %s,

Wonderful news! A new line in espionage gadgets
is available at my shop.

Sincerely,
Kurt

\pagebreak
]]

addr = {
  {"James",[[
James Bond
c/o MI5
London]]},
  {"Ian",[[
Ian Fleming
Elysian Fields]]}
}

letters = {}

for i=1,#addr do letters[#letters+1]=letter:format(addr[i][2],addr[i][1]) end


More information about the luatex mailing list