ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] get measures as dimension in Lua
@ 2024-06-09 20:12 Henning Hraban Ramm
  2024-06-10  5:08 ` [NTG-context] " Max Chernoff
  0 siblings, 1 reply; 6+ messages in thread
From: Henning Hraban Ramm @ 2024-06-09 20:12 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,
when I define a measure on the TeX side, I used to get at the value in 
Lua with tex.getdim, like:

\definemeasure[Bleed][3mm]

tex.getdimen("Bleed")

But now I get "incorrect dimen name".
I need it as a dimension, since I do calculations with other dimensions 
like "topspace".

How should I do this correctly?

(I just assume this used to work – the code is old any maybe stuck in an 
intermediate state where I switched from \newdim to \definemeasure…)

Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: get measures as dimension in Lua
  2024-06-09 20:12 [NTG-context] get measures as dimension in Lua Henning Hraban Ramm
@ 2024-06-10  5:08 ` Max Chernoff
  2024-06-10  8:28   ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 6+ messages in thread
From: Max Chernoff @ 2024-06-10  5:08 UTC (permalink / raw)
  To: Henning Hraban Ramm, ntg-context

Hi Hraban,

On Sun, 2024-06-09 at 22:12 +0200, Henning Hraban Ramm wrote:
> when I define a measure on the TeX side, I used to get at the value in
> Lua with tex.getdim, like:
>
> \definemeasure[Bleed][3mm]
>
> tex.getdimen("Bleed")
>
> But now I get "incorrect dimen name".
> I need it as a dimension, since I do calculations with other dimensions
> like "topspace".
>
> How should I do this correctly?

There doesn't appear to be any official interfaces to get the value of a
measure from Lua, but the following should work okay:

    \definemeasure[mymeasure][10pt]

    \newdimen\mydimen \mydimen=10pt

    \definemeasure[measureexpr][1in + 27.73pt]

    \startluacode
        -- Returns the unexpanded value of some variable in a namespace.
        local function get_variable(namespace, variable)
            -- -- Doesn't work since "measure" is defined as a "system namespace".
            -- namespace = interfaces.getnamespace(namespace)
            namespace = tokens.getters.macro("??" .. namespace)
            return tokens.getters.macro(namespace .. variable)
        end

        -- Gets the value of a measure and converts it to an integer in sp units.
        local function get_measure(name)
            return tex.sp(get_variable("measure", name))
        end

        -- Gets the \dimexpr-evaluated value of a measure, in sp units.
        local function get_measure_expr(name)
            local namespace = tokens.getters.macro("??measure")
            -- Undocumented, but seems to work as expected.
            return tex.getdimensionvalue(namespace .. name)
        end

        -- Print the values of the lengths.
        print(("="):rep(80))
        print("mymeasure:"  , get_measure("mymeasure")       )
        print("mydimen:"    , tex.dimen["mydimen"]           )
        print("measureexpr:", get_measure_expr("measureexpr"))
        print(("="):rep(80))
    \stopluacode

    \startTEXpage
        \measure{mymeasure}
        \the\mydimen
        \measure{measureexpr}
    \stopTEXpage

Thanks,
-- Max

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: get measures as dimension in Lua
  2024-06-10  5:08 ` [NTG-context] " Max Chernoff
@ 2024-06-10  8:28   ` Hans Hagen via ntg-context
  2024-06-10  8:58     ` Henning Hraban Ramm
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen via ntg-context @ 2024-06-10  8:28 UTC (permalink / raw)
  To: ntg-context; +Cc: Hans Hagen

On 6/10/2024 7:08 AM, Max Chernoff wrote:
> Hi Hraban,
> 
> On Sun, 2024-06-09 at 22:12 +0200, Henning Hraban Ramm wrote:
>> when I define a measure on the TeX side, I used to get at the value in
>> Lua with tex.getdim, like:
>>
>> \definemeasure[Bleed][3mm]
>>
>> tex.getdimen("Bleed")
>>
>> But now I get "incorrect dimen name".
>> I need it as a dimension, since I do calculations with other dimensions
>> like "topspace".
>>
>> How should I do this correctly?
> 
> There doesn't appear to be any official interfaces to get the value of a
> measure from Lua, but the following should work okay:
> 
>      \definemeasure[mymeasure][10pt]
> 
>      \newdimen\mydimen \mydimen=10pt
> 
>      \definemeasure[measureexpr][1in + 27.73pt]
> 
>      \startluacode
>          -- Returns the unexpanded value of some variable in a namespace.
>          local function get_variable(namespace, variable)
>              -- -- Doesn't work since "measure" is defined as a "system namespace".
>              -- namespace = interfaces.getnamespace(namespace)
>              namespace = tokens.getters.macro("??" .. namespace)
>              return tokens.getters.macro(namespace .. variable)
>          end
> 
>          -- Gets the value of a measure and converts it to an integer in sp units.
>          local function get_measure(name)
>              return tex.sp(get_variable("measure", name))
>          end
> 
>          -- Gets the \dimexpr-evaluated value of a measure, in sp units.
>          local function get_measure_expr(name)
>              local namespace = tokens.getters.macro("??measure")
>              -- Undocumented, but seems to work as expected.
>              return tex.getdimensionvalue(namespace .. name)
>          end
> 
>          -- Print the values of the lengths.
>          print(("="):rep(80))
>          print("mymeasure:"  , get_measure("mymeasure")       )
>          print("mydimen:"    , tex.dimen["mydimen"]           )
>          print("measureexpr:", get_measure_expr("measureexpr"))
>          print(("="):rep(80))
>      \stopluacode
> 
>      \startTEXpage
>          \measure{mymeasure}
>          \the\mydimen
>          \measure{measureexpr}
>      \stopTEXpage
> 
> Thanks,
> -- Max

Indeed we can have:

     local namespace = tokens.getters.macro("??measure")

     function tex.getmeasure(name,asdimen)
         local value = tex.getdimensionvalue(namespace..name)
         if asdimen then
             return value .. "sp"
         else
             return value
         end

and when you want to avoid the namespace you can do:

     function tex.getmeasure(name,asdimen)
         local value = 
token.getexpansion("\\tointeger\\measured{"..name.."}")
         if asdimen then
             return value .. "sp"
         else
             return tonumber(value)
         end
     end

which is some 4 times slower. It'sindeed not documented (although there 
are some examples in accessors-001.tex) but it's in one of my todo's 
because I can use it in some places (there ar emopre such todo's) after 
which it will be discussed in one of the lowlelvel manuals

i'll add tex.getmeasure to the core (but a bit different)

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 / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: get measures as dimension in Lua
  2024-06-10  8:28   ` Hans Hagen via ntg-context
