3

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.

\startluacode
    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])
    end
\stopluacode

\def\getQuestionVal#1{%
    \ctxlua{userdata.getQval([==[#1]==])}%
}

\defineenumeration[question]

\setupenumerations[question][
    text=\getQuestionVal{\getnumber[question]},
    headstyle=normal
]

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

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 Oct 24 '18 at 19:00
  • Just tried with the extra braces but unfortunately that didn't work. – anthsts Oct 24 '18 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. – Wolfgang Schuster Oct 24 '18 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 Oct 24 '18 at 20:36
3

Note

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.

Example

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.

\startluacode

function userdata.showcounter_value(value)
    context(tonumber(value))
end

function userdata.showcounter_name(name)
    context(structures.counters.value(name))
end

\stopluacode

\definecounter[testcounter]

\starttext

\incrementcounter[testcounter]

\ctxlua{userdata.showcounter_value(\rawcountervalue[testcounter])}

\incrementcounter[testcounter]

\ctxlua{userdata.showcounter_name("testcounter")}

\stoptext

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.