From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from cgl.ntg.nl (Cgl.ntg.nl [5.39.185.202]) by inbox.vuxu.org (Postfix) with ESMTP id F1EB329D7B for ; Mon, 10 Jun 2024 10:28:56 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cgl.ntg.nl (Postfix) with ESMTP id 6349C484B34 for ; Mon, 10 Jun 2024 10:28:52 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at cgl.ntg.nl Authentication-Results: cgl.ntg.nl (amavisd-new); dkim=fail (1024-bit key) reason="fail (message has been altered)" header.d=freedom.nl Received: from cgl.ntg.nl ([127.0.0.1]) by localhost (cgl.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NtvUREL5cwQu for ; Mon, 10 Jun 2024 10:28:51 +0200 (CEST) Received: from cgl.ntg.nl (localhost [127.0.0.1]) by cgl.ntg.nl (Postfix) with ESMTP id ECAFF4846B5 for ; Mon, 10 Jun 2024 10:28:33 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cgl.ntg.nl (Postfix) with ESMTP id 30D4C48463B for ; Mon, 10 Jun 2024 10:28:20 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at cgl.ntg.nl Received: from cgl.ntg.nl ([127.0.0.1]) by localhost (cgl.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 47N5hKDrIlwl for ; Mon, 10 Jun 2024 10:28:19 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=185.233.34.20; helo=outbound.soverin.net; envelope-from=j.hagen@freedom.nl; receiver= Received: from outbound.soverin.net (outbound.soverin.net [185.233.34.20]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by cgl.ntg.nl (Postfix) with ESMTPS id B2188484633 for ; Mon, 10 Jun 2024 10:28:19 +0200 (CEST) Received: from smtp.freedom.nl (c04cst-smtp-frd01.int.sover.in [10.10.4.107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by outbound.soverin.net (Postfix) with ESMTPS id 4VyQ1H246bzJ4 for ; Mon, 10 Jun 2024 08:28:19 +0000 (UTC) Received: from smtp.freedom.nl (smtp.freedom.nl [10.10.4.107]) by freedom.nl (Postfix) with ESMTPSA id 4VyQ1H0G6Qz6L for ; Mon, 10 Jun 2024 08:28:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=freedom.nl; s=default; t=1718008099; bh=SL1ZRdsQPu0V6mBCNBIjnfFjZB/r0So5wsg5riwjLOA=; h=Date:Subject:To:References:From:In-Reply-To:From; b=lDp5+eHZD/EPylvBl1Igw1C57MQHsmB2VLOr8sMO53H1H6xVTtA0daVsM+/5R4Cbo 3zASSf+8wR/FLbNRUi5v1PICRT+lPf7EpItrhbhh2h22OwpubY6Ah5hT408fMf/fCb i5jzyHLOXZkxkfNEDBagx5VLbBjodY55I3I2Q8b8= ARC-Seal: i=1; s=default; d=freedom.nl; t=1718008099; a=rsa-sha256; cv=none; b=MMMMU6go1NcWQ8OsS0K//DaHoC7z4oT2UG07dyRaUsvzXVv5mwkxuzCR6tPsW3akZFz99R 9S4G7wJ7v1rWS6wD8tHGBICHU6KOgN9aoLeqRTOwL7Yr3bfvr8zv3ex1ZGwdksOYohYv8L 7WQNku1Sap4VbZENxThKf271hnr5gOk= ARC-Authentication-Results: i=1; smtp.freedom.nl; auth=pass smtp.mailfrom=j.hagen@freedom.nl ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freedom.nl; s=default; t=1718008099; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mu+wB+EXUPaNy/OnLUzRde2JmvXmYfhaNNkWA+Ir3zw=; b=DRqAiz8UiFXCtKwcgtTZuzmCmSot10psK4AlevQOlp1y6bwdmJTbsEFzCmCNB9inWs0PaN 7BxGM3Io1O57wD74GYs5vD9SRI9AFl+FY0Xu/1lTYi5FqdnmV0nVwhpHbQgau+kAZRTr3N p3Q6Ut8bURYwjbEpQz/vu0884pM/8K4= Message-ID: Date: Mon, 10 Jun 2024 10:28:18 +0200 MIME-Version: 1.0 Content-Language: en-US To: ntg-context@ntg.nl References: X-Soverin-Authenticated: true In-Reply-To: X-Spampanel-Class: ham Message-ID-Hash: TH33BHT54I45OSOIK2JF3WDOCTYJPPRG X-Message-ID-Hash: TH33BHT54I45OSOIK2JF3WDOCTYJPPRG X-MailFrom: j.hagen@freedom.nl X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list Reply-To: mailing list for ConTeXt users Subject: [NTG-context] Re: get measures as dimension in Lua List-Id: mailing list for ConTeXt users Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Hans Hagen via ntg-context Cc: Hans Hagen Content-Type: text/plain; charset="us-ascii"; format="flowed" Content-Transfer-Encoding: 7bit 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 ___________________________________________________________________________________