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

cgnieder
  • 66,645
cis
  • 8,073
  • 1
  • 16
  • 45
  • Unrelated: the newest version of xsim correctly adds the points with \addpoints the the sum of points of the exercise, see the discussion here: https://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 01 '20 at 00:05

1 Answers1

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

cgnieder
  • 66,645
  • 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 01 '20 at 17:37
  • @cis The picture above was created with a complete texlive 2019 updated this morning… – cgnieder Mar 01 '20 at 17:40
  • I did tlmgr update --all this afternoon. This was not enough? – cis Mar 01 '20 at 17:44
  • BTW: There occurs no error or something like that. It show simply no point-sums.... – cis Mar 01 '20 at 17:50
  • @cis after how many compilations? – cgnieder Mar 01 '20 at 17:51
  • About 3-4 (or uncountable more...) :() – cis Mar 01 '20 at 17:57
  • @cis I just copied my code from above: it gives the picture I posted after two compilations. Maybe you have some local packages interfering or something… does it say Package: xsim 2020/02/23 v0.18 in your log? – cgnieder Mar 01 '20 at 18:01
  • I have been completely confused, because I have the latest version. But there was a mistake in my file system. It works! ... fine! – cis Mar 01 '20 at 18:26
  • xsim is a very fine package, by the way. But hard-boiled to use. ;) Good written manual, that not copies the line numbers of code-blocks. but you wouldn't have expected otherwise from @cgnieder. – cis Mar 01 '20 at 19:54