Here's an example of something I'm doing.
I'm writing a document which has a lot of sections. At the end of each section is a score, made of 3 numbers. The first number is on a scale of 0-10, and the second two are 0-3. Also the scale of the first is inverted, meaning that 1 is the biggest and 10 is the smallest, but 0 should still be 0. This code outputs each score and the average score it calculates.
\def\score#1#2#3{
\directlua{
s1 = #1;
s2 = #2;
s3 = #3;
if s1 > 0 then
s1 = 11 - s1
end
avgscore = (s1 + (10 * s2 / 3) + (10 * s3 / 3)) / 3;
tex.print("Score 1: ", s1, ", ")
tex.print("Score 2: ", s2, ", ")
tex.print("Score 3: ", s3, ", ")
tex.print("Combined Score: ", string.format("\%0.2f", avgscore))
}
}
And then I use it multiple places in my document like this:
\section{stuff}
....
\score{8}{3}{0}
....
\section{even more stuff}
....
\score{0}{1}{1}
I'm also dumping these scores to a file (using lua), reading them in later (again with lua), and using pgfplots to create a chart of some things. There may be a much better way to do this, but I'm not willing to spend the rest of my life learning how to do a few calculations.
The LuaTeX developers have commented on their choice of Lua over other languages, including Python, on their home page.
Embedding the interpreter is one thing, and apparently no fun with Python. Another is to actually make the innards of TeX visible to the embedded interpreter. While much of the communication code could likely be adapted in some way from LuaTeX to PythonTeX, it will still be a lot of work.
A better approach may be to cheat a bit, and simply piggyback on the LuaTeX interpreter. There is a bridge that 'embeds' Python inside Lua called Lunatic Python, and maybe it can be made to work with LuaTeX also. However, it may be easier to just use XML-RPC: Let LuaTeX spawn a Python process, which acts as the XML-RPC server. LuaTeX commands can then make calls to the Python process. Bonus feature: Once this is implemented on the LuaTeX side, it can be used with any other scripting language, not just Python.
Best Answer
You asked, inter alia:
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
andselnolig
packages. (Full disclosure: I'm the main author of theselnolig
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:versus
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.