我内心改变了\DeclareExerciseEnvironmentTemplate{custom}{...}
\IfInsideSolutionF{\rule{1.2cm}{1pt}\slash}%
到
\IfInsideSolutionF{\noindent\usekomafont{sectioning}\color{pointscolor}}
我使用了一种计算点数总和的方法https://tex.stackexchange.com/a/494468/46023
为什么这里会出现几次垂直转变?
\documentclass[]{scrartcl}
%\usepackage[ngerman]{babel}
\usepackage{xcolor}
\colorlet{pointscolor}{blue}
\usepackage[showframe,
left=2cm,
right=4cm,
marginparwidth=2.125cm,
]{geometry}
\usepackage{calc}
\usepackage{xsim}
%\xsimsetup{
%% clear-aux,
% solution/print = false,
% grading-table/template = default,
% grading-table/type = exercise,
%}
\newcounter{expoints}
\newcommand{\addpts}[1]{%
\addtocounter{expoints}{#1}%
\SetExpandedExerciseProperty{points}{\theexpoints}%
\marginpar[]{\color{pointscolor}\points{#1}}%
}
\xsimsetup{
exercise/pre-hook ={\setcounter{expoints}{0}}
}
\makeatletter
\DeclareExerciseEnvironmentTemplate{custom}{%
\GetExerciseHeadingF{\subsection*}%
{%
\XSIMmixedcase{\GetExerciseName}\nobreakspace
\GetExerciseProperty{counter}%
\IfInsideSolutionF
{%
\GetExercisePropertyT{subtitle}
{ {\normalfont\itshape\PropertyValue}}%
}%
}
\GetExercisePropertyT{points}
{%
\marginpar
{%
\IfInsideSolutionF{\noindent\usekomafont{sectioning}\color{pointscolor}}% \rule{1.2cm}{1pt}\slash
\printgoal{\PropertyValue}
\GetExercisePropertyT{bonus-points}{~(+\printgoal{\PropertyValue})
}%
~\XSIMtranslate{points}% point-abbr
}%
}%
}
{\par}
\makeatother
\xsimsetup{
exercise/template = custom ,
solution/template = custom
}
\begin{document}
% \gradingtable
\begin{exercise}[subtitle=aaa]
\begin{enumerate}
\item subquestion 1 \addpts{2}
\item subquestion 2 \addpts{3}
\item subquestion 3 \addpts{5}
\end{enumerate}
\end{exercise}
\begin{exercise}[points=7]
another exercise
\end{exercise}
\begin{exercise}
\begin{enumerate}
\item subquestion 1 \addpts{1}
\item subquestion 2 \addpts{2}
\item subquestion 3 \addpts{3}
\end{enumerate}
\end{exercise}
\end{document}
答案1
您可以使用合适的模板定义来解决这个问题,其中 a)\marginpar
在标题段落结束之前发出,并且 b) 其内容以\leavevmode
自身开始(我没有研究为什么在使用颜色时后者似乎是必要的 - 顺便说一句:\usekomafont{sectioning}
问题\normalcolor
)。
下面我还删除了手动计算点数。有了最新的版本,就不再需要手动计算点数了xsim
。
\documentclass{scrartcl}
\usepackage[
showframe,
left=2cm,
right=4cm,
marginparwidth=2.125cm
]{geometry}
\usepackage{xsim}
\usepackage{xcolor,needspace}
\colorlet{pointscolor}{blue}
\newcommand*\pointformat{\usekomafont{sectioning}\color{pointscolor}}
\newcommand*\addpts[1]{\marginpar{\leavevmode\pointformat{\addpoints{#1}}}}
\makeatletter
\DeclareExerciseEnvironmentTemplate{custom}
{%
\par\vspace{3.25ex plus 1ex minus .2ex}
\Needspace*{3\baselineskip}%
\noindent\usekomafont{sectioning}%
\XSIMmixedcase{\GetExerciseName}\nobreakspace
\GetExerciseProperty{counter}%
\IfInsideSolutionF{%
\GetExercisePropertyT{subtitle}
{ {\normalfont\itshape\PropertyValue}}%
}%
\GetExercisePropertyT{points}{%
\marginpar{%
\leavevmode\pointformat
\printgoal{\PropertyValue}%
\GetExercisePropertyT{bonus-points}{~(+\printgoal{\PropertyValue})}%
~\XSIMtranslate{points}%
}%
}%
\par\vspace{1.5ex plus .2ex}
\normalsize\normalfont
\@afterindentfalse\@afterheading
}
{\par}
\makeatother
\xsimsetup{
exercise/template = custom ,
solution/template = custom
}
\begin{document}
\begin{exercise}[subtitle=aaa]
\begin{enumerate}
\item subquestion 1 \addpts{2}
\item subquestion 2 \addpts{3}
\item subquestion 3 \addpts{5}
\end{enumerate}
\end{exercise}
\begin{exercise}[points=7]
another exercise
\end{exercise}
\begin{exercise}
\begin{enumerate}
\item subquestion 1 \addpts{1}
\item subquestion 2 \addpts{2}
\item subquestion 3 \addpts{3}
\end{enumerate}
\end{exercise}
\end{document}