Neat trick with LuaTeX: Passing Lua tables from TeX to Lua

It has been a long time since I wrote a blog post so I thought I’d share a rather nice “trick” I discovered today. You can pass a Lua table from a TeX macro to Lua. Here’s how.

% We start by creating a simple macro that takes one argument, but when we expand
% the macro we enclose its argument in braces {...}, which is then coerced, by Lua, 
% into a table constructor! 

\def\tabtest#1{
\directlua{
% This simple function is just for demo purposes
function join(a,b)
print("I was called with ("..a..","..b..")")
end

%Here's the neat bit, our TeX macro argument, enclosed in braces {...}, becomes a Lua table!
str=#1 % Yay, str becomes a Lua table!
% Here we call the join function with values in the table 
str[1][1](str[1].x,str[1].y)
}}

% Here's our macro call --- \tabtest{{<--this becomes a table-->}}
% We include a pointer to the function "join", together with the values to call it with
\tabtest{%
{% start of Lua table to send
{[1]=join, x=1, y=2}
}% end table to send
}% end \tabdef call

The result of running the above calls join(1,2) and is printed to the terminal: I was called with (1,2). Rather nice, I thought!

More progress with HarfBuzz/LuaTeX (update)

Just a short post to share another example from my on-going work on HarfBuzz/LuaTeX. A rather pointless example – without using any code to correctly place mark glyphs (e.g., vowels) – showing randomly coloured Arabic glyphs. Thanks to the power of HarfBuzz and the superb Lua C API (especially C closures and “for loop” iterators) the code to process the Arabic text is about 25 lines of Lua script.

Source of text for typesetting example: BBC Arabic. I don’t know what the text says but Google Translate indicated it was neither controversial or offensive – I hope that is the case!

Download PDF

Update

Just to add an example with mark glyph positioning and random colours. Vowel positioning added about 10 lines of Lua script :-).

Download PDF

Quick tip: Decompressing PDFs for Debugging

If you are creating special effects in PDFs (e.g., using pdfTeX’s \pdfliteral{…} or via \special{…}) it can be extremely helpful to see the raw PDF code being output by your code/macros etc – especially when debugging! Often, by default, PDF content streams are compressed/encoded into binary data which makes it extremely difficult to access the raw PDF data/content. However, you can use the recently released free community version of Coherent’s excellent command-line PDF manipulation software to decompress the binary data into plain text. It’s as simple as:

cpdf -decompress compressedfile.pdf -o decompressedfile.pdf

cpdf has a wealth of other interesting command-line options too.

Early results of integrating HarfBuzz into LuaTeX

Building on the work of porting LuaTeX to build on Windows I decided to explore adding HarfBuzz to provide Arabic shaping. The excellent HarfBuzz API lends itself to some interesting solutions so here’s a quick post to show some early results.

Source of text for typesetting fully vowelled Arabic examples: http://en.wikipedia.org/wiki/Arabic_language#Studying_Arabic

Download PDF