有没有简单的方法可以计算某个部分包含的分数?
\documentclass{scrartcl}
\usepackage[no-files]{xsim}
\xsimsetup{
exercise/the-counter = \thesection.\arabic{exercise},
exercise/within = section
}
\begin{document}
\section{First section \\ {\small You can reach ?? points in this section}}
\begin{exercise}[points=3.5]
An exercise in section 1
\end{exercise}
\section{Second section \\ {\small You can reach ?? points in this section}}
\begin{exercise}[points=1]
An exercise in section 2
\end{exercise}
\begin{exercise}[points=2]
Another exercise in section 2
\end{exercise}
\section{Total \\ {You can reach \printtotalpoints\, in this letter.}}
\end{document}
答案1
您可以循环练习,与当前部分值进行比较并求和:
\cs_new_protected:Npn \cis_section_points:
{
\fp_zero:N \l_tmpa_fp
\xsim_foreach_exercise_id_type:nn {used}
{
\int_compare:nNnT
{ \xsim_get_property:nnn {##1} {##2} {section-value}+0 } = { \value {section} }
{ \fp_add:Nn \l_tmpa_fp {##5} }
}
\xsim_print_goal:nnn
{ \fp_to_decimal:N \l_tmpa_fp }
{ \, \XSIMtranslate {point} }
{ \, \XSIMtranslate {points} }
}
上面使用的宏没有直接记录。但是,它们对应的接口命令有。
\xsim_foreach_exercise_id_type:nn {used} {..}
是相同的\ForEachUsedExerciseByID{..}
,\xsim_get_property:nnn {..} {..} {..}
等同于\ExercisePropertyGet{..}{..}{..}
,并且\xsim_print_goal:nnn {..} {..} {..}
是相同的\ExerciseGoalValuePrint{..}{..}{..}
这也意味着如果您对 expl3 不熟悉,您也可以使用记录的命令,例如与etoolbox
的工具一起使用:
\newrobustcmd*\sectionpoints{%
\def\thissectionpoints{0}%
\ForEachUsedExerciseByID{%
\ifnumcomp
{\ExercisePropertyGet{##1}{##2}{section-value}+0}={\value{section}}
{\edef\thissectionpoints{\thissectionpoints+##5}}
{}%
}%
\ExerciseGoalValuePrint
{\thissectionpoints}
{\,\XSIMtranslate{point}}
{\,\XSIMtranslate{points}}%
}
完整示例:
\documentclass{scrartcl}
\usepackage[no-files]{xsim}
\xsimsetup{
exercise/the-counter = \thesection.\arabic{exercise},
exercise/within = section
}
\newrobustcmd*\sectionpoints{%
\def\thissectionpoints{0}%
\ForEachUsedExerciseByID{%
\ifnumcomp
{\ExercisePropertyGet{##1}{##2}{section-value}+0}={\value{section}}
{\edef\thissectionpoints{\thissectionpoints+##5}}
{}%
}%
\ExerciseGoalValuePrint
{\thissectionpoints}
{\,\XSIMtranslate{point}}
{\,\XSIMtranslate{points}}%
}
\begin{document}
\section{First section}
You can reach \sectionpoints\ in this section
\begin{exercise}[points=3.5]
An exercise in section 1
\end{exercise}
\section{Second section}
You can reach \sectionpoints\ in this section
\begin{exercise}[points=1]
An exercise in section 2
\end{exercise}
\begin{exercise}[points=2]
Another exercise in section 2
\end{exercise}
\end{document}