TransWikia.com

Generating jpg images of Questions and Options in Exam Document Class through any method

TeX - LaTeX Asked on July 27, 2021

The following is a code of TeX file.

 documentclass{exam}
    begin{document}
    begin{questions}
    question  Sample Question
    begin{choices}
    choice choice 1
    choice choice 2
    choice choice 3
    choice choice 4
    end{choices}
    end{questions}
    end{document}

Is it possible to have make4ht or htlatex or lua Code or any other method which will produce jpg image of each question and each option (so sample question in above tex file will generate five images : one for question and other for four options.) The jpg file should not include question number and option label. At the end images are to be named as 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg for question 1 and its four options. The second question will have five images named as 6.jpg, 7.jpg, 8.jpg, 9.jpg, 10.jpgand so on.

One Answer

Try this configuration file:

Preamble{xhtml}

% this counter will be used for picture numbering
newcountchoicespic

% configure pictures to be .jpg
Configure{Picture}{.jpg}

defsetupnextpicture{%
  globaladvancechoicespic by 1%
  NextPictureFile{thechoicespic}%
}

% configure markup and images for the choices environemnt
ConfigureList{choices}
{letorigchoicechoiceHCode{</div>}defchoiceend{}}% 
{choiceendletchoiceorigchoice}
{%
  choiceendHCode{<div class="choice">}%
  defchoiceend{HCode{</div>}}% 
  % update image name
}
{
  setupnextpicture% declare new picture name
  Picture*{}% start picture
  defchoice{EndPictureorigchoice}% close picture before every choice command
}
% close picture at end of the choices environment
AtEndEnvironment{choices}{EndPicture}
% close question picture
BeforeBeginEnvironment{choices}{EndPicture}

makeatletter
% save original version of questions environemtn
letorig@questionsquestions
% redefine the process@question command to start the picture
defquestions{%
 orig@questions% execute the original code
 letorigprocessquestionprocess@question % save process@question
 defprocess@question{origprocessquestionsetupnextpicturePicture*{}}%
}
 
makeatother

ConfigureList{questions}
{%
 defquestionlistend{} % reset questionlistend
 AnchorLabelSkipRefstepAnchor% remove link inserted by label
 Configure{()}{$}{$}% disable pictures for math
}
{questionlistend}
{%
  ifvmodeIgnoreParfiEndPquestionlistend%
  HCode{<div class="question"><div class="questiontitle">}%
  defquestionlistend{HCode{</div>}%
  AnchorLabelSkipRefstepAnchor%
}
}
{}%{setupnextpicturePicture*{}}

AtEndEnvironment{questions}{HCode{</div>}}


begin{document}
EndPreamble

The problem with the exam class is that it defines some commands that are active only inside environments, so it is quite difficult to patch them.

Compile using

make4ht -c config.cfg exam.tex

This is the result:

enter image description here

And HTML:

  <div class='questions'>
    <a id='x1-3x1'></a><div class='question'><div class='questiontitle'>
 1. <img alt='Sample Question  ' src='1.jpg' />
            </div><div class='choice'>
         A.  <img alt='choice 1  ' src='2.jpg' />
            </div><div class='choice'>
         B.  <img alt='choice 2  ' src='3.jpg' />
            </div><div class='choice'>
         C.  <img alt='choice 3  ' src='4.jpg' />
            </div><div class='choice'>
         D.  <img alt='choice 4  ' src='5.jpg' /></div>
 2. <img alt='Second Question  ' src='6.jpg' />
            </div><div class='choice'>
            <a id='x1-82'></a>A. <img alt='choice 5  ' src='7.jpg' />
            </div><div class='choice'>
         B.  <img alt='choice 6  ' src='8.jpg' />
            </div><div class='choice'>
         C.  <img alt='choice 7  ' src='9.jpg' />
            </div><div class='choice'>
         D.  <img alt='choice 8  ' src='10.jpg' /></div>
</div>

Correct answer by michal.h21 on July 27, 2021

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