ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: "Jairo A. del Rio" <jairoadelrio6@gmail.com>,
	mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: mathfun
Date: Sun, 23 May 2021 16:35:28 +0200	[thread overview]
Message-ID: <91b591a4-95f8-368f-d02a-bffefa99b505@xs4all.nl> (raw)
In-Reply-To: <CAKyqqab53burZsshMQqFAkmzcAGxk84FPHNh06d=MZYonXxrTg@mail.gmail.com>

On 5/23/2021 3:45 PM, Jairo A. del Rio wrote:
> El dom, 23 de may. de 2021 a la(s) 04:51, Hans Hagen (j.hagen@xs4all.nl 
> <mailto:j.hagen@xs4all.nl>) escribió:
> 
>     On 5/22/2021 11:34 PM, Jairo A. del Rio wrote:
>      > Hi. Is \thewithproperty documented elsewhere? Is this a new
>     primitive?
>      > It looks interesting.
>     In the case of the \sin trick particular case \let\computer\the would
>     also work; i added the \thewithproperty<number> feature just because i
>     expect that users can come up with variants (like \drawme\sin ...)
>     which
>     then would adapt behaviour (via \dowithproperty2).
> 
> 
> Having flags for computing and drawing would be awesome. I think it 
> would nicely extend ConTeXt's drawing features...
> 
>     So, indeed it's a new primitive but (for now) only used with these lua
>     hacks. Actually it's no big deal to reimplement \thewithoutunit<dim> as
> 
> 
> So, is this Lua-specific?

it relates to teh way tex sees things ... thihnk of

   \dimen0=10pt

where \dimen expects a number, optional equal and value

while

   \the\dimen0

the '\the' expects all kind of things, with '\dimen' being one of then 
in which case its value is serialized

in order to implement 'new primitives' using lua (like some that need to 
be implemented for compatibility reasons due to the backend differences) 
we need to be able to distinguish between setting and getting a value

so in that sense it's indeed lua specific: a specific class of lua 
functions knows if it's triggered by serialization

and that is what we 'kind of' abuse here and also why i did add this 
additional 'pseudo prefix' so that we ca ndo more clever things if we 
want to

>         \thewithoutunit{\dowithproperty1\the}
> 
>     and then
> 
>         \thewithcmunit{\dowithproperty2\the}
> 
>     to serialize the dimension in cm but normally i need to "wonder for a
>     while if it makes sense".
> 
>     Btw, adding primitives (related to the token related bits and pieces of
>     the engine) only makes sense if the gain is in
> 
>     -- avoiding clumsy code: although that can hurt someones pride and joy
>     over a nice and complex macro doing the same ... don't worry, i threw
>     away plenty of code by now
> 
>     -- significant performance gain: which is seldom the case (there really
>     is not much to gain, at least not in context which is rather optimized
>     already)
> 
>     -- giving way less noise in tracing: which for me is a good reason
>     (tracing changed a bit anyway)
> 
>     -- not adding much code to the engine or the need to refactor: although
>     for some luametatex extensions some internals had to change
>     fundamentally
> 
>     a good example is the extended macro argument handling: less clumsy tex
>     code, a bit of performance gain and easier full expansion, way less
>     tracing, but it violates the last argument because it did lead to some
>     refactoring (i also had to make sure the extra overhead didn't slow
>     down
>     macro definitions and expansion)

> I did the following and I have two questions:
> 
> 1. What if I want to have more than one property? Say \plusone for 
> computation and \plustwo for drawing.

you can do that but keep in mind that \compute is then an 'official' 
context one (bound to 1) and any other number can conflict with other 
context numbers in the future, so best start at 1000 or so for private 
ones

> 2. What does 'usage="value"' mean in the implement function (looks new, 
> but I'm prolly wrong)?

indeed, it is already there for a while and it lets the function 
definition follow a different route ... basically one that can follow 
\the; I suppose that I can also make 'property' a valid usage (alias)

(btw often features like this are described in articles or wrapups inm 
documents in the distribution or even mentioned here but as usual with 
tex these things only make sense trigger usage when one really needs it)

> \permanent\protected\def\drawme{\thewithproperty\plussix} %better than 666

or 31415 to fit in the tex numbering

> \startluacode
> local interfaces = interfaces
> local implement = interfaces.implement
> local context = context
> local drawme_code = 6
> local drawing = [==[
> draw fullcircle scaled 10mm;
> draw fullcircle shifted up scaled 5mm;
> draw fulltriangle shifted (up+1/2right) scaled 5mm;
> ]==]
> 
> 
> local function chicken(value)
> if value == "value" or value == drawme_code then

no need to check for "value" unless you also want:

\the\chicken

> context.metafun.start()
> context.metafun(drawing);
> context.metafun.stop()
> --elseif then ...
> else
> context("Chicken!")
> end
> end
> 
> implement{
> name = "chicken",
> public = true,
> usage = "value", --What does it do?
> overload = true,
> actions = chicken
> }
> 
> \stopluacode
> 
> \starttext\chicken\ and \drawme\chicken\stoptext
indeed an example of usage, and as with the mathfun, it saves a ton of 
macros like \drawchicken and so

so now you next challenge is to pass arguments so here a next phase

\permanent\protected\def\drawme{\thewithproperty1006 }

\startluacode

local interfaces = interfaces
local implement = interfaces.implement
local context = context

local d = {
     chickcolor = "red",
     beakcolor  = "green",
}

local drawing = utilities.templates.replacer [==[
     draw fullcircle scaled 10mm withcolor %chickcolor%;
     draw fullcircle shifted up scaled 5mm;
     draw fulltriangle shifted (up+1/2right) scaled 5mm withcolor 
%beakcolor%;
]==]


local function chicken(value)
	if value == 1006 then
         local t = tokens.scanners.hash() or { }
         table.setmetatableindex(t,d)
         context.metafun.start()
             context.metafun(drawing(t,v))
		context.metafun.stop()
	else
		context("Chicken!")
	end
end

implement{
	name     = "chicken",
	public   = true,
	usage    = "value", --What does it do?
	overload = true,
	actions  = chicken
}

\stopluacode

\starttext
     \chicken\ and \drawme\chicken
     \chicken\ and \drawme\chicken[chickcolor=blue,beakcolor=cyan]
\stoptext

(just to remind readers of the real old and probably seldom used 
template mechanism)

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-05-23 14:35 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-21 13:15 mathfun Hans Hagen
2021-05-21 14:37 ` mathfun Aditya Mahajan
2021-05-21 15:06   ` mathfun Hans Hagen
2021-05-21 15:24     ` mathfun Otared Kavian
2021-05-21 16:07       ` mathfun Hans Hagen
2021-05-21 20:41       ` mathfun Aditya Mahajan
2021-05-21 21:01 ` mathfun Henning Hraban Ramm
2021-05-22  6:51   ` mathfun Hans Hagen
2021-05-22 21:34 ` mathfun Jairo A. del Rio
2021-05-23  9:51   ` mathfun Hans Hagen
2021-05-23 13:45     ` mathfun Jairo A. del Rio
2021-05-23 14:35       ` Hans Hagen [this message]
2021-05-23 15:33         ` mathfun Jairo A. del Rio
2021-05-23 21:29           ` mathfun Hans Hagen
2021-05-23 14:40       ` mathfun Hans Hagen

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=91b591a4-95f8-368f-d02a-bffefa99b505@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).