@ 2024-06-10  8:58     ` Henning Hraban Ramm
  2024-06-13 17:24       ` Henning Hraban Ramm
  0 siblings, 1 reply; 6+ messages in thread
From: Henning Hraban Ramm @ 2024-06-10  8:58 UTC (permalink / raw)
  To: ntg-context

Thank you, Max, for the effort and Hans for a core solution!

Am 10.06.24 um 10:28 schrieb Hans Hagen via ntg-context:
> Indeed we can have:
> 
>      local namespace = tokens.getters.macro("??measure")
> 
>      function tex.getmeasure(name,asdimen)
>          local value = tex.getdimensionvalue(namespace..name)
>          if asdimen then
>              return value .. "sp"
>          else
>              return value
>          end
> 
> and when you want to avoid the namespace you can do:
> 
>      function tex.getmeasure(name,asdimen)
>          local value = 
> token.getexpansion("\\tointeger\\measured{"..name.."}")
>          if asdimen then
>              return value .. "sp"
>          else
>              return tonumber(value)
>          end
>      end
> 
> which is some 4 times slower. It'sindeed not documented (although there 
> are some examples in accessors-001.tex) but it's in one of my todo's 
> because I can use it in some places (there ar emopre such todo's) after 
> which it will be discussed in one of the lowlelvel manuals
> 
> i'll add tex.getmeasure to the core (but a bit different)

Not to be misunderstood, I couldn’t check yet, maybe I wasn’t clear:

I need to process the measure as a dimension, like \measured{Bleed}.

As far as I tried, neither tex.measured nor context.measured worked (but 
I wouldn’t swear on it, maybe I had a different error).

Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: get measures as dimension in Lua
  2024-06-10  8:58     ` Henning Hraban Ramm
@ 2024-06-13 17:24       ` Henning Hraban Ramm
  2024-06-13 21:03         ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 6+ messages in thread
From: Henning Hraban Ramm @ 2024-06-13 17:24 UTC (permalink / raw)
  To: ntg-context

Am 10.06.24 um 10:58 schrieb Henning Hraban Ramm:

>>      local namespace = tokens.getters.macro("??measure")
>>
>>      function tex.getmeasure(name,asdimen)
>>          local value = tex.getdimensionvalue(namespace..name)
>>          if asdimen then
>>              return value .. "sp"
>>          else
>>              return value
>>          end

So I needed just tex.getdimensionvalue(namespace..name)


> Not to be misunderstood, I couldn’t check yet, maybe I wasn’t clear:
> 
> I need to process the measure as a dimension, like \measured{Bleed}.
> 
> As far as I tried, neither tex.measured nor context.measured worked (but 
> I wouldn’t swear on it, maybe I had a different error).

I understand now that a dimension in Lua is just a string.

I also messed a few thing up when I converted my macros from 
measure-macros to measures.

Now my image calculations work again. Phew!

Thank you,
Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: get measures as dimension in Lua
  2024-06-13 17:24       ` Henning Hraban Ramm
@ 2024-06-13 21:03         ` Hans Hagen via ntg-context
  0 siblings, 0 replies; 6+ messages in thread
From: Hans Hagen via ntg-context @ 2024-06-13 21:03 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen

On 6/13/2024 7:24 PM, Henning Hraban Ramm wrote:
> Am 10.06.24 um 10:58 schrieb Henning Hraban Ramm:
> 
>>>      local namespace = tokens.getters.macro("??measure")
>>>
>>>      function tex.getmeasure(name,asdimen)
>>>          local value = tex.getdimensionvalue(namespace..name)
>>>          if asdimen then
>>>              return value .. "sp"
>>>          else
>>>              return value
>>>          end
> 
> So I needed just tex.getdimensionvalue(namespace..name)

just use tex.getmeasure in the upcoming

>> Not to be misunderstood, I couldn’t check yet, maybe I wasn’t clear:
>>
>> I need to process the measure as a dimension, like \measured{Bleed}.
>>
>> As far as I tried, neither tex.measured nor context.measured worked 
>> (but I wouldn’t swear on it, maybe I had a different error).
> 
> I understand now that a dimension in Lua is just a string.

no, it is a number (scaled points or sp) but it can have a string 
representation

> I also messed a few thing up when I converted my macros from 
> measure-macros to measures.
> 
> Now my image calculations work again. Phew!
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 / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-06-13 21:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-09 20:12 [NTG-context] get measures as dimension in Lua Henning Hraban Ramm
2024-06-10  5:08 ` [NTG-context] " Max Chernoff
2024-06-10  8:28   ` Hans Hagen via ntg-context
2024-06-10  8:58     ` Henning Hraban Ramm
2024-06-13 17:24       ` Henning Hraban Ramm
2024-06-13 21:03         ` Hans Hagen via ntg-context

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).