[NTG-context] How to let a macro check the previous value of #1 the last time the same macro was called?

Hans Hagen j.hagen at xs4all.nl
Tue Jan 11 09:20:54 CET 2022


On 1/11/2022 5:22 AM, Aditya Mahajan via ntg-context wrote:
> On Tue, 11 Jan 2022, Henning Hraban Ramm via ntg-context wrote:
> 
>> Am 10.01.22 um 20:21 schrieb Wolfgang Schuster:
>>> Henning Hraban Ramm via ntg-context schrieb am 10.01.2022 um 09:52:
>>>> Would it make more sense, or would it be “cleaner” to use a variable?
>>>
>>> You can get rid of the temp variable before the command definition but
>>> now you have to access it with a different method in \mymacro, below is
>>> one way (LMTX only) but \setvariable and \getvariable work as well.
>>>
>>> \define[1]\mymacro
>>>     {\iftok{#1}{\getvalue{previousmymacro}}%
>>>        same as last time
>>>      \else
>>>        it is different from last time
>>>      \fi
>>>      \setvalue{previousmymacro}{#1}}
>>
>> That was the approach that I meant. Thank you.
>>
>> But would you consider one way to be better?
> 
> Just for fun: lua code
> 
> \startluacode
>    local previous = nil
> 
>    interfaces.implement {
>      name = "mymacro",
>      public = true,
>      arguments = "string",
>      actions = function (current)
>            if current == previous then
>                context("same as last time")
>            else
>                context("Different!")
>            end
>            previous = current
>        end,
>    }
> 
> \stopluacode
> 
> \starttext
> 
> \startlines
> cat: \mymacro{cat}
> cat: \mymacro{cat}
> mouse: \mymacro{mouse}
> mouse: \mymacro{mouse}
> cat: \mymacro{cat}
> \stoplines
> 
> \stoptext

keep in mind that what you do is global (probably what one wants here):

\startlines
cat: \mymacro{cat}
cat: \mymacro{cat}
{mouse: \mymacro{mouse}}
{mouse: \mymacro{mouse}}
cat: \mymacro{cat}
\stoplines


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list