I need to get the value of a macro from lualatex directly and return it in a function call

function GetMacroValue(macro)
    return macro.value

I've tried various things such as using tex.print to store the value in a temp value BUT since tex.print executes after the function call/luacode block it does not work

function GetMacroValue()
    tex.print("\\directlua{value = tonumber('\\the\\"..macro.."')}")
    return value

Would work except the tex.print line does not execute until after the fact in which case the previous value is returned.

since tex.execute does not exist yet is there any other method? I've tried coroutines but it does not work for(or I can't get it to work).


2 Answers 2


If it's a count register, you can access it directly (tex.count[...]). Similar for other stored items such as toks, skip, dimen and attribute registers. See manual section 4.13.4.

If it is something you only get "during runtime" you have to use coroutines for that. I have written an answer to another question where I have given a short working example for a coroutine:


  • As I've said, I've tried the coroutines and they don't work. It gives me an error about crossing the C boundary. I've not implemented your method but Charles'. It shouldn't matter too much which one. In any case I do not want to have 10x the code to execute a simple function call as I can just pass the macro value to the lua code in the first place without too much trouble. (for example, we could modify directlua to take optional arguments like \directlua[mymacro = "\the\mymacro"] and insert the values into a table in lua. Only works well if mymacro isn't modified in the lua code. Commented Jul 16, 2012 at 9:49
  • @AbstractDissonance Right, for any trivial case you can pass parameters to the Lua function. See tex.stackexchange.com/a/48475/243 for example. If you have a more difficult case (modifying a value during a \directlua{} call) you need coroutines.
    – topskip
    Commented Jul 16, 2012 at 10:22
  • Well, I'm simply trying to get the current interactivemode. It is sort of a texprimitive but I looking at the docs shows it only is a primitive to etex... so now I have to figure out what etex is... Commented Jul 16, 2012 at 11:00
  • @AbstractDissonance What about \directlua{ i = \the\interactionmode ; print("interactionmode = ".. tostring(i))}?
    – topskip
    Commented Jul 16, 2012 at 14:00

It seems the only way to get some of these tex values is by passing them to lua. This maybe due to bugs in lualatex as the manual states it is possible. The problem exists because there is no way for lua code to pass control back to TeX(to get the variable) and then continue where it left off. Coroutines should allow this behavior but throw an exception due, at least in my case, to some mismatch between TeX and Lua.

The best way I've come up to solve this problem is to write a macro that passes all the variables from TeX to Lua I need and have it called transparently each time lua code is executed(by wrapping \directlua).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .