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

Weber, Matthias matweber at indiana.edu
Thu Jan 31 23:28:02 CET 2019


A while back I asked a similar question, and Hans pointed out the following which I still use:

\defineenumeration
 [exercise]
 [way=bysection,
  text=Exercise,
  coupling=solution,
  location=hanging]

\defineenumeration
 [solution]
 [text=Solution,
  way=bysection,
  coupling=exercise,
  location=hanging]

\defineblock[exercise]
\defineblock[solution]
\hideblocks[solution]

\setupinteraction
 [state=start]

\starttext

\section{Main Text}

\subsection[subsec:1]{problem section 1}

\startexercise
Addition\index{addition} problem: $1+1=?$ 
\stopexercise

\beginsolution[one]
\startsolution
Answer: 2. 
\stopsolution
\endsolution

\subsection[subsec:2]{problem section 2}

\startexercise
Multiplication problem: $1\times1=?$
\stopexercise

\beginsolution[two]
\startsolution
Answer: 1.
\stopsolution
\endsolution

\page

%\reset[solution]

\section{Answers to the Problems}

\subsection{Answers to \in{section}[subsec:1]}

\useblocks[solution][one]

\subsection{Answers to \in{section}[subsec:2]}

\useblocks[solution][two]

\stoptext


> On Jan 31, 2019, at 5:02 PM, Wolfgang Schuster <wolfgang.schuster.lists at gmail.com> wrote:
> 
> Hi Sanjoy,
> 
> when you have a equal number of questions ans answers you can create a enumeration for each and collect the answers with the block mechanism.
> 
> At the end of the document you can flush the collected answers.
> 
> \defineblock [answer]
> 
> \defineenumeration [question]
> \defineenumeration [answer]
> 
> \starttext
> 
> \startquestion
> Question 1
> \stopquestion
> 
> \beginanswer
> \startanswer
> Answer 1
> \stopanswer
> \endanswer
> 
> \startquestion
> Question 2
> \stopquestion
> 
> \beginanswer
> \startanswer
> Answer 2
> \stopanswer
> \endanswer
> 
> \page
> 
> \useblocks[answer]
> 
> \stoptext
> 
> Wolfgang
> 
> 
> Sanjoy Mahajan schrieb am 31.01.19 um 22:51:
>> Dear List,
>> I'm trying to have problem solutions automatically collected, each with
>> its corresponding problem number, e.g. 2.3, and then typeset at the end
>> of a document.  Thus, I imagine something like
>> \startproblem{A problem title}  % Problem 1.1
>> A question
>> \startsolution
>> An answer
>> \stopsolution
>> \stopproblem
>> And then the solution(s) would appear at the end (with the "1.1"
>> remembered from when the problem was typeset):
>>   Solution to Problem 1.1.
>>   An answer
>> It seems that buffers are the way to do this.  But I cannot quite get
>> the automatic numbering to work.  The delayed evaluation and information
>> passing between lua and ConTeXt has me confused and tangled in a knot.
>> Below is a minimal almost-working example.  The issue is in the line
>>       {\ctxlua{userdata.addTempToBuffer('#1', [==[\getnumber[problem]]==])}}
>> which ends up putting the verbatim string "\getnumber[problem]" into the
>> temp buffer, rather than the typeset result, which would be something
>> like 1.1 or 1.2.
>> I've read the cld-mkiv.pdf manual, but I must be missing a simple
>> solution to getting ConTeXt to send back the result of
>> \getnumber[problem].  I've also tried the analogous
>> context.getnumber({"problem"}), though without success.
>> Does anyone see what I am missing?
>> Many thanks!
>> -Sanjoy
>> \defineenumeration[problem]
>>                   [title=yes, text=Problem, way=bychapter, prefix=chapter]
>> \defineenumeration[solution][problem]
>> \setupenumeration [solution][text={Solution to problem}, number=no]
>> \def\test#1{[#1]}
>> % appendbuffer modified from Sietse Brouwer's on mailing list (Jan 15, 2013)
>> \startluacode
>>     userdata = userdata or { }
>>     function userdata.addTempToBuffer(buffername, problem)
>>         buffers.append(
>>             buffername,
>>             '\\startsolution{' .. problem .. '}\n' .. buffers.getcontent('APPENDTEMP') .. '\n\\stopsolution\n\n'
>>         )
>>     end
>> \stopluacode
>> \def\startappendbuffer[#1]%
>>   {\def\stopappendbuffer%
>>       {\ctxlua{userdata.addTempToBuffer('#1', [==[\getnumber[problem]]==])}}
>>    \dostartbuffer[APPENDTEMP][startappendbuffer][stopappendbuffer]}
>> \starttext
>> \chapter{One}
>> \startproblem{Test}
>>   Test problem
>> \startappendbuffer[soln]
>> Solution
>> \stopappendbuffer
>> \stopproblem
>> \startproblem{Test}
>> Test problem
>> \startappendbuffer[soln]
>> Solution
>> \stopappendbuffer
>> \stopproblem
>> \typebuffer[soln]
>> \stoptext
>> ___________________________________________________________________________________
>> 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
>> ___________________________________________________________________________________
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________



More information about the ntg-context mailing list