# Lua techniques and libraries for LuaTeX [closed]

With LuaTeX, it is possible to use many libraries and functions that can simplify various aspects of Lua programming. As Lua as language is quite new for the majority of TeX users, I think it would be good if we could share experiences in searching for useful libraries and techniques, that appeared to us while learning this language. Maybe we can collect a list of them.

Each answer should contain a description and a small example of usage.

Welcomed are examples using both LuaTeX (for typesetting) or TeXLua (for shell scripting).

Example:

String interpolation:

As simple templating engine one can use a technique called "string interpolation". Various attempts are described at lua wiki. A simple example is following:

``````#!/usr/bin/env texlua
function interp(s, tab)
return (s:gsub('(\$%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
print( interp("\${name} is \${value}", {name = "foo", value = "bar"}) )
``````

Which prints on the terminal:

``````foo is bar
``````

We can make it more simple to use:

``````getmetatable("").__mod = interp
print( "\${name} is \${value}" % {name = "foo", value = "bar"} )
``````

In this example, we set the `mod` operator for string to call our interpolation function, so one can use `"format \${val}" % {val="hello"}`.

-

## closed as off topic by topskip, lockstep, ℝaphink, Stephan Lehmke, MarcoApr 12 '12 at 17:03

Questions on TeX - LaTeX Stack Exchange are expected to relate to TeX, LaTeX or related typesetting systems within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

While I like the question, it is IMO off topic and should be on Stack Overflow. Your example has nothing to do with TeX or `texlua`. – topskip Apr 10 '12 at 19:46
As it stands, the question is about Lua programming in general, which seems better suited to StackOverflow than TeX-sx. Could the link with TeX and typesetting be enhanced, so for example Lua libraries which have an application in TeX work, perhaps requiring a MWE of use 'in the wild'? – Joseph Wright Apr 11 '12 at 7:09
@PatrickGundlach you are right, I will try to reformulate the question – michal.h21 Apr 11 '12 at 8:06