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.
3 Answers
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 luatex
or http://mirrors.ctan.org/systems/doc/luatex/luatex.pdfThe 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_filter
callback. Of course there are many more articles in TUGboat dealing with LuaTeX.Further LuaTeX online resources
http://www.luatex.org/documentation.html
LuaTeX project website with some older presentations.
http://wiki.luatex.org/index.php/Main_Page
LuaTeX Wiki with examples for callbacks and nodes.
https://wiki.contextgarden.net/Lua
ConTeXt Wiki page about Lua. The packages
luaotfload
andlualibs
(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
\directlua
instruction with four additional interfaces -- the macros\luadirect
and\luaexec
, and the environmentsluacode
andluacode*
. These interfaces differ in the way TeX-special characters such as%
and\
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
\luastring
,\luastringN
, and\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.
-
1How does this answer the question about how to learn Lua programming? Jul 18, 2019 at 10:51
-
2@HenriMenke - I generally interpret questions and objectives as broadly as is sensible. (Aside: I don't think anyone has ever accused me of being a pedant.) The OP did write, "I want to learn about ... writing functions etc. in Lua which then I can use in LaTeX." [emphasis added] Understanding and mastering the interface(s) between LaTeX and Lua is -- to me at least -- a very important part of learning how to employ the Lua machinery in a LuaLaTeX document.– MicoJul 18, 2019 at 12:01
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"
https://github.com/lualatex/lualatex-doc- Montijano, Juan I. and Pérez, Mario and Rández, Luis and Varona, Juan Luis, "Numerical Methods with LuaLaTeX"
https://www.unirioja.es/cu/jvarona/downloads/numerical-methods-luatex.pdf
-
1“A Guide to LuaLaTeX” is pretty out-of-date. It still features the
euenc
andluatextra
packages. It also doesn't teach Lua programming, which the question was asking for. Jul 18, 2019 at 10:53 -
1Well, I did not know any Lua before starting with these, so they do teach at least a tiny bit of Lua programming. Also, I think "Numerical methods with LuaLaTeX" does a good job in introducing Lua for a specific use case in LaTeX, which seems relevant given the context in which the question was asked. I did not realize how old "A guite to LuaLaTeX" was, I will edit the answer to make it clear.– ClaudioJul 19, 2019 at 7:37
unpack
andsetfenv
have just disappeared.