0

I am trying to adapt the amazingly useful answer here and its followup here about how to occasionally display the solutions after each section and numbering them in exam documentclass. The last link already explains the issue: endnotes does not expand the numbers of the solutions before moving them to the end, so the MWE underneath

\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\printanswers

\usepackage{url}
\usepackage{endnotes}

\def\enotesize{\normalsize}
\def\makeenmark{\relax}
\def\notesname{Answers}
\def\answer#1{\endnotetext{\vspace*{-3.5ex}\begin{solution}#1\end{solution}\unskip}}
\def\theanswers{\theendnotes \medskip}

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \thequestion\thepartno\thesubpart\thesubsubpart:}\par\noindent} % like the exam documentation recommends

\begin{document}
    
    \begin{questions}
        \addpoints \question This is the first question
        \begin{parts}
            \part This is part one.
            \answer{Answer 1.1}
            \part This is part two.
            \answer{Answer 1.2}
        \end{parts}     
        \addpoints \question This the second question
        
        \answer{This is the solution to question two.}
        
    \end{questions}
    
    \theanswers
    
\end{document}

still gives as an output:

Screenshot of output, the numbering of the solution only looks at the question right before endnotes

where all the solutions are numbered after the last question, part, subpart, subsubpart instead of their corresponding questions, parts, subparts, subsubparts. The solution proposed here does not work, as the question counter does not need to increase everytime a solution environment is encountered, because it can also belong to a part or subpart of the question.

Is there any way to force endnotes to expand the numbering before moving all the solutions to the endnotes?

3
  • Welcome to TEX Stackexchange
    – alchemist
    Commented Feb 13, 2023 at 11:36
  • I just read some comments about endnotes and the issues about expanding and numbering. Take a look at this summary of endnote packages: tex.stackexchange.com/questions/669517/…
    – alchemist
    Commented Feb 13, 2023 at 11:41
  • I use the package xsim to write my exams and solutions. In this question I asked about changing the gradetable, but the code in the provided MWE (rather long) shows how I manage to have my solutions ordered according to section where the questions were, while maintaining continuous numbering. It may provide you with an alternative to using exam and endnotes. The link is: tex.stackexchange.com/questions/491904/…. Please adapt the font selection as typeface won't work anymore.
    – alchemist
    Commented Feb 13, 2023 at 11:46

1 Answer 1

0

The postnotes package offers some infrastructure to store (and expand) some variables at the place the mark is set, and retrieve them when the note is printed at the end. This infrastructure is used by the package to port a number of variables like \thechapter, \thesection, \thepage and so on, and also to add support for some packages which need to see some variables "as if they were at the place of the mark". But the mechanism can be leveraged by the user too.

The basic structure is that postnotes provides a pair of hooks, postnotes/note/store which is called where the mark is set, and postnotes/print/note/begin which is called right before the note is printed at the end. And you can establish a connection between the two points by means of the note unique ID, which you can access at the first hook by \l_postnotes_note_id_tl and in the second by \l_postnotes_print_note_id_tl.

In practice:

\documentclass[a4paper]{exam}

% This is already the default nowadays.
% \usepackage[utf8]{inputenc}

\printanswers

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \pnthequestion\pnthepartno\pnthesubpart\pnthesubsubpart:}\par\noindent} % like the exam documentation recommends

% I presume this is desired?
\counterwithin*{partno}{question}

\usepackage{postnotes}
\postnotesetup{
  listenv=none,
  format=\normalsize,
  maketextmark=,
}
\renewcommand*{\pntitle}{Answers}

\NewDocumentCommand{\answer}{m}{%
  \postnote[nomark]{\vspace*{-3.5ex}\begin{solution}#1\end{solution}}}

\NewDocumentCommand{\theanswers}{}{\printpostnotes}

\ExplSyntaxOn
\AddToHook { postnotes/note/store }
  {
    \prop_new:c { g__ypnia_exam_ \l_postnotes_note_id_tl _prop }
    \prop_gput:cnx { g__ypnia_exam_ \l_postnotes_note_id_tl _prop }
      { thequestion } { \thequestion }
    \prop_gput:cnx { g__ypnia_exam_ \l_postnotes_note_id_tl _prop }
      { thepartno } { \thepartno }
    \prop_gput:cnx { g__ypnia_exam_ \l_postnotes_note_id_tl _prop }
      { thesubpart } { \thesubpart }
    \prop_gput:cnx { g__ypnia_exam_ \l_postnotes_note_id_tl _prop }
      { thesubsubpart } { \thesubsubpart }
  }
\AddToHook { postnotes/print/note/begin }
  {
    \prop_get:cnN
      { g__ypnia_exam_ \l_postnotes_print_note_id_tl _prop }
      { thequestion } \pnthequestion
    \prop_get:cnN
      { g__ypnia_exam_ \l_postnotes_print_note_id_tl _prop }
      { thepartno } \pnthepartno
    \prop_get:cnN
      { g__ypnia_exam_ \l_postnotes_print_note_id_tl _prop }
      { thesubpart } \pnthesubpart
    \prop_get:cnN
      { g__ypnia_exam_ \l_postnotes_print_note_id_tl _prop }
      { thesubsubpart } \pnthesubsubpart
  }
\ExplSyntaxOff

\begin{document}

\begin{questions}
  \addpoints \question This is the first question
  \begin{parts}
    \part This is part one.
    \answer{Answer 1.1}
    \part This is part two.
    \answer{Answer 1.2}
  \end{parts}
  \addpoints \question This the second question

  \answer{This is the solution to question two.}

\end{questions}

\theanswers

\end{document}

Results in:

enter image description here

Note that the numbering scheme in:

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \thequestion\thepartno\thesubpart\thesubsubpart:}\par\noindent}

