Can anyone explain why the macro \makequesone below fails and/or suggest a fix? The verbatim option works (as in the MWE) but I would like the content of the argument to be able to contain relatively arbitrary text and mathematical content so I don't think the verbatim option is ideal.
The basic point is to store a list of questions in a lua table, and then to generate exams by randomly selecting questions from the bank. Right now, I'm just getting the storage retrieval part working.
The fourth argument will contain an exam question and I have the understanding that \luatexluaescapestring should be used to prevent bad things from happening. In the working code below, I used a v type argument, but using an m type argument and just storing the args in token lists and doing nothing with them generates no errors, so I have the feeling that the issue is with \luatexluaescapestring which doesn't seem to like a \par somewhere. A "fix" would allow me to pass the content of that 4th argument to lua relatively freely and to retrieve it with \getquestion. In either case, an explanation of what's going on would be nice :)
\documentclass{article}
\usepackage{xparse}
\usepackage{luacode}
\begin{luacode*}
cats = {}
function makequestion(c,id,pt,q)
-- if question category doesn't exist, then create a table for it
if not cats[c] then
cats[c] = {}
end
-- add new question with tag "id" to category "c" table.
-- question worth "pt" points, and has content "q"
-- some questions may have entries for the parts subtable
cats[c][id]={points = pt, ques = q, parts = {}}
end
function getquestion (c,id)
tex.sprint(cats[c][id].ques)
end
\end{luacode*}
% Adds a question of category #1 and question id #2 to the table.
% the question point value is #3 and the actual question is #4.
% in use, there will be a 5th field for question parts.
%
% #1 = category
% #2 = id
% #3 = points
% #4 = question
\NewDocumentCommand{\makequesone}{ m m m +m }
{
\directlua{makequestion("\luatexluaescapestring{#1}","\luatexluaescapestring{#2}","\luatexluaescapestring{#3}","\luatexluaescapestring{#4}")}
}
\NewDocumentCommand{\makequestwo}{ m m m v }
{
\directlua{makequestion("\luatexluaescapestring{#1}","\luatexluaescapestring{#2}","\luatexluaescapestring{#3}","\luatexluaescapestring{#4}")}
}
\NewDocumentCommand{\getquestion} { m m }
{
\directlua{getquestion("\luatexluaescapestring{#1}","\luatexluaescapestring{#2}")}
}
\begin{document}
hi
% fails
%\makequesone{factor}{id1}{1}{\begin{itemize}\item fish\end{itemize}}
% works
\makequestwo{factor}{id1}{1}!Here is my question \begin{itemize}\item fish\end{itemize}!
\getquestion{factor}{id1}
\end{document}
\unexpanded{#4}there will be no error; I can't say there's success, because I don't know what the thing is supposed to do and how to check for success/failure. – egreg Oct 19 '12 at 06:48tex.print("\\gdef\\matout{"..ReturnMatrix(mout).."}");-). One have to mask the backslash if the TeX-string is formated in Lua. And sometimes one have to use the mask symbol%from the Lua environment like this:count = string.gsub(text, "%[displayletter%]", ""). The two symbols (\ and %) have significantly different meanings in each of the two worlds of TeX and Lua and caused me sometimes a lot of headache and a lot of strange behavior in my functions. So I'm very careful when using it. – Holle Oct 19 '12 at 20:05