26

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.
  • 3
    Have you had a look at Wikipedia regarding Lua (https://en.wikipedia.org/wiki/Lua_(programming_language))? At least two of your questions can be answered there. – Dr. Manuel Kuehner Apr 28 '17 at 09:36
  • Also http://lua-users.org/wiki/LuaBinaries answers the 3rd question. – Chris H Apr 28 '17 at 09:38
  • The 4th question seems like a duplicate of https://tex.stackexchange.com/questions/36188/do-xetex-and-luatex-always-use-unicode – Chris H Apr 28 '17 at 09:39
  • 1
    The 2nd question is potentially interesting though -- perhaps edit down to just that. – Chris H Apr 28 '17 at 09:40
  • @ChrisH the question is in the differences of usage while implementing it to LaTeX code, not just writing a program – Michael Fraiman Apr 28 '17 at 09:45
  • @MichaelFreimann which question, you have 4? – Chris H Apr 28 '17 at 09:47
  • @ChrisH Yes. Is there any example of a LaTeX code with Lua usage, that will work on one OS, but won't on another? – Michael Fraiman Apr 28 '17 at 09:48
  • @MichaelFreimann For the second answer I give you some examples: https://tex.stackexchange.com/a/45388/243, https://tex.stackexchange.com/a/28128/243 and https://tex.stackexchange.com/a/58327/243. – topskip Apr 28 '17 at 09:52
  • 1
    @topskip these are very good examples. Seems that Lua is good for visualizing some behind-the-scenes processes the TeX engine uses. – Michael Fraiman Apr 28 '17 at 09:57
  • 4
    @MichaelFreimann see https://tex.stackexchange.com/questions/197485/practical-use-of-lua for what is possible with Lua in LaTeX, without the need to fiddle with other languages the user would have to install, or at least to care about! – Josef Apr 28 '17 at 10:55
  • This question made me wonder if there was a TeX with Python. Unsurprisingly, a few things turned up: https://github.com/gpoore/pythontex. Not sure how active they are. – jpmc26 Apr 29 '17 at 01:29
  • @jpmc26 I wonder if there exists TeXTex. So you write on TeX while writing on TeX. I've heard that PythonTeX is under development. – Michael Fraiman Apr 29 '17 at 04:56
  • Please don't include the answers in the question. The answers belong in the answers, which is where they are. –  Apr 29 '17 at 05:23
  • @QPaysTaxes these answers were obtained after some comments and personal research. It is OK to include it in the question. – Michael Fraiman Apr 29 '17 at 06:12
  • @MichaelFreimann No, it's not. It is, however, fine to post your own anwser; self-answering (which is the Googleable term) is totally fine, and indeed encouraged, if you find the answer to your question. –  Apr 29 '17 at 06:23
  • See http://wiki.luatex.org/index.php/TeX_without_TeX – Henri Menke Apr 30 '17 at 21:42

3 Answers3

20

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.

Mico
  • 506,678
  • So, I can use it to o some calculations or modifications of the data depending on the context. Looks much easier than xparse and expl3. – Michael Fraiman Apr 28 '17 at 16:23
  • @MichaelFreimann - I contrasted the two code chunks precisely because the LuaTeX-based can make use of the powerful string.sub Lua function. In other cases, the advantage enjoyed by LuaTeX may not be as clear cut. – Mico Apr 28 '17 at 20:00
18

Lua is a programming language designed to run in a small easily embedded virtual machine (much smaller than the Java JVM for example) and so ideal for embedding in other systems (it is used as the configuration language for several games for example).

So it is not developed by the luatex people (in fact Lua itself is currently at at least at version 5.3, ahead of the Lua 5.2 version shipped with luatex) but chosen as a suitable extension language.

It can be used to re-implement several of the built in TeX algorithms

It's more or less OS independent

Yes as you see in Luatex it can handle Unicode fonts and Unicode strings (Unicode strings are exposed as UTF-8 encoding so a single unit is a byte rather than a character)

David Carlisle
  • 757,742
5

As an example of what can be done with LuaLaTeX, I created a script that automatically converts CSV data into the LaTeX tabular or matrix format. It is quite useful for lab reports since the user no longer has to worry about manually updating data or simulation results in the TeX file.

https://gist.github.com/calebreister/8dd7ab503c91dea4dd2c499b9d004231