csvsimple to generate questions

I am trying to generate question labels using csvsimple. I am not getting what is wrong with my code.

I have a file called test.csv with three columns: mylabel,name & regnno.

\csvreader[head to column names]{tester.csv}{}{
\element{candi}
{
\begin{question}{\mylabel}
Registration Number: \regnno \hfill Name: \name
\begin{choices}[o]
\correctchoice{A}
\wrongchoice{B}
\wrongchoice{C}
\wrongchoice{D}
\end{choices}
\vskip2mm
\hrule
\end{question}
}
}

\onecopy{1}
{
\insertgroup{candi}
}


Here I get the output as formatted and with exact number of cadidates as that in the csv file. But the actual data from the csv file are missing.

Interestingly, if I comment the \element part and \onecopy part and compile the file off the AMC, then it works perfect!

Thanks in advance
VInay

Replies (2)

RE: csvsimple to generate questions - Added by Alexis Bienvenüeover 7 years ago

The problem here is that the fields macros are not expanded before the \insertgroup call, when they are all empty. You can solve this with the help of the expl3 package, and using datatool instead of csvsimple:

\documentclass[a4paper]{article}

\usepackage{datatool}
\usepackage{expl3}

\usepackage{automultiplechoice}

\begin{filecontents*}{tester.csv}
regnno,name,mylabel
01,alex,qalex
02,jojo,qjojo
03,rene,qrene
\end{filecontents*}

\begin{document}

\ExplSyntaxOn
\def\expandthree#1#2#3#4{
\exp_args:Nooo#1{#2}{#3}{#4}
}
\ExplSyntaxOff

\def\addq#1#2#3{
\element{candi}
{

\begin{question}{#1}
Registration Number: #2 \hfill Name: #3
\begin{choiceshoriz}[o]
\correctchoice{A}
\wrongchoice{B}
\wrongchoice{C}
\wrongchoice{D}
\end{choiceshoriz}
\vskip2mm
\hrule
\vskip3mm
\end{question}
}
}

\DTLloaddb{tester}{tester.csv}
\DTLforeach{tester}{\regnno=regnno,\name=name,\mylabel=mylabel}{
\expandthree\addq\mylabel\regnno\name
}

\onecopy{1}{
\insertgroup{candi}
}

\end{document}


RE: csvsimple to generate questions - Added by Al DUSover 7 years ago

It is working well !

However, we loose the ability to shuffle the questions.
Without the [o] option, it doesn't work anymore, and I don't see why.
One could imagine that after the "expanthree ..." you have exactly the same thing in the file as if one would have typed it manually.
Mystery !

(1-2/2)