Otared Kavian
6. November 2016 um 11:43
Hi Wolfgang,

Thanks for the solution you sent yesterday.
Your solutions solves the problem I was fighting with, but when I try to apply it to my real document there is an issue when some questions don’t have answers. This prevents the synchronization of Questions and Answers if one doesn't issue the commands
 
\beginanswer
\incrementcounter[answer]
\endanswer

right after a question without an answer. And when the above commands are issued, the last Question does not show the link to its corresponding Answer.

Below is your modified example where one can see the problem (please see the fifth Question in the source below).

Best regards: OK
%%%% begin example-ws.tex

\setupinteraction[state=start]

\newcounter\QuestionCounter
\newcounter\AnswerCounter

\define[1]\QuestionTextCommand
  {\doglobal\increment\QuestionCounter
   \pagereference[question:\QuestionCounter]%
   \doifreferencefoundelse{answer:\QuestionCounter}
     {\goto{#1}[answer:\QuestionCounter]}
     {#1}}

\define[1]\AnswerTextCommand
  {\doglobal\increment\AnswerCounter
   \pagereference[answer:\AnswerCounter]%
   \doifreferencefoundelse{question:\AnswerCounter}
     {\goto{#1}[question:\AnswerCounter]}
     {#1}}
Add a check for triatypesetting mode to increment \QuestionCounter
only once for each question.

\define[1]\QuestionTextCommand
  {\doifnotmode{*trialtypesetting}
     {\doglobal\increment\QuestionCounter
      \pagereference[question:\QuestionCounter]}%
   \doifreferencefoundelse{answer:\QuestionCounter}
     {\goto{#1}[answer:\QuestionCounter]}
     {#1}}

\define[1]\AnswerTextCommand
  {\doifnotmode{*trialtypesetting}
     {\doglobal\increment\AnswerCounter
      \pagereference[answer:\AnswerCounter]}%
   \doifreferencefoundelse{question:\AnswerCounter}
     {\goto{#1}[question:\AnswerCounter]}
     {#1}}
%% Why this command generates an erreor?
\define\noanswer{%
\beginanswer
\incrementcounter[answer]
\endanswer}
The block environment can’t be used within a definition but
you put the content in a bufer and call then the buffer content
with your \noanswer command.

\startbuffer[noanswer]
    \beginanswer
    \doglobal\increment\AnswerCounter
    \incrementcounter[answer]
    \endanswer
\stopbuffer

\define\noanswer
  {\getbuffer[noanswer]}

Wolfgang