I saw power of LuaLaTeX through several examples on the TeX Stack Exchange site. Could you suggest some books or other resources to learn Lua programming? I want to learn about variables, loops, conditionals, string operations, writing functions etc. in Lua which then I can use in LaTeX.
There are three canonical resources to learn Lua programming.
“Programming in Lua” by Roberto Ierusalimschy
You can read the first edition online for free but since that was written for Lua 5.0 and the current version of Lua is 5.3, the usefulness of the online version is somewhat limited. The latest edition is available as a paperback and always covers the latest version of Lua. It is a really good book and I recommend you read it.
“Lua Programming Gems” edited by L. H. de Figueiredo, W. Celes, R. Ierusalimschy
This book presents implementations of various computational problems and algorithms in Lua. It also gives advice on how to write fast and efficient Lua code. However, it requires a good understanding of the Lua language already.
Lua Reference Manual
The reference manual is not really something you read cover to cover, but it lists all the functions and constructs known to the Lua interpreter, together with their parameters and occasional usage examples.
Since you are asking in the context of LuaTeX, there are some other resources to consider.
The LuaTeX manual
texdoc luatexor http://mirrors.ctan.org/systems/doc/luatex/luatex.pdf
The LuaTeX manual is a reference manual and does not attempt to teach how to use the Lua extensions of TeX. However, it lists all the available callbacks and methods. To learn about how to use them, I recommend the two articles
- P. Isambert. Three things you can do with LuaTeX that would be extremely painful otherwise. TUGboat, 31(3):184–190, 2010. https://tug.org/TUGboat/tb31-3/tb99isambert.pdf
- P. Isambert. LuaTeX: What it takes to make a paragraph TUGboat, 32(1):68–76, 2011. https://tug.org/TUGboat/tb32-1/tb100isambert.pdf
Because these two articles are also a little dated, some of the functionality presented therein has been removed meanwhile, most notably the
token_filtercallback. Of course there are many more articles in TUGboat dealing with LuaTeX.
Further LuaTeX online resources
LuaTeX project website with some older presentations.
LuaTeX Wiki with examples for callbacks and nodes.
ConTeXt Wiki page about Lua. The packages
lualibs(the latter being loaded by the former) are actually extracted from ConTeXt to work with Lua(La)TeX. Therefore some of the ConTeXt extensions to the Lua language are available in Lua(La)TeX as well.
I can strongly recommend becoming familiar with the luacode package, which provides various tools to make it more straightforward to get Lua code up and running inside a LuaLaTeX document.
The package complements the basic
\directluainstruction with four additional interfaces -- the macros
\luaexec, and the environments
luacode*. These interfaces differ in the way TeX-special characters such as
\can (or, rather, must) be entered and passed back and forth between the LaTeX and Lua sides of things.
The package also provides the user-level macros
\luastringO. These can be extremely useful for passing strings of various degrees of complexity from LaTeX to Lua.
The package's user guide is very clear and nicely written.
I mostly use Lua in complex TikZ / PGFplots figures, where I can use a more convenient language that outputs LaTeX code without any extra compilation steps. I started learning from these two documents:
- (Note that this one is quite outdated, as pointed out in the comments) Pégourié-Gonnard, Manuel, "A Guide to LuaLaTeX"
- Montijano, Juan I. and Pérez, Mario and Rández, Luis and Varona, Juan Luis, "Numerical Methods with LuaLaTeX"