I am trying to use two ExerciseTypes 'exercise' and 'question' using xsim. Both the ExerciseTypes will be used to display 'exercises' and 'questions' in different sections of a book and their answers separately at the end of the book. How to use ExerciseType 'exercise' for the desired purpose has been described by @cgnieder here. I have adopted the code as below:
\DeclareExerciseEnvironmentTemplate{exercise-temp}{%
\par\noindent
\textbf{\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}
\newcommand\printsectionexercises{%
\ForEachUsedExerciseByType{%
\ifnum\ExercisePropertyGet{exercise}{##2}{chapter-value}=\value{chapter}
\ifnum\ExercisePropertyGet{exercise}{##2}{section-value}=\value{section}
\XSIMprint{exercise}{exercise}{##2}%
\fi
\fi
}%
}
\newcommand\printexercisesolutions{%
\def\currentchapter{}%
\def\currentsection{}%
\def\lastchapter{}%
\def\lastsection{}%
\ForEachUsedExerciseByType{%
\let\lastchapter\currentchapter
\let\lastsection\currentsection
\edef\currentchapter{\ExercisePropertyGet{exercise}{##2}{chapter-value}}%
\edef\currentsection{\ExercisePropertyGet{exercise}{##2}{section-value}}%
\ifx\lastchapter\currentchapter\else
\section*{Chapter \ExercisePropertyGet{exercise}{##2}{chapter}}
\fi
\ifx\lastsection\currentsection\else
\subsection*{Exercise \ExercisePropertyGet{exercise}{##2}{section}}
\fi
\XSIMprint{solution}{exercise}{##2}%
}%
}
\xsimsetup{
exercise/template = exercise-temp ,
solution/template = exercise-temp ,
exercise/within = section ,
exercise/the-counter = \arabic{exercise}. ,
exercise/print = false
}
To print an 'exercise', I use
\subsection*{Exercise \thesection}
\printsectionexercises
And to provide 'answer' to all the exercises at the end of the book, I use
\printexercisesolutions
The above codes give the desired result.
Next, I am trying to adopt the above code for the other ExerciseType 'question' as below:
\DeclareExerciseEnvironmentTemplate{question-temp}{%
\par\noindent
\textbf{\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}
\DeclareExerciseType{question}{
exercise-env = question ,
solution-env = answer ,
exercise-name = \XSIMtranslate{Question} ,
exercises-name = \XSIMtranslate{Questions} ,
solution-name = \XSIMtranslate{Answer} ,
solutions-name = \XSIMtranslate{Answers} ,
exercise-template = question-temp ,
solution-template = question-temp ,
exercise-heading = \subsection* ,
solution-heading = \subsection*
}
\newcommand{\printsectionquestions}{%
\ForEachUsedExerciseByType{%
\ifnum\ExercisePropertyGet{question}{##2}{chapter-value}=\value{chapter}
\ifnum\ExercisePropertyGet{question}{##2}{section-value}=\value{section}
\XSIMprint{question}{question}{##2}%
\fi
\fi
}%
}
\newcommand{\printquestionsolutions}{%
\def\currentchapter{}%
\def\currentsection{}%
\def\lastchapter{}%
\def\lastsection{}%
\ForEachUsedExerciseByType{%
\let\lastchapter\currentchapter
\let\lastsection\currentsection
\edef\currentchapter{\ExercisePropertyGet{question}{##2}{chapter-value}}%
\edef\currentsection{\ExercisePropertyGet{question}{##2}{section-value}}%
\ifx\lastchapter\currentchapter\else
\section*{Chapter \ExercisePropertyGet{question}{##2}{chapter}}
\fi
\ifx\lastsection\currentsection\else
\subsection*{Question \ExercisePropertyGet{question}{##2}{section}}
\fi
\XSIMprint{answer}{question}{##2}%
}%
}
\xsimsetup{
exercise/template = question-temp ,
solution/template = question-temp ,
exercise/within = section ,
exercise/the-counter = \arabic{question}. ,
exercise/print = false
}
To print the 'question', I am using
\subsection*{Question \thesection}
\printsectionquestions
And to print the 'answer' to the questions at the end, I'm using
\printquestionsolutions
But the above codes for 'question' is not giving results similar to the corresponding code for 'exercise'.
Question: The result of the code for 'question' is different from that of the 'exercise' in the following two ways:
- The heading of the question set (for example, 'Question 1.1' in the following screenshot) appears below the question set while it should appear above:
- The collection of answers at the end displays only the headings of the question sets (as in the following screenshot) but does not display the corresponding answers:
I would be grateful if someone could help with the above problems. I am new to latex so please bear with me.
The complete code for ExerciseType 'Question' is as below:
\documentclass[fontsize=11pt]{book}
\usepackage{xsim}
%%For 'Questions'%%
\DeclareExerciseEnvironmentTemplate{question-temp}{%
\par\noindent
\textbf{\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}
\DeclareExerciseType{question}{
exercise-env = question ,
solution-env = answer ,
exercise-name = \XSIMtranslate{Question} ,
exercises-name = \XSIMtranslate{Questions} ,
solution-name = \XSIMtranslate{Answer} ,
solutions-name = \XSIMtranslate{Answers} ,
exercise-template = question-temp ,
solution-template = question-temp ,
exercise-heading = \subsection* ,
solution-heading = \subsection*
}
\newcommand{\printsectionquestions}{%
\ForEachUsedExerciseByType{%
\ifnum\ExercisePropertyGet{question}{##2}{chapter-value}=\value{chapter}
\ifnum\ExercisePropertyGet{question}{##2}{section-value}=\value{section}
\XSIMprint{question}{question}{##2}%
\fi
\fi
}%
}
\newcommand{\printquestionsolutions}{%
\def\currentchapter{}%
\def\currentsection{}%
\def\lastchapter{}%
\def\lastsection{}%
\ForEachUsedExerciseByType{%
\let\lastchapter\currentchapter
\let\lastsection\currentsection
\edef\currentchapter{\ExercisePropertyGet{question}{##2}{chapter-value}}%
\edef\currentsection{\ExercisePropertyGet{question}{##2}{section-value}}%
\ifx\lastchapter\currentchapter\else
\section*{Chapter \ExercisePropertyGet{question}{##2}{chapter}}
\fi
\ifx\lastsection\currentsection\else
\subsection*{Question \ExercisePropertyGet{question}{##2}{section}}
\fi
\XSIMprint{answer}{question}{##2}%
}%
}
\xsimsetup{
exercise/template = question-temp ,
solution/template = question-temp ,
exercise/within = section ,
exercise/the-counter = \arabic{question}. ,
exercise/print = false
}
\begin{document}
\mainmatter
\chapter{Chapter on Cat}
\section{Cat-1}
SOME TEXT...
\begin{question}
Question 1 in Section 1.1 in Chapter 1
\end{question}
\begin{answer}
Answer of Question 1 in Section 1.1 in Chapter 1
\end{answer}
\begin{question}
Question 2 in Section 1.1 in Chapter 1
\end{question}
\begin{answer}
Answer of Question 2 in Section 1.1 in Chapter 1
\end{answer}
\subsection*{Question \thesection}
\printsectionquestions
\section{Cat-2}
SOME TEXT...
\begin{question}
Question 1 in Section 1.2 in Chapter 1
\end{question}
\begin{answer}
Answer of Question 1 in Section 1.2 in Chapter 1
\end{answer}
\begin{question}
Question 2 in Section 1.2 in Chapter 1
\end{question}
\begin{answer}
Answer of Question 2 in Section 1.2 in Chapter 1
\end{answer}
\subsection*{Question \thesection}
\printsectionquestions
\chapter{Chapter on Dog}
\section{Dog-1}
SOME TEXT...
\begin{question}
Question 1 in Section 2.1 in Chapter 2
\end{question}
\begin{answer}
Answer of Question 1 in Section 2.1 in Chapter 2
\end{answer}
\begin{question}
Question 2 in Section 2.1 in Chapter 2
\end{question}
\begin{answer}
Answer of Question 2 in Section 2.1 in Chapter 2
\end{answer}
\subsection*{Question \thesection}
\printsectionquestions
\section{Dog-2}
SOME TEXT...
\begin{question}
Question 1 in Section 2.2 in Chapter 2
\end{question}
\begin{answer}
Answer of Question 1 in Section 2.2 in Chapter 2
\end{answer}
\begin{question}
Question 2 in Section 2.2 in Chapter 2
\end{question}
\begin{answer}
Answer of Question 2 in Section 2.2 in Chapter 2
\end{answer}
\subsection*{Question \thesection}
\printsectionquestions
\chapter*{\underline{Answers to Questions}}
\printquestionsolutions
\end{document}
EDIT: Finally, I want something like the following using 'exercise' and 'question' in the same file: