[Tex/LaTex] be interested in Lua

lualuatex

I have been using LuaLaTex since I got acquainted with TeX, because it can easily work with system fonts, which I really need. Font packages in TeX are not friends of the Russian language, though they are friends of the Lua language.

I have found some examples here: What is a simple example of something you can do with LuaTeX?, but I still do not fully understand what can Lua do.

So, my questions are the following:

  • Was Lua specially designed for TeX, or it is just a programming language that can be implemented in TeX? Answer: It is just a programming language, not specific to TeX.
  • How can it be used in typesetting? Can it somehow substitute any packages?
  • Is it OS dependent, i. e. is there anything specific while using it on Windows?
  • Can it work with non-latin characters? Answer: YES.

Best Answer

You asked, inter alia:

What for can [Lua] be used in typesetting? Can it somehow substitute any packages?

  • Some typesetting tasks can be achieved only in LuaTeX, but not pdfTeX or XeTeX. We're starting to see more and more packages that require LuaLaTeX to accomplish their objectives; these packages simply couldn't have been written at all in pdfLaTeX or XeLaTeX. Examples are the showhyphens and selnolig packages. (Full disclosure: I'm the main author of the selnolig package.)

  • Some typesetting tasks can be accomplished more easily (or, at least, with no more work) in LuaLaTeX than in either pdfLaTeX or XeLaTeX. Compare, for instance, the following two code chunks (from the posting Arithmetic temporaries in tex). In both cases, the macro \StrMid serves to extract a substring from a string:

    \usepackage{luacode} % for "\luastring" macro
    \newcommand\StrMid[3]{\directlua{tex.sprint(string.sub(\luastring{#1},#2,#3))}}
    

    versus

    \usepackage{xparse}
    \ExplSyntaxOn
    \NewExpandableDocumentCommand{\StrMid}{mmm}
     {
      \tl_map_function:fN { \tl_range:onn { #1 } { #2 } { #3 } } \use:n
     }
    \cs_generate_variant:Nn \tl_map_function:nN { f }
    \cs_generate_variant:Nn \tl_range:nnn { o }
    \ExplSyntaxOff
    

    By contrasting these two chunks of code, I certainly do not mean to slight the author of the LaTeX3 code. Not at all! All I mean to do is to show that some tasks can be rather easy to accomplish if one knows even just a little bit of Lua. This is especially true when it comes to manipulating strings, as Lua provides quite a few powerful string-handling functions.

  • If you're used to working with a modern programming language but haven't ever fully mastered all the fine points of a macro expansion language -- I myself must plead guilty to just this charge... -- you'll probably find it much easier to program some non-trivial typesetting tasks by using Lua code and the clear interface between Lua and TeX that's built into LuaTeX.