TransWikia.com

Turn question and choices of exam class to csv (with a python? Perl? script)?

TeX - LaTeX Asked on March 27, 2021

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("------------------------------------------nn")

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.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP