[NTG-context] Evaluating a lua expression at the end (again)

2017-02-09 Thread Kumar Appaiah
Dear ConTeXt users,

Last time, this is what I wanted, and Hans provided me an example:

https://www.mail-archive.com/ntg-context@ntg.nl/msg78079.html

However, the example given in that e-mail does not work with my Debian
context 2016.05.17.20160523-1. Could you please help me out again? I
get:

/usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: attempt to
index upvalue 'tobesavedmacros' (a nil value)

Thanks.

Kumar
-- 
Kumar Appaiah
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@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
___

Re: [NTG-context] Evaluating a Lua expression at the end (repeat)

2016-08-25 Thread Kumar Appaiah
On Thu, Aug 25, 2016 at 5:49 PM, Henri Menke  wrote:
> Hi Kumar,
>
> the function job.variables.save seems to save the key in 
> job.variables.collected.macros instead of job.variables.collected.  The 
> problem is that macros is not created unconditionally, which is why you might 
> run into a »attempt to index a nil value« error without some manual error 
> checking.  Therefore I recommend assigning the temp value to 
> job.variables.tobesaved["document:temp:"..name].  Perhaps Hans joins this 
> discussion and schools me about why this could be a bad idea, but so far it 
> works.
>
> Also, I'd recommend making all your user level macros protected and check for 
> the optional argument in \startwhatever (otherwise you'll get errors like 
> »Macro doesn't match its definition«).  Because I'm really cautious I also 
> use \luaescapestring{#1} (Try for example »foo"bar« as a value without).




Indeed, this worked! I'll try to understand the internals myself to
know what is going on exactly. Thanks for the solution.

Kumar
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end (repeat)

2016-08-25 Thread Henri Menke
Hi Kumar,

the function job.variables.save seems to save the key in 
job.variables.collected.macros instead of job.variables.collected.  The problem 
is that macros is not created unconditionally, which is why you might run into 
a »attempt to index a nil value« error without some manual error checking.  
Therefore I recommend assigning the temp value to 
job.variables.tobesaved["document:temp:"..name].  Perhaps Hans joins this 
discussion and schools me about why this could be a bad idea, but so far it 
works.

Also, I'd recommend making all your user level macros protected and check for 
the optional argument in \startwhatever (otherwise you'll get errors like 
»Macro doesn't match its definition«).  Because I'm really cautious I also use 
\luaescapestring{#1} (Try for example »foo"bar« as a value without).

\starttext

\startluacode
local name = nil
local temp = 0

function document.startwhatever(s)
   name = s
   temp = 0
end

function document.addwhatever(n)
   temp = temp + n
   context(n)
end

function document.stopwhatever()
   job.variables.tobesaved["document:temp:"..name] = temp
end

function document.getwhatever(s)
   context(job.variables.collected["document:temp:"..s])
end
\stopluacode

\define\startwhatever{\dosingleempty\dostartwhatever}
\def\dostartwhatever[#1]{\ctxlua{document.startwhatever("\luaescapestring{#1}")}}
\define   \stopwhatever {\ctxlua{document.stopwhatever()}}
\define[1]\addwhatever  {\ctxlua{document.addwhatever(\luaescapestring{#1})}}
\define[1]\getwhatever  {\ctxlua{document.getwhatever("\luaescapestring{#1}")}}

total: \getwhatever{foo}

\startwhatever[foo]

test 1: \addwhatever{10}\par
test 2: \addwhatever{20}\par
test 3: \addwhatever{30}\par

\stopwhatever

\stoptext

On 08/25/2016 01:28 PM, Kumar Appaiah wrote:
> Dear List,
> 
> Last year, Hans helped me with this example:
> 
> 
> \starttext
> 
> \startluacode
>  local name = nil
>   local temp = 0
> 
>  function document.startwhatever(s)
>name = s
>temp = 0
> end
>  function document.addwhatever(n)
>   temp = temp + n
>context(n)
> end
>  function
>   document.stopwhatever()
>job.variables.save("document:temp:"..name,temp)
> end
>  function document.getwhatever(s)
>   context(job.variables.collected["document:temp:"..s])
>end
>\stopluacode
> 
> \def\startwhatever[#1]{\ctxlua{document.startwhatever("#1")}}
> \def\stopwhatever {\ctxlua{document.stopwhatever()}}
> \def\addwhatever#1{\ctxlua{document.addwhatever(#1)}}
> \def\getwhatever#1{\ctxlua{document.getwhatever("#1")}}
> 
> total: \getwhatever{foo}
> 
> \startwhatever[foo]
> 
> test 1: \addwhatever{10}\par
> test 2: \addwhatever{20}\par
> test 3: \addwhatever{30}\par
> 
> \stopwhatever
> 
> \stoptext
> 
> However, on my current ConTeXt on Debian (2016.05.17.20160523-1), I am
> unable to get it working. It says:
> 
> lua error   > lua error on line 37 in file /tmp/test.tex:
> 
> /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: attempt to
> index upvalue 'tobesavedmacros' (a nil value)
> stack traceback:
> /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: in
>   function 'save'
>   [ctxlua]:14: in function 'stopwhatever'
>   [ctxlua]:1: in main chunk
> 
> 27 \def\getwhatever#1{\ctxlua{document.getwhatever("#1")}}
> 28
> 29 total: \getwhatever{foo}
> 30
> 31 \startwhatever[foo]
> 32
> 33 test 1: \addwhatever{10}\par
> 34 test 2: \addwhatever{20}\par
> 35 test 3: \addwhatever{30}\par
> 36
> 37 >>  \stopwhatever
> 38
> 39 \stoptext
> 40
> 41
> 
> Could you please suggest a workaround?
> 
> Thanks.
> 
> Kumar
> ___
> If your question is of interest to others as well, please add an entry to the 
> Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki : http://contextgarden.net
> ___
> 

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

[NTG-context] Evaluating a Lua expression at the end (repeat)

2016-08-25 Thread Kumar Appaiah
Dear List,

Last year, Hans helped me with this example:


\starttext

\startluacode
 local name = nil
  local temp = 0

 function document.startwhatever(s)
   name = s
   temp = 0
end
 function document.addwhatever(n)
  temp = temp + n
   context(n)
end
 function
  document.stopwhatever()
   job.variables.save("document:temp:"..name,temp)
end
 function document.getwhatever(s)
  context(job.variables.collected["document:temp:"..s])
   end
   \stopluacode

\def\startwhatever[#1]{\ctxlua{document.startwhatever("#1")}}
\def\stopwhatever {\ctxlua{document.stopwhatever()}}
\def\addwhatever#1{\ctxlua{document.addwhatever(#1)}}
\def\getwhatever#1{\ctxlua{document.getwhatever("#1")}}

total: \getwhatever{foo}

\startwhatever[foo]

test 1: \addwhatever{10}\par
test 2: \addwhatever{20}\par
test 3: \addwhatever{30}\par

\stopwhatever

\stoptext

However, on my current ConTeXt on Debian (2016.05.17.20160523-1), I am
unable to get it working. It says:

lua error   > lua error on line 37 in file /tmp/test.tex:

/usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: attempt to
index upvalue 'tobesavedmacros' (a nil value)
stack traceback:
/usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: in
function 'save'
[ctxlua]:14: in function 'stopwhatever'
[ctxlua]:1: in main chunk

27 \def\getwhatever#1{\ctxlua{document.getwhatever("#1")}}
28
29 total: \getwhatever{foo}
30
31 \startwhatever[foo]
32
33 test 1: \addwhatever{10}\par
34 test 2: \addwhatever{20}\par
35 test 3: \addwhatever{30}\par
36
37 >>  \stopwhatever
38
39 \stoptext
40
41

Could you please suggest a workaround?

Thanks.

Kumar
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

[NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Kumar Appaiah
Hi.

I am trying to set up a system where I can use Lua to find a total
that I can display at the end. For example, if I set a question paper,
after each question, I do a \directlua{total = total + 4}, assuming
this question has 4 points. At the top of my document, I now want to
say Total points: and display the value of total.

Now, naturally, using \directlua gives me 0, since total is 0 at the
beginning. But \latelua didn't seem to be what I am looking for. Could
someone please guide me in the right direction?

Thanks.

Kumar
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Norbert Melzer
Sorry, wrong recipient...
Am 13.04.2015 14:05 schrieb Norbert Melzer timmel...@gmail.com:

 If it is possible I'd like to wait for the idris book and get that one. If
 that's not possible I'm interested in http://www.manning.com/blackheath/
 Am 13.04.2015 13:54 schrieb Kumar Appaiah a.ku...@alumni.iitm.ac.in:

 Hi.

 I am trying to set up a system where I can use Lua to find a total
 that I can display at the end. For example, if I set a question paper,
 after each question, I do a \directlua{total = total + 4}, assuming
 this question has 4 points. At the top of my document, I now want to
 say Total points: and display the value of total.

 Now, naturally, using \directlua gives me 0, since total is 0 at the
 beginning. But \latelua didn't seem to be what I am looking for. Could
 someone please guide me in the right direction?

 Thanks.

 Kumar

 ___
 If your question is of interest to others as well, please add an entry to
 the Wiki!

 maillist : ntg-context@ntg.nl /
 http://www.ntg.nl/mailman/listinfo/ntg-context
 webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
 archive  : http://foundry.supelec.fr/projects/contextrev/
 wiki : http://contextgarden.net

 ___


___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Norbert Melzer
If it is possible I'd like to wait for the idris book and get that one. If
that's not possible I'm interested in http://www.manning.com/blackheath/
Am 13.04.2015 13:54 schrieb Kumar Appaiah a.ku...@alumni.iitm.ac.in:

 Hi.

 I am trying to set up a system where I can use Lua to find a total
 that I can display at the end. For example, if I set a question paper,
 after each question, I do a \directlua{total = total + 4}, assuming
 this question has 4 points. At the top of my document, I now want to
 say Total points: and display the value of total.

 Now, naturally, using \directlua gives me 0, since total is 0 at the
 beginning. But \latelua didn't seem to be what I am looking for. Could
 someone please guide me in the right direction?

 Thanks.

 Kumar

 ___
 If your question is of interest to others as well, please add an entry to
 the Wiki!

 maillist : ntg-context@ntg.nl /
 http://www.ntg.nl/mailman/listinfo/ntg-context
 webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
 archive  : http://foundry.supelec.fr/projects/contextrev/
 wiki : http://contextgarden.net

 ___
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Procházka Lukáš Ing . - Pontex s . r . o .

Hello,

another single-pass approach, which uses the fact that you know the questions and their 
point weights in advance is to form questions into a Lua table and evaluate 
the total first:


local C = context

local tab =
{ {Question 1, 10, },
  {Question 2, 20, },
  {Question 3, 30, },
}

local n = 0

for _, v in ipairs(tab) do local q, n1 = unpack(v); n = n + n1 end

-- Or: for _, v in ipairs(tab) do n = n + v[2] end

local f = function(it)
  local q, n1 = unpack(it)

  n = n + n1

  C(q .. :  .. n)
  C.par()
end

C.starttext()
  C(Total score:  .. n)
  C.par()

  n = 0 -- Reset

  f(tab[1])
  f(tab[2])
  f(tab[3])

  -- Or: for _, v in ipairs(tab) do f(v) end
C.stoptext()


Best regards,

Lukas



Thanks for the response. I am looking at something like this:

==
\starttext
\directlua{total = 0}
\title{Homework 1}

Total score: \directlua{tex.print(total);}

Question 1 (3 points)
\directlua{total = total + 3}

Question 2 (4 points)
\directlua{total = total + 4}

Question 3 (5 points)
\directlua{total = total + 5}

\stoptext
==

I expect the “Total score” to be followed by “12”, but I don't know
how to get that (or whether that is even possible).

Thanks.

Kumar



--
Ing. Lukáš Procházka | mailto:l...@pontex.cz
Pontex s. r. o.  | mailto:pon...@pontex.cz | http://www.pontex.cz
Bezová 1658
147 14 Praha 4

Tel: +420 241 096 751
Fax: +420 244 461 038

Count2.cld
Description: Binary data


Count2.pdf
Description: Adobe PDF document
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Procházka Lukáš Ing . - Pontex s . r . o .

Hello Kumar,

could you provide a (non-working) minimal example?

I tried this:


local C = context

local n = 0

C.starttext()

  C(A)
  n = n + 1
  C(B)
  n = n + 1
  C(C)
  n = n + 1

print(***, n)

C.stoptext()

print(, n)


and I got 3 (twice) - what I expected:


...
ConTeXt  ver: 2015.04.08 21:31 MKIV beta  fmt: 2015.4.9  int: english/english
...
\\Count.cld', result 'Count'
...
sandbox  call  processing as cld: 
d://Lukas/Jobs/Podebrady-Sachta.RDS/Deska.SO/Statics/t/Count.cld
*** 3

3

...


... And similar result for Count.mkiv.

On Mon, 13 Apr 2015 13:53:28 +0200, Kumar Appaiah a.ku...@alumni.iitm.ac.in 
wrote:


Hi.

I am trying to set up a system where I can use Lua to find a total
that I can display at the end. For example, if I set a question paper,
after each question, I do a \directlua{total = total + 4}, assuming
this question has 4 points. At the top of my document, I now want to
say Total points: and display the value of total.

Now, naturally, using \directlua gives me 0, since total is 0 at the
beginning. But \latelua didn't seem to be what I am looking for. Could


Where could I learn more about \latelua (I'm hearing first time now about that 
command)...

Best regards,

Lukas



someone please guide me in the right direction?

Thanks.

Kumar



--
Ing. Lukáš Procházka | mailto:l...@pontex.cz
Pontex s. r. o.  | mailto:pon...@pontex.cz | http://www.pontex.cz
Bezová 1658
147 14 Praha 4

Tel: +420 241 096 751
Fax: +420 244 461 038

Count.cld
Description: Binary data


Count.pdf
Description: Adobe PDF document


Count.mkiv
Description: Binary data
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Kumar Appaiah
On Mon, Apr 13, 2015 at 03:32:04PM +0200, Hans Hagen wrote:
 On 4/13/2015 1:53 PM, Kumar Appaiah wrote:
 Hi.
 
 I am trying to set up a system where I can use Lua to find a total
 that I can display at the end. For example, if I set a question paper,
 after each question, I do a \directlua{total = total + 4}, assuming
 this question has 4 points. At the top of my document, I now want to
 say Total points: and display the value of total.
 
 Now, naturally, using \directlua gives me 0, since total is 0 at the
 beginning. But \latelua didn't seem to be what I am looking for. Could
 someone please guide me in the right direction?
 
 forget about latelua ... by the time that kicks in typesetting is done
 already .. you need to go multipass:
 
 \starttext
 
 \startluacode
 local name = nil
 local temp = 0
 
 function document.startwhatever(s)
 name = s
 temp = 0
 end
 function document.addwhatever(n)
 temp = temp + n
 context(n)
 end
 function document.stopwhatever()
 job.variables.save(document:temp:..name,temp)
 end
 function document.getwhatever(s)
 context(job.variables.collected[document:temp:..s])
 end
 \stopluacode
 
 \def\startwhatever[#1]{\ctxlua{document.startwhatever(#1)}}
 \def\stopwhatever {\ctxlua{document.stopwhatever()}}
 \def\addwhatever#1{\ctxlua{document.addwhatever(#1)}}
 \def\getwhatever#1{\ctxlua{document.getwhatever(#1)}}
 
 total: \getwhatever{foo}
 
 \startwhatever[foo]
 
 test 1: \addwhatever{10}\par
 test 2: \addwhatever{20}\par
 test 3: \addwhatever{30}\par
 
 \stopwhatever
 
 \stoptext

Thanks Hans. This is what I needed!

Kumar
-- 
Kumar Appaiah
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Kumar Appaiah
Dear Lukáš,

On Mon, Apr 13, 2015 at 02:26:38PM +0200, Procházka Lukáš Ing. - Pontex s. r. 
o. wrote:
 Hello Kumar,
 
 could you provide a (non-working) minimal example?
 
 I tried this:
 
 
 local C = context
 
 local n = 0
 
 C.starttext()
 
   C(A)
   n = n + 1
   C(B)
   n = n + 1
   C(C)
   n = n + 1
 
 print(***, n)
 
 C.stoptext()
 
 print(, n)
 
 
 and I got 3 (twice) - what I expected:

Thanks for the response. I am looking at something like this:

==
\starttext
\directlua{total = 0}
\title{Homework 1}

Total score: \directlua{tex.print(total);}

Question 1 (3 points)
\directlua{total = total + 3}

Question 2 (4 points)
\directlua{total = total + 4}

Question 3 (5 points)
\directlua{total = total + 5}

\stoptext
==

I expect the “Total score” to be followed by “12”, but I don't know
how to get that (or whether that is even possible).

Thanks.

Kumar
-- 
Kumar Appaiah
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] Evaluating a Lua expression at the end

2015-04-13 Thread Hans Hagen

On 4/13/2015 1:53 PM, Kumar Appaiah wrote:

Hi.

I am trying to set up a system where I can use Lua to find a total
that I can display at the end. For example, if I set a question paper,
after each question, I do a \directlua{total = total + 4}, assuming
this question has 4 points. At the top of my document, I now want to
say Total points: and display the value of total.

Now, naturally, using \directlua gives me 0, since total is 0 at the
beginning. But \latelua didn't seem to be what I am looking for. Could
someone please guide me in the right direction?


forget about latelua ... by the time that kicks in typesetting is done 
already .. you need to go multipass:


\starttext

\startluacode
local name = nil
local temp = 0

function document.startwhatever(s)
name = s
temp = 0
end
function document.addwhatever(n)
temp = temp + n
context(n)
end
function document.stopwhatever()
job.variables.save(document:temp:..name,temp)
end
function document.getwhatever(s)
context(job.variables.collected[document:temp:..s])
end
\stopluacode

\def\startwhatever[#1]{\ctxlua{document.startwhatever(#1)}}
\def\stopwhatever {\ctxlua{document.stopwhatever()}}
\def\addwhatever#1{\ctxlua{document.addwhatever(#1)}}
\def\getwhatever#1{\ctxlua{document.getwhatever(#1)}}

total: \getwhatever{foo}

\startwhatever[foo]

test 1: \addwhatever{10}\par
test 2: \addwhatever{20}\par
test 3: \addwhatever{30}\par

\stopwhatever

\stoptext



-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___