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
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).
%%%% 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}}
\defineenumeration[question]
[text=Question,
headcommand=\QuestionTextCommand,
number=yes,
prefix=yes,
prefixsegments=chapter,
coupling=answer]
\defineenumeration[answer]
[text=Answer,
headcommand=\AnswerTextCommand,
number=yes,
prefix=yes,
prefixsegments=chapter,
coupling=question]
\defineblock[answer]
%% Why this command generates an erreor?
\define\noanswer{%
\beginanswer
\incrementcounter[answer]
\endanswer}
\define\PrintCounterValues{Here \type{\QuestionCounter = }\QuestionCounter.\par
Here \type{\AnswerCounter = }\AnswerCounter.\par
Here \type{\rawcountervalue[question] = }\rawcountervalue[question].\par
Here \type{\rawcountervalue[answer] = }\rawcountervalue[answer].
}
\starttext
\startchapter[title=Questions]
\startquestion[q:1]
Prove that ${\rm e}\sim 2.73$ is irrational.
This is the first question, with its own reference, for later use.
\PrintCounterValues
\stopquestion
\beginanswer
\startanswer
This is the answer to the first question (to \in{Question}[q:1]).
\PrintCounterValues
\stopanswer
\endanswer
\startquestion
This is the second question, without its own reference.
\PrintCounterValues
\stopquestion
\beginanswer
\startanswer[a:2]
This is the answer to the second question.
\PrintCounterValues
\stopanswer
\endanswer
\startquestion
This is the third question, an easy one, without a given solution.
\PrintCounterValues
\stopquestion
%\noanswer\ % this generates an error…
\beginanswer
%\increment\AnswerCounter
%\incrementcounter[AnswerCounter]
%\increment\QuestionCounter
%\incrementcounter[QuestionCounter]
\incrementcounter[answer]
%\incrementcounter[question]
\endanswer
\startquestion[q:2]
This is the fourth question with its own reference.
\PrintCounterValues
\stopquestion
\beginanswer
\startanswer
This is the answer to the fourth question: use the result of \in{question}[q:1].
\PrintCounterValues
\stopanswer
\endanswer
\startquestion
This is the fifth question, without its own reference. (This Question is not linked to its Answer).
\PrintCounterValues
\stopquestion
\beginanswer
\startanswer
This is the answer to the fifth question. Read again \in{Answer}[a:2].
\PrintCounterValues
\stopanswer
\endanswer
\page
\stopchapter
\startchapter[ownnumber=1,title=Answers and solutions]
\useblocks[answer]
\stopchapter
\stoptext
%%%% end example