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
text={\getQuestionVal{\getnumber[question]}}
?\rawcountervalue[<COUNTER>]
command to pass the value to Lua but you can also use the functionstructures.counters.value(<COUNTER>)
to access the value from Lua itself.