[NTG-context] getting ConTeXt results back to Lua (for typesetting solutions at end of document)

Otared Kavian otared at gmail.com
Fri Feb 1 09:04:02 CET 2019

Hi Sanjoy,

Two or three years ago Wolfgang helped me to solve an analogous question. Here is an example which works fine, and moreover one can navigate from a question to its hint (if there is one…) and then from the hint to its solution (if present…).

Best regards: OK

%%%% Here, following Wolfgang Schuster's ideas,
%%%% we define some macros allowing to couple
%%%% Questions, Hints and Answers in an automatic way
%%%% Upon clicking on an interactive title for Question, Hint or Answer
%%%% one goes to the corresponding Hint, Answer or Question

\setupinteraction[state=start]

% We define two counters which follow the numbers
% appearing in Question and Answer

\newcounter\QuestionCounter
\newcounter\HintCounter

% We create two commands to be used in the
% enumeration environments
% Note that the check for trial typestting
% \doifnotmode{*trialtypesetting}
% is necessary in order to avoid unwanted incrementation
\define[1]\QuestionTextCommand
{\doifnotmode{*trialtypesetting}
{\doglobal\increment\QuestionCounter
\pagereference[question:\QuestionCounter]}%
\doifreferencefoundelse{hint:\QuestionCounter}
{\goto{#1}[hint:\QuestionCounter]}
{#1}}

\define[1]\HintTextCommand
{\doifnotmode{*trialtypesetting}
{\doglobal\increment\HintCounter
\pagereference[hint:\HintCounter]}%
{#1}}

{\doifnotmode{*trialtypesetting}
{#1}}

% We define here two enumeration environments for
\defineenumeration[question]
[text=Question,
number=yes,
prefix=yes,
prefixsegments=chapter,
coupling=hint]

\defineenumeration[hint]
[text=Hint,
number=yes,
prefix=yes,
prefixsegments=chapter,

number=yes,
prefix=yes,
prefixsegments=chapter,
coupling=question]

% Each question is followed immediately by its answer.
% The answers are put in a block which will be used later
\defineblock[hint]
\hideblocks[hint]

% We define a command used after a
% Question for which no Answer is provided
% Since the block commands
% cannot be used directly in a macro definition
% we use a trick...
% In the buffer the two counters associated to Answer
% are incremented...
\beginhint
\doglobal\increment\HintCounter
\incrementcounter[hint]
\endhint
\stopbuffer

% ...and then the above buffer is invoked

% example of use:
\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.
\stopquestion

\beginhint
\starthint
Write ${\rm e}$ as a series.
\stophint
\endhint

This is the answer to the first question (to \in{Question}[q:1]).

\startquestion
This is the second question, without its own reference.
\stopquestion

\beginhint
\starthint
Do you really need a hint? :-)
\stophint
\endhint

This is the answer to the second question.

(Note that this answer has a reference named \type{a:Test}).

\startquestion[q:Obvious]
This is the third question, an easy one, without a given solution.
\stopquestion

% we increment here the counters for Answer

\startquestion[q:2]
This is the fourth question with its own reference.
\stopquestion

\beginhint
\starthint
Do you really need a hint? :-)
\stophint
\endhint

This is the answer to the fourth question: use the result of \in{Question}[q:1].

\startquestion
This is the fifth question, without its own reference.

({\it Hint:} look again at \in{Question}[q:Obvious]).
\stopquestion

\beginhint
\starthint
Do you really need a hint? :-)
\stophint
\endhint

\stopchapter

% Here we say ownnumber=1, in order to match the prefix
% of numbers associated to Answer
\startchapter[ownnumber=1,title=Hints]
\useblocks[hint]
\stopchapter

\stopchapter

\stoptext

> On 1 Feb 2019, at 04:10, Sanjoy Mahajan <sanjoy at olin.edu> wrote:
>
> Thank you, Matthias and Wolfgang, for the examples/solutions.
>
> If I understand them right, there must be one solution for each exercise
> (otherwise the solution numbering gets out of sync).  Or do the
> coupling= options in Matthias's example remove that restriction?  In my
> experiments with the examples, deleting the first solution
> unsynchronized the exercise--solution numbering, no matter how I fiddled
> with the coupling keys.
>
> In the ideal pedagogical world, I'd write a solution for each exercise.
> But reality might intervene, so I still wonder if the solutions can
> automatically know and include the exercise number (perhaps by placing
> \getnumber[exercise]).  If there is a clever way, that's great.  But,
> the nature of TeX as a macro language may preclude the \getnumber code
> being evaluated just when one wants.
>
> Regards,
> -Sanjoy
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context at ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20190201/f189f2d7/attachment-0001.html>