ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Max Chernoff <mseven@telus.net>
To: Henning Hraban Ramm <texml@fiee.net>, ntg-context@ntg.nl
Subject: [NTG-context] Re: get measures as dimension in Lua
Date: Sun, 09 Jun 2024 23:08:27 -0600	[thread overview]
Message-ID: <a6a880a61e02786c76f0873c319b02fff84f45d8.camel@telus.net> (raw)
In-Reply-To: <adee61dd-9e47-42f4-87ee-9a36e1c16454@fiee.net>

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
___________________________________________________________________________________

  reply	other threads:[~2024-06-10  5:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-09 20:12 [NTG-context] " Henning Hraban Ramm
2024-06-10  5:08 ` Max Chernoff [this message]
2024-06-10  8:28   ` [NTG-context] " 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

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=a6a880a61e02786c76f0873c319b02fff84f45d8.camel@telus.net \
    --to=mseven@telus.net \
    --cc=ntg-context@ntg.nl \
    --cc=texml@fiee.net \
    /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).