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

Sanjoy Mahajan sanjoy at olin.edu
Thu Jan 31 22:51:20 CET 2019

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                

An answer


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!


                  [title=yes, text=Problem, way=bychapter, prefix=chapter]
\setupenumeration [solution][text={Solution to problem}, number=no]


% appendbuffer modified from Sietse Brouwer's on mailing list (Jan 15, 2013)
    userdata = userdata or { }
    function userdata.addTempToBuffer(buffername, problem)
            '\\startsolution{' .. problem .. '}\n' .. buffers.getcontent('APPENDTEMP') .. '\n\\stopsolution\n\n'

      {\ctxlua{userdata.addTempToBuffer('#1', [==[\getnumber[problem]]==])}}



  Test problem

Test problem



More information about the ntg-context mailing list