Maybe similar to the "spreadsheet" question: I'm trying to use Wolfgang's letter module for my invoices and would like to use Lua for the sums. But I fail already at the beginning: \startluacode userdata = userdata or {} userdata.invoice = userdata.invoice or { sum = 0 } -- global table for sums function userdata.singlesum(hours, perhour) amount = hours * perhour userdata.invoice.sum = userdata.invoice.sum + amount global.context(string.gsub(string.format("\%.2f", amount), "%.", ",")) return amount end \stopluacode gives: (virtual://viafile.1 unknown preamble key [the character )] unknown preamble key [the character )] unknown preamble key [the character )] ! LuaTeX error <main ctx instance>:1: '=' expected near '<eof>'. system > tex > error on line 11 in file invoice.tex: LuaTeX error ... 4 \startluacode 5 userdata = userdata or {} 6 7 userdata.invoice = userdata.invoice or { sum = 0 } 8 9 function userdata.singlesum(hours, perhour) 10 amount = hours * perhour 11 >> userdata.invoice.sum = userdata.invoice.sum + amount 12 global.context(string.gsub(string.format("\%.2f", amount), "%.", ",")) 13 return amount 14 end I can't see what's wrong!? I tried local and usercode, but didn’t get further. Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
Am 2011-02-20 um 12:55 schrieb Henning Hraban Ramm:
But I fail already at the beginning:
\startluacode
Sorry for the noise, the error was in the call, not in the definition. Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
Am 20.02.2011 um 12:55 schrieb Henning Hraban Ramm:
Maybe similar to the "spreadsheet" question: I'm trying to use Wolfgang's letter module for my invoices and would like to use Lua for the sums. But I fail already at the beginning:
\startluacode userdata = userdata or {}
userdata.invoice = userdata.invoice or { sum = 0 } -- global table for sums
function userdata.singlesum(hours, perhour) amount = hours * perhour userdata.invoice.sum = userdata.invoice.sum + amount global.context(string.gsub(string.format("\%.2f", amount), "%.", ",")) return amount end
\stopluacode
\startluacode userdata = userdata or {} userdata.invoice = userdata.invoice or { sum = 0 } -- global table for sums function userdata.singlesum(hours, perhour) local amount = hours * perhour userdata.invoice.sum = userdata.invoice.sum + amount context(string.gsub(string.format("%.2f",amount),"%.",",")) end \stopluacode \starttext \ctxlua{userdata.singlesum(3,4)} \stoptext Wolfgang
Am 2011-02-20 um 13:08 schrieb Wolfgang Schuster:
local amount = hours * perhour
Thank you - I guess you meant that "local". I tried that, but since my error was in the call... anyway. Please consider the following nearly-minimal example: \starttext \startluacode userdata = userdata or {} userdata.invoice = { sum = 0, hours = 0, perhour = 100 } function userdata.numberformat(amount) context(string.gsub(string.format("\%.2f", amount), "%.", ",")) end function userdata.InvoiceLine(text, hours) context.NC() context(text) context.NC() userdata.numberformat(hours) context("\\,h") context.NC() context.NC() userdata.numberformat(hours * userdata.invoice.perhour) context("\\,€") context.NC() context.NR() userdata.invoice.sum = userdata.invoice.sum + hours * userdata.invoice.perhour userdata.invoice.hours = userdata.invoice.hours + hours end function userdata.InvoiceSumLine(text) context.HL() context.NC() context(text) context.NC() userdata.numberformat(userdata.invoice.hours) context("\\,h") context.NC() userdata.numberformat(userdata.invoice.perhour) context("\\,€/h") context.NC() userdata.numberformat(userdata.invoice.sum) context("\\,€") context.NC() context.NR() end \stopluacode \starttabulate[|lw(8cm)|rg(,)w(2cm)|rg(,)w(2cm)|rg(,)w(3cm)|] %\ctxlua{userdata.InvoiceLine("Play with \\LUATEX", 2.5)} \ctxlua{userdata.InvoiceLine("Do some \\CONTEXT", 0.5)} \ctxlua{userdata.InvoiceSumLine("Sum")} \stoptabulate \stoptext That seems to work so far, but invoice.hours and invoice.sum are always doubled! In my letter setup, it’s even quadrupled! I guess my functions gets called several times, but how can I avoid that? Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
Am 20.02.2011 um 13:58 schrieb Henning Hraban Ramm:
\starttabulate[|lw(8cm)|rg(,)w(2cm)|rg(,)w(2cm)|rg(,)w(3cm)|] %\ctxlua{userdata.InvoiceLine("Play with \\LUATEX", 2.5)} \ctxlua{userdata.InvoiceLine("Do some \\CONTEXT", 0.5)} \ctxlua{userdata.InvoiceSumLine("Sum")} \stoptabulate
\startluacode context.starttabulate({"|lw(8cm)|rg(,)w(2cm)|rg(,)w(2cm)|rg(,)w(3cm)|"}) userdata.InvoiceLine("Do some \\CONTEXT", 0.5) userdata.InvoiceSumLine("Sum") context.stoptabulate() \stopluacode
That seems to work so far, but invoice.hours and invoice.sum are always doubled! In my letter setup, it’s even quadrupled! I guess my functions gets called several times, but how can I avoid that?
The content of tabulate is processed twice to get the information for “p” columns and you get therefore the wrong values in the last row. Wolfgang
Am 2011-02-20 um 13:58 schrieb Henning Hraban Ramm:
That seems to work so far, but invoice.hours and invoice.sum are always doubled! In my letter setup, it’s even quadrupled! I guess my functions gets called several times, but how can I avoid that?
I found a better approach, but with the letter module involved, my "RegisterItem" is called twice! \starttext \usemodule[letter] \startluacode userdata = userdata or {} userdata.invoice = { amount = 0, hours = 0, perhour = 100, items = {} } function userdata.numberformat(amount) context(string.gsub(string.format("\%.2f", amount), "%.", ",")) end function userdata.InvoiceLine(text, hours) context.NC() context(text) context.NC() userdata.numberformat(hours) context("\\,h") context.NC() context.NC() userdata.numberformat(hours * userdata.invoice.perhour) context("\\,€") context.NC() context.NR() end function userdata.InvoiceSumLine(text) context.HL() context.NC() context(text) context.NC() userdata.numberformat(userdata.invoice.hours) context("\\,h") context.NC() context("à ") userdata.numberformat(userdata.invoice.perhour) context("\\,€/h") context.NC() userdata.numberformat(userdata.invoice.amount) context("\\,€") context.NC() context.NR() end function userdata.RegisterItem(text, hours) for no, item in ipairs(userdata.invoice.items) do -- we need to check for double registering due to some call logic of ConTeXt and the letter module if item.text == text then --return -- commented to show double calling end end table.insert(userdata.invoice.items, {text=text, hours=hours}) end function userdata.Invoice() local amountsum, hoursum = 0,0 context("\\starttabulate[|lw(8cm)|rg(,)w(2cm)|rg(,)w(2cm)| rg(,)w(3cm)|]") for no, item in ipairs(userdata.invoice.items) do userdata.InvoiceLine(item.text, item.hours) hoursum = hoursum + item.hours amountsum = amountsum + item.hours * userdata.invoice.perhour end userdata.invoice.hours = hoursum userdata.invoice.amount = amountsum userdata.InvoiceSumLine("Sum") context.stoptabulate() end \stopluacode \startletter[subject={INVOICE}] \startluacode userdata.RegisterItem("Do some \\CONTEXT ", 0.5) userdata.RegisterItem("Do some \\LUATEX ", 1.5) userdata.Invoice() \stopluacode \stopletter \stoptext Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
Am 20.02.2011 um 14:29 schrieb Henning Hraban Ramm:
I found a better approach, but with the letter module involved, my "RegisterItem" is called twice!
The letter modules does trialtypesetting of the content to get the content of the \cc, \encl and \ps commands. To prevent that certain commands and environments are preprocessed you can wrap them in \iftrialtypesetting \else ... \fi or \unless\iftrialtypesetting ... \fi @Hans: Is it possible to write the above like this \startnotmode[trialtypesetting] ... \stopnotmode Wolfgang
Am 2011-02-20 um 14:41 schrieb Wolfgang Schuster:
Am 20.02.2011 um 14:29 schrieb Henning Hraban Ramm:
I found a better approach, but with the letter module involved, my "RegisterItem" is called twice!
The letter modules does trialtypesetting of the content to get the content of the \cc, \encl and \ps commands. To prevent that certain commands and environments are preprocessed you can wrap them in
\iftrialtypesetting \else ... \fi
or
\unless\iftrialtypesetting ... \fi
Thank you, but how does that map to Lua? I couldn't find "trialtypesetting" in the LuaTeX manual. I tried "if context.trialtypesetting() then return end" but get only "undefined control sequence". """ context.iftrialtypesetting() do return end context.fi() """ can't work, of course ("Incomplete \iffalse") Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
On 20-2-2011 3:49, Henning Hraban Ramm wrote:
"trialtypesetting" in the LuaTeX manual.
things like this are not part of luatex but of context so you need the cld manual (or wait till more documentation shows up, or ask ..)
context.iftrialtypesetting() do return end context.fi() """ can't work, of course ("Incomplete \iffalse")
please not that kind of horrible misuse of the context.* namespace as you don't want to end up with expandafter's at that end Hans ----------------------------------------------------------------- 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 20-2-2011 2:41, Wolfgang Schuster wrote:
Am 20.02.2011 um 14:29 schrieb Henning Hraban Ramm:
I found a better approach, but with the letter module involved, my "RegisterItem" is called twice!
The letter modules does trialtypesetting of the content to get the content of the \cc, \encl and \ps commands. To prevent that certain commands and environments are preprocessed you can wrap them in
\iftrialtypesetting \else .... \fi
or
\unless\iftrialtypesetting .... \fi
@Hans: Is it possible to write the above like this
\startnotmode[trialtypesetting] .... \stopnotmode
it's a system mode, so \startnotmode[*trialtypesetting] ... and in lua if tex.systemmodes.trialtypesetting then ... but, you need to use \settrialtypesetting (see syst-aux.mkiv) as there is more than the \if now (too late to get rid of the if) Hans ----------------------------------------------------------------- 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 -----------------------------------------------------------------
Am 2011-02-20 um 16:09 schrieb Hans Hagen:
it's a system mode, so
\startnotmode[*trialtypesetting] ...
and in lua
if tex.systemmodes.trialtypesetting then ...
but, you need to use \settrialtypesetting (see syst-aux.mkiv) as there is more than the \if now (too late to get rid of the if)
You mean, Wolfgang must set it in his module? I guess he did. At least this works as expected: function userdata.RegisterItem(text, hours) if tex.systemmodes.trialtypesetting then return end table.insert(userdata.invoice.items, {text=text, hours=hours}) end Thank you! Am 2011-02-20 um 16:12 schrieb Hans Hagen:
On 20-2-2011 3:49, Henning Hraban Ramm wrote:
"trialtypesetting" in the LuaTeX manual.
things like this are not part of luatex but of context so you need the cld manual (or wait till more documentation shows up, or ask ..)
Ah, I guess the CLD manual was what I was looking for all the time ;-) I suggest you mention tex.systemmodes.trialtypesetting in section 3.3. "trial typesetting" - the "return true" approach didn't help in my case (or I didn't understand it). I'll wikify my invoice sample soon. LuaTeX docs in the wiki are a bit scattered ATM... (e.g. Lua, LuaTeX, CLD and MkIV all explain LuaTeX basics) Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
Hi Hraban! On 2011-02-20 <20:16:03>, Henning Hraban Ramm wrote: < … />
I'll wikify my invoice sample soon. LuaTeX docs in the wiki are a bit scattered ATM... (e.g. Lua, LuaTeX, CLD and MkIV all explain LuaTeX basics)
“Lua” is intended to be some kind of portal for all topics related to Lua programming in Luatex. As opposed to “Luatex” which should have a focus on typesetting-specific stuff, new primitives and everything else revolutionary that Luatex offers. “CLD” is *very* specific and basically just a demonstration for what a wiki reader might expect in the manual. “MkIV” seems outdated but I have no idea what content should go there. (Btw. there’s also a page “programming in luatex”…) Personally I would prefer that the distinction between the categories typesetting and Lua scripting be emphasized, e.g. that info on using the token, node or tex libs goes into the former (or the Luatex wiki…) whereas Lua-based approaches to stuff that used to be hacked in Tex macros would go into the latter. Does this make sense? Anyways, looking forward to see your invoice code Philipp
Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
___________________________________________________________________________________ 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 ___________________________________________________________________________________
Am 20.02.2011 um 21:20 schrieb Philipp Gesang:
Anyways, looking forward to see your invoice code
It would be more interesting to have something like luacalc [1] available in context. As it is only another kind of table one could insert the data with a syntax like this \startspreadsheet \startrow \startcell 3 \stopcell \startcell 5 \stopcell \startcell = A1 + A3 \stopcell \stoprow \stopspreadsheet and the final result is printed with a natural table. [1] http://luacalc.sourceforge.net/ Wolfgang
On 20-2-2011 9:28, Wolfgang Schuster wrote:
Am 20.02.2011 um 21:20 schrieb Philipp Gesang:
Anyways, looking forward to see your invoice code
It would be more interesting to have something like luacalc [1] available in context. As it is only another kind of table one could insert the data with a syntax like this
\startspreadsheet \startrow \startcell 3 \stopcell \startcell 5 \stopcell \startcell = A1 + A3 \stopcell \stoprow \stopspreadsheet
and the final result is printed with a natural table.
See attached file. For this to work ok we need a reset hook into TABLE but it shows the principle. I think that we should stick close to lua so it's mostly a matter of tex interfacing. I can even imagine named sheets so that results can be recalled later on. A nice distraction from more complex coding as this is rather simple so I'll think a bit about it. Hans ----------------------------------------------------------------- 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 -----------------------------------------------------------------
Am 2011-02-20 um 21:20 schrieb Philipp Gesang:
Anyways, looking forward to see your invoice code
Here you are: http://wiki.contextgarden.net/Calculations_in_Lua It’s just in a "works for me" state, and the page can go away as soon as the spreadsheet module goes live. Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
On Tue, Feb 22, 2011 at 10:47 AM, Henning Hraban Ramm
Am 2011-02-20 um 21:20 schrieb Philipp Gesang:
Anyways, looking forward to see your invoice code
Here you are: http://wiki.contextgarden.net/Calculations_in_Lua
It’s just in a "works for me" state, and the page can go away as soon as the spreadsheet module goes live. Can you add a small example ?
-- luigi
Am 22.02.2011 um 10:51 schrieb luigi scarso:
On Tue, Feb 22, 2011 at 10:47 AM, Henning Hraban Ramm
wrote: Am 2011-02-20 um 21:20 schrieb Philipp Gesang:
Anyways, looking forward to see your invoice code
Here you are: http://wiki.contextgarden.net/Calculations_in_Lua
It’s just in a "works for me" state, and the page can go away as soon as the spreadsheet module goes live. Can you add a small example ?
The spreadsheet module has a example at the end of the file and Hennings code has also a example at the end. Wolfgang
On Tue, Feb 22, 2011 at 11:01 AM, Wolfgang Schuster
Am 22.02.2011 um 10:51 schrieb luigi scarso:
On Tue, Feb 22, 2011 at 10:47 AM, Henning Hraban Ramm
wrote: Am 2011-02-20 um 21:20 schrieb Philipp Gesang:
Anyways, looking forward to see your invoice code
Here you are: http://wiki.contextgarden.net/Calculations_in_Lua
It’s just in a "works for me" state, and the page can go away as soon as the spreadsheet module goes live. Can you add a small example ?
The spreadsheet module has a example at the end of the file and Hennings code has also a example at the end.
Yes I've seen -- register invoice items userdata.RegisterAmountItem("Some introductional text", 0) -- just text userdata.RegisterTimeItem("Learn \\LUATEX", 2.5) userdata.RegisterTimeItem("Do some calculations in \\CONTEXT", 3.5 + 7) userdata.RegisterAmountItem("Donations to Open Source projects", 99) -- lump sum -- output userdata.Invoice() But it' s better \startluacode -- functions \stopluacode %% \starttext \startluacode -- register invoice items userdata.RegisterAmountItem("Some introductional text", 0) -- just text userdata.RegisterTimeItem("Learn \\LUATEX", 2.5) userdata.RegisterTimeItem("Do some calculations in \\CONTEXT", 3.5 + 7) userdata.RegisterAmountItem("Donations to Open Source projects", 99) -- lump sum -- output userdata.Invoice() \stopluacode \stoptext (or maybe a better example) -- luigi
Am 2011-02-22 um 11:14 schrieb luigi scarso: >>> Can you add a small example ? >> >> The spreadsheet module has a example at the end of the file and >> Hennings code >> has also a example at the end. > Yes I've seen ... > But it' s better > \startluacode > -- functions > \stopluacode > %% > \starttext > \startluacode > -- register invoice items > userdata.RegisterAmountItem("Some introductional text", 0) -- just > text > userdata.RegisterTimeItem("Learn \\LUATEX", 2.5) > userdata.RegisterTimeItem("Do some calculations in \\CONTEXT", 3.5 + > 7) > userdata.RegisterAmountItem("Donations to Open Source projects", 99) > -- lump sum > -- output > userdata.Invoice() > \stopluacode > \stoptext > > (or maybe a better example) Feel free to change it, it’s a wiki. In my own document the functions live in an environment, together with all the definitions for the letter, and the invoice items in the actual invoice document. I wanted to make a rather minimal example, so I left out all the letter stuff. The page is of use only for those who speak little Lua (I was looking for a similar example); if you know better, you won’t need it, and if you don’t speak Lua at all, it’s useless for you. Greetlings from Lake Constance! Hraban --- http://www.fiee.net/texnique/ http://wiki.contextgarden.net https://www.cacert.org (I'm an assurer)
participants (5)
-
Hans Hagen
-
Henning Hraban Ramm
-
luigi scarso
-
Philipp Gesang
-
Wolfgang Schuster