cgnieder / xsim

eXercise Sheets IMproved
65 stars 20 forks source link

counter not found by \GetExerciseProperty{counter} [v.0.20 regression from v.0.19] #91

Closed ptoche closed 2 years ago

ptoche commented 2 years ago

I posted the following on stackexchange, but then it occurred to me that perhaps it would be better as an issue here. I can delete the stackexchange question if this is best discussed here. Please do let me know.

https://tex.stackexchange.com/questions/617147/xsim-question-counter-does-not-print

The following template worked with xsim v.0.19, but no longer works with v.0.20. The problem seems to be that \GetExerciseProperty{counter} is unable to retrieve the counter.

The document compiles correctly with "overleaf" and it compiled fine. However, overleaf uses xsim 2020/04/04 v0.19b eXercise Sheets IMproved with Version 3.14159265-2.6-1.40.21 (TeX Live 2020) whereas I'm using xsim 2021/02/03 v0.20c eXercise Sheets IMproved with Version 3.141592653-2.6-1.40.23 (TeX Live 2022/dev) (preloaded format=pdflatex 2021.9.28)

LaTeX info:

Package: xsim 2021/02/03 v0.20c eXercise Sheets IMproved
PDFLATEX: pdflatex -version
pdfTeX 3.141592653-2.6-1.40.23 (TeX Live 2022/dev)

Broken Template:

\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{filecontents} % for self-contained example
\begin{filecontents}{QuestionBank.tex}
\begin{mc}[ID=Q001]
First Question: Select one of the following solutions:
\begin{enumerate}
\item A
\item B
\end{enumerate}
\end{mc}
\begin{hint}
Solution to First Question
\end{hint}
\begin{mc}[ID=Q002]
Second Question: Select one of the following solutions:
\begin{enumerate}
\item C
\item D
\end{enumerate}
\end{mc}
\begin{hint}
Solution to Second Question
\end{hint}
\end{filecontents}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[verbose,clear-aux]{xsim}
\usepackage[most]{tcolorbox}

\newcommand*\includeQuestion[1]{%
\XSIMexpandcode{\printexercise{mc}{\GetExerciseIdForProperty{ID}{#1}}}%
}

\newcommand*\includeSolution[1]{%
\XSIMexpandcode{\printsolution{mc}{\GetExerciseIdForProperty{ID}{#1}}}%
}

\DeclareExerciseEnvironmentTemplate{mctemplate}
{%
    \tcolorbox[breakable,
        drop shadow,
        beforeafter skip=1\baselineskip,
        fonttitle=\bfseries,
        fontupper=\normalsize,
        valign=top,
        colframe=\IfInsideSolutionF{green!20!white}%
                 \IfInsideSolutionT{blue!20!white},
        colback=\IfInsideSolutionF{green!3!white}%
                \IfInsideSolutionT{blue!3!white},
        coltext=black,
        coltitle=black,
        boxrule=1pt,
        width=\linewidth,
        left=2mm,
        title=\IfInsideSolutionT{%
          \XSIMmixedcase{\XSIMtranslate{solution}}~to~}%
          \XSIMmixedcase{\XSIMtranslate{question}}~\GetExerciseProperty{counter}%
    ]%
}{\endtcolorbox}

\DeclareExerciseType{mc}{
    exercise-env = mc,
    solution-env = hint,
    exercise-name = question,% used with headings=true
    solution-name = answer,% used with headings=true
    exercise-template = mctemplate,
    solution-template = mctemplate,
    counter = mc
}

\DeclareExerciseCollection{myCollection}

\begin{document}

\collectexercises{myCollection}
\input{QuestionBank.tex}
\collectexercisesstop{myCollection}

\section*{Multiple Choice Questions with XSIM}
Total number of questions: \numberofmcs 

\noindent
The counter above should be 2, not 0.

\noindent
The following should print \textbf{Question 1} and \textbf{Solution to Question 1}. And likewise \textbf{Question 2} and \textbf{Solution to Question 2}.

\includeQuestion{Q001}
\includeSolution{Q001}

\includeQuestion{Q002}
\includeSolution{Q002}

\end{document}

enter image description here

sidispa commented 2 years ago

Move \collectexercisesstop{myCollection} after issuing the last question.

ptoche commented 2 years ago

Thanks Simon! In typical usage it's enough to place it immediately after the last instance of \end{exercise}. And it used to be fine placing it immediately after the \input{QuestionBank.tex}. In fact the manual shows an example like that on page 25:

  \collectexercises{foo-easy}
  \xsimsetup{difficulty=easy}
  \input{foo.tex}
  \collectexercisesstop{foo-easy}

There is a warning in the manual which may be designed to hint at a counter issue, but I can't really undestand it. Page 23:

''' Please be aware that exercises are not used or printed while they are collected. Nonethe- less the property use is set to true (so that solutions can be printed even if the exercises are not) and the property print is set to false. Also their counters are not stepped during the process. This only happens when they are printed the first time, cf . the used property. At that time also the properties page, section and chapter are set and the property print is set to true. '''

Thanks again!