7

I have some exercises with ID.

\documentclass{report}
\usepackage[paperwidth=17cm, paperheight=24cm, left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{fouriernc}
\usepackage{xsim,pgffor}
\usepackage{needspace}
\DeclareExerciseEnvironmentTemplate{runin}
{%
\par\vspace{\baselineskip}
\Needspace * {2\baselineskip}
\noindent
\textbf{\XSIMmixedcase{\GetExerciseName}~\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{#1}} %
\GetExercisePropertyT{points}{%
\marginpar{%
\PropertyValue
\GetExercisePropertyT{bonus-points}{+\PropertyValue}%
\,\IfExerciseGoalSingularTF{points}
 {\XSIMtranslate{point}}
{\XSIMtranslate{points}}%
 }%
 }%
 }
 {}
\xsimsetup{
exercise/within=section ,
exercise/template=runin,
solution/template=runin,
solution/print = true,
exercise/the-counter = \thesection.\arabic{exercise}.}
\DeclareExerciseCollection{exam}

\begin{document}
\chapter{AAA}
\section{BBB}
\collectexercises{exam}
\begin{exercise}[ID=q1]
  1
\end{exercise}
\begin{solution}
  1
\end{solution}
\begin{exercise}[ID=q2]
  2
\end{exercise}
\begin{solution}
  2
\end{solution}
\begin{exercise}[ID=q3]
  3
\end{exercise}
\begin{solution}
  3
\end{solution}
\begin{exercise}[ID=q4]
  4
\end{exercise}
\begin{exercise}[ID=q5]
  5
\end{exercise}
\begin{exercise}[ID=q6]
  6
\end{exercise}
\begin{exercise}[ID=q7]
  7
\end{exercise}
\begin{exercise}[ID=q8]
  8
\end{exercise}
\begin{exercise}[ID=q9]
  9
\end{exercise}

\begin{exercise}[ID=q10]
  10
\end{exercise}
\printcollection{all exercises}
\section{Print with ID}
\printexercise{exercise}{q3}

\section{Answers}
\printsolutions
\end{document} 

Now I want to print one (or some) exercise. I tried

\printexercise{exercise}{q3}

or

\printexercise{exercise}{q1,q3,q5}

But I can't get the result. How can I get it?

4
  • \printexercise{exercise}{q1,q3,q5} can't work. It doesn't even match the description of the \printexercise... you're missing that there are two different types of id: id and ID. \XSIMexpandcode{\printexercise{exercise}{\GetExerciseIdForProperty{ID}{q3}}} should work… (it would probably best to define a wrapper macro \newcommand\insertexercise[1]{\XSIMexpandcode{\printexercise{exercise}{\GetExerciseIdForProperty{ID}{#1}}}})
    – cgnieder
    Jul 25, 2017 at 17:30
  • @clemens I copied your code, but I didn't get the result. Is there a command in front of \begin{document}? Jul 25, 2017 at 23:42
  • I don't understand what you mean
    – cgnieder
    Jul 26, 2017 at 11:03
  • BTW your code is missing \collectexercisesstop{exam}
    – cgnieder
    Jul 26, 2017 at 11:05

1 Answer 1

3

I tried [...]

\printexercise{exercise}{q1,q3,q5}

That actually doesn't make much sense as it doesn't follow the command description of \printexercise (p.19 of the current manual):

  • \printexercise{<type>}{<id>}
    Inserts the exercise of type <type> with the id <id>.

It is important to notice expects the id whereas in the question you tried to use the ID (p.10 of the current manual):

  • id = {<integer>}
    Holds the internal id of an exercise. Cannot be set by the user.
  • ID = {<text>}
    Holds the user id of an exercise if defined. Otherwise it is equal to id.

Here is a way that works:

\documentclass{article}
\usepackage{xsim}
\begin{document}

\begin{exercise}[ID=foo]
  bla bla
\end{exercise}

\XSIMexpandcode{\printexercise{exercise}{\GetExerciseIdForProperty{ID}{foo}}}

\end{document}

or probably better

\documentclass{article}
\usepackage{xsim}

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

\begin{document}

\begin{exercise}[ID=foo]
  bla bla
\end{exercise}

\insertexercise{foo}

\end{document}

enter image description here

9
  • Suppose I have 10 exercises with ID=foo, when I type \insertexercise{foo}, I only get one exercise. How can I get enough 10 exercises? Jul 27, 2017 at 7:04
  • @toandhsp each ID must be individual. (If assigning the same ID to several exercises doesn't throw an error when compiling then there is a bug in xsim.) It seems you want to insert exercises fitting certain tags, maybe something like this: github.com/cgnieder/xsim/blob/master/examples/…
    – cgnieder
    Jul 27, 2017 at 12:28
  • I cleared points, but when I compile, I still get points. How can I clear all points? Jul 27, 2017 at 15:38
  • 1
    @MichielVanCouwenberghe It is needed so \printexercise sees the result (i.e. the id) of \GetExerciseIdForProperty{ID}{#1} instead of seeing a macro which eventually expands to the id
    – cgnieder
    Oct 4, 2019 at 9:43
  • 1
    @MichielVanCouwenberghe a similar reason. Try \do{\expandafter\insertexercise\expandafter{\x}
    – cgnieder
    Oct 4, 2019 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .