1

I changed inside \DeclareExerciseEnvironmentTemplate{custom}{...}

\IfInsideSolutionF{\rule{1.2cm}{1pt}\slash}%

to

\IfInsideSolutionF{\noindent\usekomafont{sectioning}\color{pointscolor}}

and I used a method for calculating sums of points from here https://tex.stackexchange.com/a/494468/46023

why do I get several vertical shifts here?

\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}

enter image description here

3
  • Unrelated: the newest version of xsim correctly adds the points with \addpoints the the sum of points of the exercise, see the discussion here: github.com/cgnieder/xsim/issues/58
    – cgnieder
    Feb 29 '20 at 18:54
  • @cgnieder Do you mean this is a problem with marginpar not with xsim?
    – cis
    Feb 29 '20 at 19:37
  • I do not have an example without that error!
    – cis
    Mar 1 '20 at 0:05
2

You can work around this with a suitable template definition where a) the \marginpar is issued before the paragraph of the heading ends and b) its content starts with \leavevmode itself (I have not researched why the latter seems to be necessary when colors are in play – BTW: \usekomafont{sectioning} issues \normalcolor).

Below I also removed the manual calculation of points. It is not necessary any more with an up to date 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}

enter image description here

9
  • He wants me to install pdftexcmds.sty infwarerr.sty ltxcmds.sty Ok, why not. But I do not get the points-sums (10 points and so on) with that code....
    – cis
    Mar 1 '20 at 17:37
  • @cis The picture above was created with a complete texlive 2019 updated this morning…
    – cgnieder
    Mar 1 '20 at 17:40
  • I did tlmgr update --all this afternoon. This was not enough?
    – cis
    Mar 1 '20 at 17:44
  • BTW: There occurs no error or something like that. It show simply no point-sums....
    – cis
    Mar 1 '20 at 17:50
  • @cis after how many compilations?
    – cgnieder
    Mar 1 '20 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.