9

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.

5
  • 4
    the canonical place to start is "Programming in Lua" lua.org/pil/1.html Jul 17, 2019 at 20:31
  • 4
    Also, it's is general Lua you want, I think you need to ask on StackOverflow rather than here: we can't cover all of Lua
    – Joseph Wright
    Jul 17, 2019 at 20:36
  • @DavidCarlisle Keep in mind that lua.org/pil/1.html refers to Lua 5.0 whereas LuaTeX embeds Lua 5.3 where the syntax for some things changed dramatically. Jul 17, 2019 at 21:13
  • 1
    @HenriMenke yes it does warn about that, but still to start with the basics it's an easy read, the new integers in 5.3 do change things a bit I agree. Jul 17, 2019 at 21:14
  • 2
    @DavidCarlisle The integers of Lua 5.3 are not even so crucial because they are just an add-on, but for example the global functions unpack and setfenv have just disappeared. Jul 17, 2019 at 21:17

3 Answers 3

15

There are three canonical resources to learn Lua programming.

  1. “Programming in Lua” by Roberto Ierusalimschy

    https://www.lua.org/pil/

    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.

  2. “Lua Programming Gems” edited by L. H. de Figueiredo, W. Celes, R. Ierusalimschy

    https://www.lua.org/gems/

    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.

  3. Lua Reference Manual

    https://www.lua.org/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.

  1. The LuaTeX manual

    texdoc luatex or 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

    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.

  2. Further LuaTeX online resources

6

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 environments luacode and luacode*. 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.

2
  • 1
    How 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.
    – Mico
    Jul 18, 2019 at 12:01
4

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:

2
  • 1
    “A Guide to LuaLaTeX” is pretty out-of-date. It still features the euenc and luatextra packages. It also doesn't teach Lua programming, which the question was asking for. Jul 18, 2019 at 10:53
  • 1
    Well, 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.
    – Claudio
    Jul 19, 2019 at 7:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .