2

I would like to conditionally add text (for example, "- Answers") to my exercise sheet title based on the global solution/print option set in \xsimsetup.

My goal is to have the title change automatically when I toggle the option, without any manual edits. I have tried many conditionals in the documentation, such as \ExerciseParameterIfSetTF but couldn't manage to make it work. I think it just work inside exercise environment.

\documentclass{article} \usepackage{xsim} \xsimsetup{ solution/print = true } \begin{document} {\Huge My Awesome Exercise Sheet : \ExerciseParameterIfSetTF{solution}{print}{Answers}{ }} \begin{exercise}[title=First Question] What is the result of $2+2$? \end{exercise} \begin{solution} The result is $4$. \end{solution} \end{document} 

1 Answer 1

3

The option solution/print is stored per exercise type, with the default type being exercise. There is an internal command \xsim_if_solution_print:nTF{exercise type}{true}{false} that queries the option for the specified exercise type. You can write a wrapper command around this internal command, with exercise as the first argument.

In the code below I used exercise as the default value for an optional argument to the wrapper command. If you have a different exercise type then you can use that optional argument with the name of the exercise type.

MWE:

\documentclass{article} \usepackage{xsim} \xsimsetup{ solution/print = true } \ExplSyntaxOn \NewDocumentCommand\IfPrintSolutions{O{exercise}mm}{% \xsim_if_solution_print:nTF {#1} {#2} {#3} } \ExplSyntaxOff \begin{document} {\LARGE My Awesome Exercise Sheet: \IfPrintSolutions{Answers}{No answers}} \begin{exercise}[title=First Question] What is the result of $2+2$? \IfPrintSolutions{Answer below}{Answer yourself} \end{exercise} \begin{solution} \IfPrintSolutions{This is the answer}{This is not the answer}: the result is $4$. \end{solution} \end{document} 

Result:

text shown for print=true

4
  • Excellent, thank you very much. Is there an analogous command that is triggered based on the option exercise/print = true or false ? Commented Sep 24 at 13:28
  • 1
    @AymaneFihadi not directly, but as an untested first guess you might be able to use \bool_if:cTF {l__xsim_exercise_exercise_print_bool}{true}{false} inside of \ExplSyntaxOn. Commented Sep 24 at 13:44
  • Thanks again \NewDocumentCommand\IfPrintExercises{mm}{ \bool_if:cTF {l__xsim_exercise_exercise_print_bool} {#1} {#2} works perfectly. I just don't know how to integrate the optional parameter for the exercise type. Commented Sep 24 at 14:15
  • @AymaneFihadi I think \NewDocumentCommand\IfPrintExercises{O{exercise}mm}{ \bool_if:cTF {l__xsim_#1_exercise_print_bool} {#2} {#3} may work for that. Commented Sep 24 at 17:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.