2

I am trying to print the total points of my exercises using the "\num"-command of siunitx, in order to have a comma separted format.

I am getting an invalid-token error when trying

 \num{\TotalExerciseGoal{points}{}{}} 

which is probably because the xsim-command already prints a string which is not appliable to the "\num"-function.

I think the following MWE makes things more clear:

\documentclass{report}
\usepackage{xsim}
\usepackage{siunitx}

\sisetup{locale=DE}

\begin{document}
    % Not working:
    % Total Points: \num{\TotalExerciseGoal{points}{}{}}

    % Should Do:
    Total Points: \num{8.5}

    \begin{exercise}[points=5]
        Test exercise 1
    \end{exercise}
    \begin{exercise}[points=3.5]
        Test exercise 2
    \end{exercise}

\end{document}

Any ideas?

mspee
  • 43

2 Answers2

1

\TotalExerciseGoal is not expandable, but it could be made so.

\documentclass{report}
\usepackage{xsim}
\usepackage{siunitx}

\ExplSyntaxOn
\cs_set:Npn \xsim_print_goal:nnn #1#2#3
  {
    \fp_to_decimal:n {#1}
    \fp_compare:nTF { (#1) = 1 } {#2} {#3}
  }
\cs_set:Npn \xsim_print_goal_sum:nnnn #1#2#3#4
  {
    \xsim_print_goal:nnn { \fp_use:c {g__xsim_total_#1_goal_#2_fp} } {#3} {#4}
  }
\cs_set:Npn \xsim_print_total_goal_sum:nnn #1#2#3
  {
    \xsim_print_goal:nnn { \fp_use:c {g__xsim_total_goal_#1_fp} } {#2} {#3}
  }

\RenewExpandableDocumentCommand \TotalExerciseGoal {mmm}
  { \xsim_print_total_goal_sum:nnn {#1} {#2} {#3} }
\ExplSyntaxOff

\sisetup{locale=DE}

\begin{document}

Total Points: \num{\TotalExerciseGoal{points}{}{}}

Total Points: \num{8.5}

\begin{exercise}[points=5]
  Test exercise 1
\end{exercise}

\begin{exercise}[points=3.5]
  Test exercise 2
\end{exercise}

\end{document}

I guess you can ask the package author to do the appropriate changes.

enter image description here

egreg
  • 1,121,712
1

You could just tell xsim to use \num:

\documentclass{report}
\usepackage{xsim}
\usepackage{siunitx}

\sisetup{locale=DE}
\xsimsetup{ goal-print = \num{#1} }

\RenewDocumentCommand\printpoints{}{%
  \TotalExerciseTypeGoal{exercise}{points}{}{}%
}

\begin{document}
    Total Points: \printpoints

    \begin{exercise}[points=5]
        Test exercise 1
    \end{exercise}
    \begin{exercise}[points=3.5]
        Test exercise 2
    \end{exercise}

\end{document}

enter image description here

This also prints the points in the margin correctly…

cgnieder
  • 66,645