From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/113124 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ramkumar KB via ntg-context Newsgroups: gmane.comp.tex.context Subject: Re: Reading ConTeXt metadata values into Lua Date: Sun, 17 Oct 2021 16:06:27 +0800 Message-ID: References: <0876810d-a68f-4859-0403-acb7f87f0988@gmail.com> <8c281c4b-e656-d12d-c641-59468744b53a@xs4all.nl> Reply-To: mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============2259659729297251106==" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39955"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Ramkumar KB To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Sun Oct 17 10:07:21 2021 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mc1CS-000AAc-Se for gctc-ntg-context-518@m.gmane-mx.org; Sun, 17 Oct 2021 10:07:20 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 5C2C4288B8C; Sun, 17 Oct 2021 10:06:44 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Xe4zswcXNqVA; Sun, 17 Oct 2021 10:06:42 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 27A7F288B88; Sun, 17 Oct 2021 10:06:42 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id E48D7288B86 for ; Sun, 17 Oct 2021 10:06:40 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uf1OWyuf0ryf for ; Sun, 17 Oct 2021 10:06:39 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.167.42; helo=mail-lf1-f42.google.com; envelope-from=ramkumarkb@gmail.com; receiver= Original-Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 9B3E5287B42 for ; Sun, 17 Oct 2021 10:06:39 +0200 (CEST) Original-Received: by mail-lf1-f42.google.com with SMTP id u21so56627018lff.8 for ; Sun, 17 Oct 2021 01:06:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=tKIpyarfs5Iv698+0EIlWCPEYP1HKehxjAVJE6Pfg2Q=; b=Df/2nT2xJkX2YpXYFi93CZ6ZespqJ5kMLsg+mY/4a3SZb3mxJ+0co1nqYYX9AZRcAL mGTMQnj4AIY3EvIWBish18TUAWDoFCbUusu+GDzJj3yokkSbz2yw3gAQ5gUu9TxB04yc WQanJw3D8RXewBEckvNZfMVXAsEA7evzSn4nvb/9zep0SU4RRBks+yqpVfICeI+l7EAu RM1HwyimUhzwRH7o5RUdVYrQQSJFd4tftvx8l+WAX2Xr5pbkp1lnWgzVmCSyFgK/HQZq beDh9UNvs2djQNYujGJuQ6YWPMk6I2GdWajRa+taQMWjYjhrVae8Hfm1Z7SCnGdFxia2 +R7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=tKIpyarfs5Iv698+0EIlWCPEYP1HKehxjAVJE6Pfg2Q=; b=49ugqYWK1q0iRo9+adUSzJUwmg4w1Y8tEhIoKUfbp1Na68ex2UaxsazrLwAGyaLy15 RdLo99Gm2vHnIqpEPcPZxUduk5P5/vjdHhhaOalzYXnJVL5ewPoRqDJk2Rs5FyhI/X8b SVlG2Pg6oibKnHMYVa61SHWQEDw2rFScyfEiXrHzo7NT96nVpdSJB1dQyPlEc2kK2DdC QfCymT3dfkr8bWTnst/Lbq9WoVVnvfRko0F+MVFprcuKwhb+1i0yUhRlWQOoig/zwU1M RYi0CnszxtdumpILn4Y7e7a7YsihDj6NgcUdNhlxC1wKJsbC3udouPZkB7b7DQajNa5r FU7w== X-Gm-Message-State: AOAM533WdplVUDDzwZKX6DvABVq5V9yRssn4LiTIwGg2rXyfBCh0Hj5b XfG4/PWNWTUNdkztGLHqNrKDkn/4DhlMwdY82h7QlHnW X-Google-Smtp-Source: ABdhPJxPbtG3vwLrCGiChTmHOKsO004QEcCHZX2Vv6RRMKGnwYoVCgHEngew8c9dLuf/DaZBf7DMCUg4sSHyot4OZcM= X-Received: by 2002:ac2:5fee:: with SMTP id s14mr21207660lfg.537.1634457998512; Sun, 17 Oct 2021 01:06:38 -0700 (PDT) In-Reply-To: <8c281c4b-e656-d12d-c641-59468744b53a@xs4all.nl> X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:113124 Archived-At: --===============2259659729297251106== Content-Type: multipart/alternative; boundary="00000000000066d6d405ce87e868" --00000000000066d6d405ce87e868 Content-Type: text/plain; charset="UTF-8" Hans / Wolfgang, Thank you for your quick feedback and the tips. Yes, this works for now. In general, I will try to avoid going into such "deep-end" APIs (at least for now, until such time I read all the manuals in depth!). IMHO, the standard ConTeXt system is itself quite powerful for 98% of the Typesetting usecases. best regards, Ramkumar On Sat, Oct 16, 2021 at 4:59 PM Hans Hagen via ntg-context < ntg-context@ntg.nl> wrote: > On 10/16/2021 10:33 AM, Wolfgang Schuster via ntg-context wrote: > > Ramkumar KB via ntg-context schrieb am 16.10.2021 um 06:00: > >> Hello All, > >> > >> I was trying to read the value of '\totalnumberofpages' into lua to do > >> something with it. As an example, see below. > >> > >> Now, as I understand it, my example would not work as TeX gets the > >> first go at this and therefore the variable 'pages' is the string > >> '\totalnumberofpages' and not the actual value itself. And Lua cannot > >> process this "calculation". > >> > >> I tried to go thru the manual - *cld-mkiv.pdf *- in the distro but > >> could not catch on how to read a metadata value (not print). I am sure > >> that I am missing a small trick somewhere (or is it something more > >> involved?). > >> > >> \startluacode > >> function myFancyCalculations(pages) > >> -- some fancy stuff but as an example > >> local myCal = tonumber(pages) + 1 > >> return myCal > >> \stopluacode > >> \starttext > >> > >> \blah > >> > >> \ctxlua{context(myFancyCalculations("\\totalnumberofpages"))} > >> > >> \stoptext > > > > Everything gets easier when you use the Lua functions to access the > > counter values but be aware the values can be wrong when you're at a > > page break. > > > > \startbuffer[pagenumber] > > \startluacode > > context.starttabulate() > > context.NC() > > context("Current page") > > context.EQ() > > context(structures.counters.get("realpage",1,"number")) > > context.NC() > > context.NR() > > context.NC() > > context("Last page") > > context.EQ() > > context(structures.counters.get("realpage",1,"last")) > > context.NC() > > context.NR() > > context.stoptabulate() > > \stopluacode > > \stopbuffer > > > > \starttext > > > > \dorecurse > > {\randomnumber{5}{10}} > > {\getbuffer[pagenumber] > > \page} > > > > \stoptext > > As addendum ... The problem in general with macros is that one doesn't > really knows by looking at the name what actually happens when it > expands, which can be a lot. Now, because \totalnumberofpages is > supposed to 'return' a number and not typeset something styled (which > itself involved macro expansion) you can actually do this: > > \startluacode > local n = tokens.getters.macro("totalnumberofpages",true) > print(">>>",type(n),n) > \stopluacode > > but beware of the fact that you actually get back a string: > > \startluacode > local n = tokens.getters.macro("totalnumberofpages",true) > print(">>>",type(n),n) > local n = tonumber(n) > print(">>>",type(n),n) > \stopluacode > > etc etc > > Hans > > ----------------------------------------------------------------- > Hans Hagen | PRAGMA ADE > Ridderstraat 27 | 8061 GH Hasselt | The Netherlands > tel: 038 477 53 69 | www.pragma-ade.nl | 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://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > wiki : http://contextgarden.net > > ___________________________________________________________________________________ > --00000000000066d6d405ce87e868 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hans / Wolfgang,

