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 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"
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
___________________________________________________________________________________
Sorry, wrong recipient...
Am 13.04.2015 14:05 schrieb "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"
: 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
___________________________________________________________________________________
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
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
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
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
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 -----------------------------------------------------------------
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
participants (5)
-
Hans Hagen
-
Kumar Appaiah
-
Kumar Appaiah
-
Norbert Melzer
-
Procházka Lukáš Ing. - Pontex s. r. o.