ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	"Jairo A. del Rio" <jairoadelrio6@gmail.com>
Subject: Re: Issue with \handletokens\with and Lua-defined macros
Date: Mon, 29 Mar 2021 19:16:27 +0200	[thread overview]
Message-ID: <1cd01bc2-741d-1283-5692-4d8dde12f1d5@xs4all.nl> (raw)
In-Reply-To: <CAKyqqaaosU74HZtOpbUVPeUXhAXRrQzdXknze6GACTpsXk2vsg@mail.gmail.com>

On 3/29/2021 10:54 AM, Jairo A. del Rio wrote:
> Hi list! See the following example:
> 
> \startluacode
> 
> userdata = userdata or {}
> 
> 
> --Toy example
> 
> --My actual function receives an argument, but the problem remains the same
> 
> 
> userdata.myfunction = function()
> 
> return
> 
> "343327959841638047651959775267761420323" ..
> 
> "657838053757849835434002826851807933276" ..
> 
> "324327913964298509889902373459201557839" ..
> 
> "84828001486412574060553756854137069878601"
> 
> end
> 
> 
> interfaces.implement{
> 
> name = "myfunction",
> 
> public = true,
> 
> actions =
> 
> function ()
> 
> context(userdata.myfunction())
> 
> end
> 
> }
> 
> \stopluacode
> 
> \def\colorfulnumbers#1{\colored[h=\luaexpr{40*#1},s=1,v=1]{#1}\ }%
> 
> \starttext
> 
> %It doesn't work
> 
> %\handletokens{\myfunction}\with\colorfulnumbers
> 
> \expandafter\handletokens\expandafter{\myfunction}\with\colorfulnumbers
> 
> \stoptext
> 
> 
> As readily seen from my example, I want to color each figure of the 
> numbers generated by a Lua function. However, the use of \expandafter's 
> is cumbersome in this case. I don't want to loop over the result in Lua 
> because I want to be able to apply \handletokens...\with... (or another 
> macro with a similar purpose) with different macros at the TeX end once 
> Lua has delivered results. How should I do it in ConTeXt?

In the usual way: different from what you might expect:

\startluacode
   userdata = userdata or { }

   userdata.myfunction = function()
     return
       "343327959841638047651959775267761420323" ..
       "657838053757849835434002826851807933276" ..
       "324327913964298509889902373459201557839" ..
       "84828001486412574060553756854137069878601"
end
\stopluacode

\doloopovermatch {.} {\luaexpr{userdata.myfunction()}} {%
     \colored[h=\luaexpr{40*#1},s=1,v=1]{#1}\space
}

or let youself surprise by:

\startluacode
     userdata = userdata or { }
     userdata.myfunction = function()
         return [[
             343327959841638047651959775267761420323
             657838053757849835434002826851807933276
             324327913964298509889902373459201557839
             84828001486412574060553756854137069878601
         ]]
     end
\stopluacode

\starttext

\doloopovermatch {\letterpercent d} {\luaexpr{userdata.myfunction()}} {%
     \colored[h=\luaexpr{40*#1},s=1,v=1]{#1}\space
}

(I think it's even documented somewhere.)

\handletokens is kind of mkii-ish and old fashioned.

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
___________________________________________________________________________________

      reply	other threads:[~2021-03-29 17:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-29  8:54 Jairo A. del Rio
2021-03-29 17:16 ` Hans Hagen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1cd01bc2-741d-1283-5692-4d8dde12f1d5@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=jairoadelrio6@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).