Thank you for your= quick feedback and the tips. Yes, this works for now.

=
In general, I will try to avoid going into such "deep-end" A= PIs (at least for now, until such time I read all the manuals in depth!). I= MHO, the standard ConTeXt system=C2=A0is itself quite powerful for 98% of t= he Typesetting usecases.

best regards,
R= amkumar


On Sat, Oct 16, 2021 at 4:59 PM Hans Hagen vi= a ntg-context <ntg-context@ntg.nl<= /a>> wrote:
O= n 10/16/2021 10:33 AM, Wolfgang Schuster via ntg-context wrote:
> Ramkumar KB via ntg-context schrieb am 16.10.2021 um 06:00:
>> Hello All,
>>
>> I was trying to read the value of '\totalnumberofpages' in= to lua to do
>> something with it. As an=C2=A0example, see below.
>>
>> Now, as I understand it, my example would not work as TeX gets the=
>> first go at this and therefore the variable 'pages' is the= string
>> '\totalnumberofpages' and not the actual value itself. And= Lua cannot
>> process this "calculation".
>>
>> I tried to go thru the manual - *cld-mkiv.pdf *- in the distro but=
>> could not catch on how to read a metadata value (not print). I am = sure
>> that I am missing a small trick somewhere (or is it something more=
>> involved?).
>>
>> \startluacode
>> function=C2=A0myFancyCalculations(pages)
>> =C2=A0 -- some fancy stuff but as an example
>> local myCal =3D tonumber(pages) + 1
>> return myCal
>> \stopluacode
>> \starttext
>>
>> \blah
>>
>> \ctxlua{context(myFancyCalculations("\\totalnumberofpages&quo= t;))}
>>
>> \stoptext
>
> Everything gets easier when you use the Lua functions to access the > counter values but be aware the values can be wrong when you're at= a
> page break.
>
> \startbuffer[pagenumber]
> \startluacode
> context.starttabulate()
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.NC()
>=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 context("Current= page")
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.EQ()
>=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 context(structures.co= unters.get("realpage",1,"number"))
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.NC()
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.NR()
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.NC()
>=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 context("Last pa= ge")
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.EQ()
>=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 context(structures.co= unters.get("realpage",1,"last"))
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.NC()
>=C2=A0 =C2=A0=C2=A0=C2=A0 context.NR()
> context.stoptabulate()
> \stopluacode
> \stopbuffer
>
> \starttext
>
> \dorecurse
>=C2=A0 =C2=A0=C2=A0=C2=A0 {\randomnumber{5}{10}}
>=C2=A0 =C2=A0=C2=A0=C2=A0 {\getbuffer[pagenumber]
>=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 \page}
>
> \stoptext

As addendum ... The problem in general with macros is that one doesn't =
really knows by looking at the name what actually happens when it
expands, which can be a lot. Now, because \totalnumberofpages is
supposed to 'return' a number and not typeset something styled (whi= ch
itself involved macro expansion) you can actually do this:

\startluacode
local n =3D tokens.getters.macro("totalnumberofpages",true)
print(">>>",type(n),n)
\stopluacode

but beware of the fact that you actually get back a string:

\startluacode
local n =3D tokens.getters.macro("totalnumberofpages",true)
print(">>>",type(n),n)
local n =3D tonumber(n)
print(">>>",type(n),n)
\stopluacode

etc etc

Hans

-----------------------------------------------------------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Hans Hagen | PRAGMA ADE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ridderstraat 27 | 80= 61 GH Hasselt | The Netherlands
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tel: 038 477 53 69 |
www.pragma-ade.nl | www.= pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________= ________
If your question is of interest to others as well, please add an entry to t= he Wiki!

maillist : ntg-cont= ext@ntg.nl / http://www.ntg.nl/mailman/listinfo/nt= g-context
webpage=C2=A0 : http://www.pragma-ade.nl / http://context.aanhet.net=
archive=C2=A0 : https://bitbucket.org/phg/context-m= irror/commits/
wiki=C2=A0 =C2=A0 =C2=A0: http://contextgarden.net
___________________________________________________________________________= ________
--00000000000066d6d405ce87e868-- --===============2259659729297251106== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --===============2259659729297251106==--