I've been experimenting with ConTeXt and have been trying to create an exam using it.

I would like to have an array in lua that holds the weight of each question, and a custom enumeration called "question" that uses its own counter value to take the correct weighting from the array, and prints it next to the question number.

I haven't been able to get the counter value passed from tex to be properly interpreted in lua. It is correctly identified as a string, and lua prints out the string as the correct value, and I can even append more numbers to the string and it works correctly. But any attempt to convert the value to a number results in a nil value in Lua.

Does any one know what I'm doing wrong?

This is the basic code I've written, and when compiled under the current version of ConTeXt mkiv, it prints nil for both a and b. When compiling ConTeXt will pause and give me a "Please type a command or say `\end') prompt, and I think it may be somehow connected, but perhaps this is a separate issue.

    local lpeg = require"lpeg"
    userdata = userdata or {}
    qVals = {2, 3, 5, 10}
    function userdata.getQval(numb)
        local int = lpeg.S'+-'^-1 * lpeg.R'09'^1
        local a = lpeg.match(int,numb)
        local b = qVals[tonumber(numb)]
        context(type(numb) .. type(a) .. type(b) .. qVals[1])




    \question This is the question
    \question Another
    \question and another

EDIT: After more hunting around I've concluded that \getnumber[question] is only expanded at the end, and so can't be used in indexing as-is. So now the question is how to force it to expand

  • The error may not be lua at all, but braces connected. What if you try with text={\getQuestionVal{\getnumber[question]}} ?
    – sztruks
    Commented Oct 24, 2018 at 19:00
  • Just tried with the extra braces but unfortunately that didn't work.
    – anthsts
    Commented Oct 24, 2018 at 19:20
  • 2
    You need the \rawcountervalue[<COUNTER>] command to pass the value to Lua but you can also use the function structures.counters.value(<COUNTER>) to access the value from Lua itself. Commented Oct 24, 2018 at 20:18
  • Holy cow, that worked like a charm! thanks, I've been stuck on that issue for longer than I'd like to admit. is there a way for me to accept your comment as the correct answer?
    – anthsts
    Commented Oct 24, 2018 at 20:36

1 Answer 1



The names for advanced counter mechanism where changed a few years ago from \definenumber to \definecounter and the old names are just synonyms for the new command names.


The problem in your example is that you try to use the \getnumber command to pass the value to Lua but the \getnumber command itself is not expandable. To get value of the counter on the Lua side you have to use the expandable \rawcountervalue command, another method is to access the value from Lua with the structures.counters.value function.


function userdata.showcounter_value(value)

function userdata.showcounter_name(name)









You must log in to answer this question.

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