# [luatex] Getting macro expansions from Lua

Reiner Pope reiner.pope at gmail.com
Wed Jun 1 17:01:12 CEST 2011

Hi,

I'm trying to expand TeX macros from within Lua code, but I can't work
out how to do this.

I would like to write code like this

\def\A{1}
\directlua{
t = token.create("A")
toks = tex.get_expansion(t)  % the tex.get_expansion function is what
I want
% toks should consist of the token '1'.
}

\bye

My motivation is that I want to access LaTeX's referencing information
from Lua. To find the label number for, say, 'equationone', I need to
look in the macro \r at equationone.

It is possible to achieve roughly what I want by calling out of Lua and
back into it, but it's ugly:

(file test.tex)
\def\A{2}
\directlua{
s = "A"
dofile('run.lua')
}
\bye

(file test.lua)
tex.sprint('\\directlua{ t = "\\' .. s .. '"\
if t == "1" then tex.sprint("got 1") else tex.sprint("not 1") end\
}')

(This will print "got 1" if the first line is \def\A{1}, and will print
"not 1" otherwise.)

What I'm looking for is a way to do this without nesting
\directlua{tex.sprint("\directlua{...}")}.

Cheers,
Reiner