TransWikia.com

Shuffle questions in xsim

TeX - LaTeX Asked by Xuân Duy Bảo Nguyễn on June 21, 2021

As said in the title, I want to shuffle questions while using xsim and tasks packages. Here is the code I took from multiple choice questions with correct choice environment which is written by @cgnieder.

documentclass[12pt,a4paper]{article}
usepackage[inline]{enumitem}
usepackage[no-files]{xsim}
usepackage{tasks}[newest]

DeclareExerciseEnvironmentTemplate{item}
  {item[textbf{GetExerciseName~GetExerciseProperty{counter}}]}
  {}

DeclareExerciseProperty{answer}

newcommand*answer[1]{%
  XSIMexpandcode{%
    SetExerciseProperty{answer}
      { noexpand{Alph{task}}}}%
  #1%
}

newcommand*getanswers{%
  defbetweenanswers{defbetweenanswers{hspace{2em}}}%
  ForEachUsedExerciseByID{%
    betweenanswers##3ExercisePropertyGet{##1}{##2}{answer}%
  }%
}

xsimsetup{
  exercise/template = item,
  exercise/name = Q.,
  exercise/the-counter = arabic{exercise}.
}

NewTasksEnvironment[
  label = textbf{Alph*.},
]{choice}[choice]

newenvironment{questions}
   {enumerate}
   {endenumerate}

%=========================================================
begin{document}

section{Problems}

begin{questions}
  begin{exercise}
    What is the product of $-2$ and $3$?
    begin{choice}(4)
      choice answer{$-6$}
      choice $6$
      choice $5$
      choice $-5$
    end{choice}
  end{exercise}

  begin{exercise}
    What is the sum of the sides of a polygon called?
    begin{choice}(2)
      choice Leg
      choice answer{Perimeter}
      choice Area
      choice Volume
    end{choice}
  end{exercise}

  begin{exercise}
    What is the sum of $-2$ and $-3$?
    begin{choice}(4)
      choice $-6$
      choice $6$
      choice $5$
      choice answer{$-5$}
    end{choice}
  end{exercise}
end{questions}

section{Answers}
getanswers

end{document}

In addition, is it possible to shuffle locally? For more specific, assume that I have 10 questions and divide them into 2 parts, one contains questions 1 to 5, and one contains the rest. I want to shuffle each part separately and get the questions in order, for example, 2 3 1 4 5 and 8 6 10 9 7.

One Answer

Here is an idea which first adds the exercises to a collection and then randomly outputs them which also shuffles them. Using several collections this would also enable to shuffle blocks of exercises separately.

documentclass[12pt,a4paper]{article}
usepackage{xsim}
usepackage{tasks}

NewTasksEnvironment[
  label = textbf{Alph*.} ,
  label-width = 15pt
]{choice}[choice](4)

DeclareExerciseEnvironmentTemplate{item}
  {descriptionitem[GetExerciseName~GetExerciseProperty{counter}]}
  {enddescription}

DeclareExerciseEnvironmentTemplate{space}
  {textbf{GetExerciseProperty{counter}} }
  {quad}
  
newcommand*answer[1]{%
  XSIMexpandcode{%
    SetExerciseProperty{solution-body}
      {noexpand{Alph{task}}}}%
  #1%
}

DeclareExerciseCollection{part-A}

xsimsetup{
  exercise/template = item ,
  solution/template = space ,
  exercise/name = Q. ,
  exercise/the-counter = arabic{exercise}. ,
  exercise/collect ,
  random/sort = false
}

begin{document}

collectexercises{part-A}
begin{exercise}
  What is the product of $-2$ and $3$?
  begin{choice}
    choice answer{$-6$}
    choice $6$
    choice $5$
    choice $-5$
  end{choice}
end{exercise}
begin{exercise}
  What is the sum of the sides of a polygon called?
  begin{choice}(2)
    choice Leg
    choice answer{Perimeter}
    choice Area
    choice Volume
  end{choice}
end{exercise}
begin{exercise}
  What is the sum of $-2$ and $-3$?
  begin{choice}
    choice $-6$
    choice $6$
    choice $5$
    choice answer{$-5$}
  end{choice}
end{exercise}
begin{exercise}
  Number four
end{exercise}
begin{exercise}
  Number five
end{exercise}
collectexercisesstop{part-A}

section{Problems}
printrandomexercises[collection=part-A]{5}

section{Answers}
printsolutions[headings=false]

end{document}

enter image description here

Correct answer by cgnieder on June 21, 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