ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Evaluating a Lua expression at the end
@ 2015-04-13 11:53 Kumar Appaiah
  2015-04-13 12:05 ` Norbert Melzer
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Kumar Appaiah @ 2015-04-13 11:53 UTC (permalink / raw)
  To: ntg-context

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 11:53 Evaluating a Lua expression at the end Kumar Appaiah
@ 2015-04-13 12:05 ` Norbert Melzer
  2015-04-13 12:06   ` Norbert Melzer
  2015-04-13 12:26 ` Procházka Lukáš Ing. - Pontex s. r. o.
  2015-04-13 13:32 ` Hans Hagen
  2 siblings, 1 reply; 8+ messages in thread
From: Norbert Melzer @ 2015-04-13 12:05 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1294 bytes --]

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.kumar@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
>
> ___________________________________________________________________________________

[-- Attachment #1.2: Type: text/html, Size: 2040 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 12:05 ` Norbert Melzer
@ 2015-04-13 12:06   ` Norbert Melzer
  0 siblings, 0 replies; 8+ messages in thread
From: Norbert Melzer @ 2015-04-13 12:06 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1427 bytes --]

Sorry, wrong recipient...
Am 13.04.2015 14:05 schrieb "Norbert Melzer" <timmelzer@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.kumar@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
>>
>> ___________________________________________________________________________________
>
>

[-- Attachment #1.2: Type: text/html, Size: 2428 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 11:53 Evaluating a Lua expression at the end Kumar Appaiah
  2015-04-13 12:05 ` Norbert Melzer
@ 2015-04-13 12:26 ` Procházka Lukáš Ing. - Pontex s. r. o.
  2015-04-13 13:48   ` Kumar Appaiah
  2015-04-13 13:32 ` Hans Hagen
  2 siblings, 1 reply; 8+ messages in thread
From: Procházka Lukáš Ing. - Pontex s. r. o. @ 2015-04-13 12:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 1634 bytes --]

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.kumar@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:LPr@pontex.cz
Pontex s. r. o.      | mailto:pontex@pontex.cz | http://www.pontex.cz
Bezová 1658
147 14 Praha 4

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

[-- Attachment #2: Count.cld --]
[-- Type: application/octet-stream, Size: 176 bytes --]

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)

[-- Attachment #3: Count.pdf --]
[-- Type: application/pdf, Size: 4752 bytes --]

[-- Attachment #4: Count.mkiv --]
[-- Type: application/octet-stream, Size: 184 bytes --]

\startluacode
  n = 0
\stopluacode

\starttext
  A
  \directlua{n = n + 1}
  B
  \directlua{n = n + 1}
  C
  \directlua{n = n + 1}
  \directlua{print("!!!", n)}
\stoptext

[-- Attachment #5: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 11:53 Evaluating a Lua expression at the end Kumar Appaiah
  2015-04-13 12:05 ` Norbert Melzer
  2015-04-13 12:26 ` Procházka Lukáš Ing. - Pontex s. r. o.
@ 2015-04-13 13:32 ` Hans Hagen
  2015-04-13 13:50   ` Kumar Appaiah
  2 siblings, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2015-04-13 13:32 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 12:26 ` Procházka Lukáš Ing. - Pontex s. r. o.
@ 2015-04-13 13:48   ` Kumar Appaiah
  2015-04-13 14:48     ` Procházka Lukáš Ing. - Pontex s. r. o.
  0 siblings, 1 reply; 8+ messages in thread
From: Kumar Appaiah @ 2015-04-13 13:48 UTC (permalink / raw)
  To: ntg-context

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 13:32 ` Hans Hagen
@ 2015-04-13 13:50   ` Kumar Appaiah
  0 siblings, 0 replies; 8+ messages in thread
From: Kumar Appaiah @ 2015-04-13 13:50 UTC (permalink / raw)
  To: ntg-context

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluating a Lua expression at the end
  2015-04-13 13:48   ` Kumar Appaiah
@ 2015-04-13 14:48     ` Procházka Lukáš Ing. - Pontex s. r. o.
  0 siblings, 0 replies; 8+ messages in thread
From: Procházka Lukáš Ing. - Pontex s. r. o. @ 2015-04-13 14:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 1569 bytes --]

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:LPr@pontex.cz
Pontex s. r. o.      | mailto:pontex@pontex.cz | http://www.pontex.cz
Bezová 1658
147 14 Praha 4

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

[-- Attachment #2: Count2.cld --]
[-- Type: application/octet-stream, Size: 436 bytes --]

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

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])
C.stoptext()

[-- Attachment #3: Count2.pdf --]
[-- Type: application/pdf, Size: 6563 bytes --]

[-- Attachment #4: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-04-13 14:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-13 11:53 Evaluating a Lua expression at the end Kumar Appaiah
2015-04-13 12:05 ` Norbert Melzer
2015-04-13 12:06   ` Norbert Melzer
2015-04-13 12:26 ` Procházka Lukáš Ing. - Pontex s. r. o.
2015-04-13 13:48   ` Kumar Appaiah
2015-04-13 14:48     ` Procházka Lukáš Ing. - Pontex s. r. o.
2015-04-13 13:32 ` Hans Hagen
2015-04-13 13:50   ` Kumar Appaiah

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).