# Properly defining problems and subproblems using the xsim package

I am trying to use the xsim package to sort and order the previously given exams in a course (MAT-1001). For this to be viable I need the following equirements

• I should be able to request exam problems by ID, topic, or year.
• I want to be able to request both questions and subquestions.

I tried to do this with the code below, but I have never encountered such bizzare results in my life. The expected output is so far from what I would expect. The code in its entirety can be found below, but I want to step through it one problem at a time.

The following file is a sample file showing a few common exercises.

## exam.tex

\begin{question}[
year=2017,semester=V,exam=O
ID=MAT-1001-2017-V-H-Problem-1-a,
topic = integral
]
Evaluate the following integrals
\task $\displaystyle \int_0^{\sqrt{\pi/2}} (x+1) \e^{x-1} dx$
\task $\displaystyle \int \frac{a}{x^2 + a^2} dx$
\task $\displaystyle \int \log x dx$.
\end{question}

\begin{question}[
year=2017,semester=V,exam=O
ID=MAT-1001-2017-V-H-Problem-1-b,
topic = limit
]
Evaluate the following limits
\task $\displaystyle \lim_{x \to 0} \frac{\sin(x^2)}{x^2}$
\task $\displaystyle \lim_{x \to 0} \frac{\sin(x^2)}{1 + x^2}$.
\end{question}

\begin{question}[
year=2017,semester=V,exam=O
ID=MAT-1001-2017-V-H-Problem-1-c,
topic = injective
]
State the definition of an injective function and provle
that the function $f \to \mathbb{R} \to \mathbb{R}$ defined by $\int_0^x e^{-s^2} dx$ is injective.
\end{question}

\begin{exercise}[year=2017,semester=H,topic={integral,limit,injective},exam=K,
ID=MAT-1001-2017-V-H-Problem-1]
\begin{enumerate}
\item \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-a}
\item \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-b}
\item \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-c}
\end{enumerate}
\end{exercise}


This leads me to my first two questions

• I want to tag the questions and use these tags, however the code above does not work. I am not able to filter out say every question with the topic integral.
• How can I put togheter questions to form an exercise (or exam problem)?

I tried to use the code

\newcommand*\insertexercise{%
\XSIMexpandcode{\printexercise{exercise}{\GetExerciseIdForProperty{ID}{#1}}}%
}


From How can I print exercise with ID in xsim?. To insert the questions into the exercise:

\begin{exercise}[year=2017,semester=H,topic={complex,differential},exam=K,
ID=MAT-1001-2017-V-H-Problem-1]
\begin{enumerate}
\item \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-a}
\item \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-b}
\item \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-c}
\end{enumerate}
\end{exercise}


However that did not work. How can one properly do this? And how would this work when displaying solutions?

In the remainder of this question I post snippets of my MWE, sharing my frustrations over things that does not work as I would expect.

• The lines below fails to compile when uncommented, why?

% \collectexercisestype{all}
%   \input{exam.tex}
% \collectexercisesstop{all}


I have already defined the file exam.tex The following code works however

\collectexercisestype{exercise-pool}{exercise}
\input{exam.tex}
\collectexercisesstop{exercise-pool}

% Random exercise
\begin{exercise}
\begin{enumerate}
\item \printrandomexercises[collection=exercise-pool]{2}
\end{enumerate}
\end{exercise}

• I would expect the code above to print the sole Exercise environment from exam.tex instead it prints nothing. Why?

The following code does print out subquestions. However, why does it only print out one no matter what number I input?

\collectexercises{subquestions}{subquestion}
\input{exam.tex}
\collectexercisesstop{subquestions}

% Random subquestion
\begin{exercise}
\begin{enumerate}
\item \printrandomexercises[collection=subquestions]{2}
\end{enumerate}
\end{exercise}


The following code I would expect to print out the Exercise environment from the file, alas it prints nothing

\insertexercise{ID=MAT-1001-2017-V-H-Problem-1}


Similarly the following code prints nothing as well

\insertexercise{ID=MAT-1001-2017-V-H-Problem-1-a}


In addition I can not print any of the collections as this gives me an undefined item error.

\section{All}

% \printcollection{all}