is a little awkward. That is not the usual way to nest different levels of document counters in LaTeX, which is rather to incorporate the level above in the printed representation (\the...). Concretely, you are relying on partno, subpart and subsubpart being formatted the way they are by default in the class, such that a 0 does not produce output. That's probably fragile, but perhaps it makes sense in exam, since the sublevels are seen very close to each other, so that the nesting would be overkill. Anyway, even if the usual nesting should not be applied, I'd still use something like:

\documentclass[a4paper]{exam}

% This is already the default nowadays.
% \usepackage[utf8]{inputenc}

\printanswers

\newcommand*{\pnsolution}{}
\AddToHook{env/questions/begin}{%
  \renewcommand*{\pnsolution}{\thequestion}}
\AddToHook{env/parts/begin}{%
  \renewcommand*{\pnsolution}{\thequestion\thepartno}}
\AddToHook{env/subparts/begin}{%
  \renewcommand*{\pnsolution}{\thequestion\thepartno\thesubpart}}
\AddToHook{env/subsubparts/begin}{%
  \renewcommand*{\pnsolution}{\thequestion\thepartno\thesubpart\thesubsubpart}}

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \pnsolution:}\par\noindent} % like the exam documentation recommends

\usepackage{postnotes}
\postnotesetup{
  listenv=none,
  format=\normalsize,
  maketextmark=,
}
\renewcommand*{\pntitle}{Answers}

\NewDocumentCommand{\answer}{m}{%
  \postnote[nomark]{\vspace*{-3.5ex}\begin{solution}#1\end{solution}}}

\NewDocumentCommand{\theanswers}{}{\printpostnotes}

\ExplSyntaxOn
\AddToHook { postnotes/note/store }
  {
    \tl_new:c { g__ypnia_solution_ \l_postnotes_note_id_tl _tl }
    \tl_gset:cx { g__ypnia_solution_ \l_postnotes_note_id_tl _tl }
      { \pnsolution }
  }
\AddToHook { postnotes/print/note/begin }
  {
    \tl_set:Nv \pnsolution
      { g__ypnia_solution_ \l_postnotes_print_note_id_tl _tl }
  }
\ExplSyntaxOff

\begin{document}

\begin{questions}
  \addpoints \question This is the first question
  \begin{parts}
    \part This is part one.
    \answer{Answer 1.1}
    \part This is part two.
    \answer{Answer 1.2}
  \end{parts}
  \addpoints \question This the second question

  \answer{This is the solution to question two.}

\end{questions}

\theanswers

\end{document}

Note also that you need the latest release of postnotes (v0.2.2) for the above.

6
  • thank you for your extensive response! I was really excited about your solution, I will try it out once postnotes update v0.2.2 is available on Miktex.
    – yPNia
    Commented Feb 20, 2023 at 16:49
  • @yPNia I'd expect it to be available for you already. I've uploaded it to CTAN past Wednesday, it usually takes at most a couple of days for the mirrors to sync.
    – gusbrs
    Commented Feb 20, 2023 at 17:35
  • miktex.org/packages postnotes was last updated on the Miktex mirror on 31/12/2022
    – yPNia
    Commented Feb 21, 2023 at 7:02
  • @yPNia Mmh, I guess I'm just used to TeX Live, and MiKTeX is different. I didn't know that. Anyway, if you don't want to wait, the mechanism itself already exists since the package was released, the only reason the answer needs the latest version is that I renamed a couple of relevant things to it. So, the postnotes/print/note/begin hook was called postnotes/print/eachnote (before v0.2.2), and the ID variables were \l__postnotes_note_id_tl and \l__postnotes_print_note_id_tl (before v0.2.1).
    – gusbrs
    Commented Feb 21, 2023 at 9:51
  • @yPNia Indeed, MiKTeX is different (see tex.stackexchange.com/a/599945/105447), but according to the answer it shouldn't take much longer.
    – gusbrs
    Commented Feb 21, 2023 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .