1

Question

How do I handle the extraction of the .tex file (as described below) to a flat csv (with format below) ?

Context

I created all my MCQ with exam. However, exams are now 100% online... (sigh)

My LaTeX file has the basic following format

\documentclass[12pt]{exam}
​
\begin{document}

\begin{questions} \question What is the answer ? \begin{oneparchoices} \choice 70 \choice 75 \choice $80 \times \Delta$ \CorrectChoice 85 \choice None of the above \end{oneparchoices} \end{questions} \end{document}

I need now to provide a csv where the questions of the MCQ above would be displayed like (Incorrect,Correct, just to be clear

question,answer1,Cor/Inc,answer2,Cor/Inc,answer3,Cor/Inc ,answer4,Cor/Inc,answer5,Cor/Inc

And it would render like

What is the answer ?,70,Inc,75,Inc,"$80 \times \Delta$",Inc,85,Cor,None of the above,Inc

Each line would obviously be a new question.

What could correspond so far

I found something interesting in python, but I am more open to a solution than a type of programming.

I see the principle for environment between \begin and \end thanks to https://stackoverflow.com/questions/11054008/extract-figures-from-latex-file

infile = open('MCQ.tex', 'r')
outfile = open('FlattenMCQ.csv', 'w')
extract_block = False
for line in infile:
    if 'begin{questions}' in line:
        extract_block = True
    if extract_block:
        outfile.write(line)
    if 'end{questions}' in line:
        extract_block = False
        outfile.write("------------------------------------------\n\n")

infile.close() outfile.close()

Where I am stuck The recurisivity to test first \begin{questions} then \question then \begin{oneparchoices} then \choice or \CorrectChoice

NB : I posted initially this question there SE but probably makes more sense here.

JeT
  • 3,020
  • It is usually considered impolite to crosspost (better link: https://stackoverflow.com/q/65008969/2336725). This is more of a SO question than a TeX question. – Teepeemm Dec 02 '20 at 13:42
  • 1
    @Teepeemm Oh is it (impolite) ? I did not have much luck on SO, I thought i'd probably be more lucky to have an aswer here. What do you recommend ? I close my question here ? – JeT Dec 02 '20 at 13:44
  • 1
    Related: https://tex.stackexchange.com/questions/564420/is-there-a-python-module-for-parsing-latex – Marijn Dec 02 '20 at 13:55
  • @Marijn :) I'll look merci ! – JeT Dec 02 '20 at 13:56
  • 1
    @Marijin You've just given me the distance (very very long) between me and what I need to do :) – JeT Dec 02 '20 at 15:19

0 Answers0