\section{Exercises}

% \printcollection{exercise-pool}

\section{Subquestions}

% \printcollection{subquestions}


The file exam.tex is defined at the start of the question

## main.tex

\documentclass{article}
\usepackage{amssymb,mathtools}

\usepackage{xsim}

% \xsimsetup{
%   exercise/name=,
%   solution/print=true,
%   solution/name=Solution,
% }

\DeclareExerciseTagging{year} % 1992, 2010, etc
\DeclareExerciseTagging{topic}
\DeclareExerciseTagging{semester} % V (Spring), H (Fall)
\DeclareExerciseTagging{exam} % O (ordinary), K (kont / re-sit exam), P (prøveeksamen)

\DeclareExerciseCollection{MAT-1001-Integrals}

\DeclareExerciseType{subquestion}{
exercise-env = question ,
exercise-name = Question ,
exercise-template = item ,
solution-template = item
}

\DeclareExerciseProperty{title}

\DeclareExerciseEnvironmentTemplate{named}
{\subsection*{\GetExercisePropertyTF{title}{#1}{??}}}
{}

\DeclareExerciseEnvironmentTemplate{item}
{\item}
{}

\xsimsetup{
exercise/template = named
}

\DeclareExerciseCollection{all}
\DeclareExerciseCollection{exercise-pool}
\DeclareExerciseCollection{subquestions}

\newcommand{\problemfolder}{exam-problems}

\newcommand*\insertexercise{%
\XSIMexpandcode{\printexercise{exercise}{\GetExerciseIdForProperty{ID}{#1}}}%
}

\begin{document}

% The lines below fails to compile why?

% \collectexercisestype{all}
%   \input{exam.tex}
% \collectexercisesstop{all}

% Random subproblem or exercise
% \begin{exercise}[title=Random subproblem or exercise]
%   \begin{enumerate}
%     \item \printrandomexercises[collection=all]{2}
%   \end{enumerate}
% \end{exercise}

% Why does the \printrandomexercises{2} only print 1 instead of two exercises?
% Why does it print the subquestions instead of the exercise?

\collectexercisestype{exercise-pool}{exercise}
\input{exam.tex}
\collectexercisesstop{exercise-pool}

% Random exercise
\begin{exercise}
\begin{enumerate}
\item \printrandomexercises[collection=exercise-pool]{1}
\end{enumerate}
\end{exercise}

% Why does the code below print absolutely nothing?

\collectexercises{subquestions}{subquestion}
\input{exam.tex}
\collectexercisesstop{subquestions}

% Random subquestion
\begin{exercise}
\begin{enumerate}
\item \printrandomexercises[collection=subquestions]{2}
\end{enumerate}
\end{exercise}

% Why does the code below print nothing? Is it possible to nest \insertexercise?

Should print the first problem

\insertexercise{ID=MAT-1001-2017-V-H-Problem-1}

% I can sort of understand why the code above does not work, but why
% can it not print subproblems either?

Should print the first subproblem (problem 1-a)

\insertexercise{ID=MAT-1001-2017-V-H-Problem-1-a}

\section{All}

% \printcollection{all}

\section{Exercises}

% \printcollection{exercise-pool}

\section{Subquestions}

% \printcollection{subquestions}

\end{document}


There are so many questions here that it is hard to keep track. I decided to answer the basic problems in your code. This may also help with the other problems you had, I think.

1. The first problem is typos – there is a comma missing after exam=0 in every of your questions (IMHO xsim should probably raise errors here but doesn't at the moment):

\begin{question}[
year=2017,semester=V,exam=O % <<<<< HERE
ID=MAT-1001-2017-V-H-Problem-1-b,
topic = limit
]
...
\end{question}

2. Another problem is your use of \insertexercise. For one thing leave ID= away in the argument \insertexercise{ID=MAT-1001-2017-V-H-Problem-1-a}. It should be the ID itself: \insertexercise{MAT-1001-2017-V-H-Problem-1-a}.

3. Another problem is that you have defined \insertexercise to only insert exercises of type exercise (\printexercise{exercise}{...}) but at the same time you are trying to insert exercises of type subquestion (as can be seen by the IDs you are using \insertexercise with).
4. A further problem is duplication of \item in \item \insertexercise{...}. You have defined the template of the subquestions to insert an \item themselves.

In a current version of xsim the second argument of \printexercise can either be the id or the ID which makes the definition of \insertexercise superfluous.

So, adding the missing commas and defining the exercise as follows:

\begin{exercise}[year=2017,semester=H,topic={integral,limit,injective},exam=K,
ID=MAT-1001-2017-V-H-Problem-1]
\begin{enumerate}
\printexercise{subquestion}{MAT-1001-2017-V-H-Problem-1-a}
\printexercise{subquestion}{MAT-1001-2017-V-H-Problem-1-b}
\printexercise{subquestion}{MAT-1001-2017-V-H-Problem-1-c}
\end{enumerate}
\end{exercise}


Should give something reasonable. And indeed:

\documentclass{article}
\usepackage{amssymb,mathtools}

\providecommand*\e{e}

\usepackage{xsim}

\DeclareExerciseTagging{year} % 1992, 2010, etc
\DeclareExerciseTagging{topic}
\DeclareExerciseTagging{semester} % V (Spring), H (Fall)
\DeclareExerciseTagging{exam} % O (ordinary), K (kont / re-sit exam), P (prøveeksamen)

\DeclareExerciseType{subquestion}{
exercise-env = question ,
exercise-name = Question ,
exercise-template = item ,
solution-template = item
}

\DeclareExerciseProperty{title}

\DeclareExerciseEnvironmentTemplate{named}
{\subsection*{\GetExercisePropertyTF{title}{#1}{??}}}
{}

\DeclareExerciseEnvironmentTemplate{item}
{\item}
{}

\xsimsetup{
exercise/template = named
}

\DeclareExerciseCollection{all}

\begin{filecontents}{\jobname-exam.tex}
\begin{question}[
year=2017,semester=V,exam=O,
ID=MAT-1001-2017-V-H-Problem-1-a,
topic = integral
]
Evaluate the following integrals
\task $\displaystyle \int_0^{\sqrt{\pi/2}} (x+1) \e^{x-1} dx$
\task $\displaystyle \int \frac{a}{x^2 + a^2} dx$
\task $\displaystyle \int \log x dx$.
\end{question}

\begin{question}[
year=2017,semester=V,exam=O,
ID=MAT-1001-2017-V-H-Problem-1-b,
topic = limit
]
Evaluate the following limits
\task $\displaystyle \lim_{x \to 0} \frac{\sin(x^2)}{x^2}$
\task $\displaystyle \lim_{x \to 0} \frac{\sin(x^2)}{1 + x^2}$.
\end{question}

\begin{question}[
year=2017,semester=V,exam=O,
ID=MAT-1001-2017-V-H-Problem-1-c,
topic = injective
]
State the definition of an injective function and provle
that the function $f \to \mathbb{R} \to \mathbb{R}$ defined by $\int_0^x e^{-s^2} dx$ is injective.
\end{question}

\begin{exercise}[year=2017,semester=H,topic={integral,limit,injective},exam=K,
ID=MAT-1001-2017-V-H-Problem-1]
\begin{enumerate}
\printexercise{subquestion}{MAT-1001-2017-V-H-Problem-1-a}
\printexercise{subquestion}{MAT-1001-2017-V-H-Problem-1-b}
\printexercise{subquestion}{MAT-1001-2017-V-H-Problem-1-c}
\end{enumerate}
\end{exercise}
\end{filecontents}

\begin{document}

\collectexercises{all}
\input{\jobname-exam.tex}
\collectexercisesstop{all}

\printexercise{exercise}{MAT-1001-2017-V-H-Problem-1}

\end{document} • Did you do something special to make your example above compile? Using exactly what you wrote produces no output. Both files in same folder etc. Any suggestions? I am a bit unsure how a subquestion should be typed... Mar 9, 2020 at 15:27
• @N3buchadnezzar the only thing you need is an up to date TeX distribution. I edited the answer to make the example self-contained ( using filecontents for the additional file with the exercises), so copy/paste/compiile works nicely on my system Mar 9, 2020 at 18:04