Dear Team, it is a common convention in luatex that dimensions are specified either as a number (in scaled points) or a string with unit included. But unless I'm blind, there is no helper function that converts string dimension to a number. Seems that there is a standard dimen_to_number() function for that purpose. Would be convenient to have such on aboard. If I make such a helper, will you consider adding it to a repo? I'm thinking of tex.sp'1in' > 4736286 tex.sp() > error argument must be a string or more general tex.unit('1in', 'cm') > 2.54 wdyt?  Pawe/l Jackowski P.Jackowski@gust.org.pl
On 2022010 14:02, Paweł Jackowski wrote:
Dear Team,
it is a common convention in luatex that dimensions are specified either as a number (in scaled points) or a string with unit included. But unless I'm blind, there is no helper function that converts string dimension to a number. Seems that there is a standard dimen_to_number() function for that purpose. Would be convenient to have such on aboard. If I make such a helper, will you consider adding it to a repo? I'm thinking of
tex.sp'1in' > 4736286 tex.sp() > error argument must be a string
or more general
tex.unit('1in', 'cm') > 2.54
wdyt?
luatex is consistently scaled points and a helper can be written in luq quite easily also, keep in mind that performance wise it's best to do it in lua anyway; it's actually just a matter of multiplication (which saves a function call too) such helper functions (if they make sense at all) cna be considered as part of adding some kind of float support (which we occasionally consider) Hans  Hans Hagen  PRAGMA ADE Ridderstraat 27  8061 GH Hasselt  The Netherlands tel: 038 477 53 69  fax: 038 477 53 74  www.pragmaade.com  www.pragmapod.nl 
Hans Hagen
On 2022010 14:02, Paweł Jackowski wrote:
Dear Team,
it is a common convention in luatex that dimensions are specified either as a number (in scaled points) or a string with unit included. But unless I'm blind, there is no helper function that converts string dimension to a number. Seems that there is a standard dimen_to_number() function for that purpose. Would be convenient to have such on aboard. If I make such a helper, will you consider adding it to a repo? I'm thinking of
tex.sp'1in' > 4736286 tex.sp() > error argument must be a string
or more general
tex.unit('1in', 'cm') > 2.54
wdyt?
luatex is consistently scaled points and a helper can be written in luq quite easily
also, keep in mind that performance wise it's best to do it in lua anyway; it's actually just a matter of multiplication (which saves a function call too)
No, it isn't. It is a matter of multiplication, division and truncation. It is a littleknow fact that 2.54cm are 72.2698pt in TeX, 1in is 72.26999pt, and both are different from 72.27pt, while 254cm and 100in perfectly well are 7227.0pt. So no, you can't substitute a unit in TeX with a constant. Units are _fractions_ of sp, and applying a "multiplier" to them does not work like one would think.  David Kastrup
David Kastrup wrote:
No, it isn't. It is a matter of multiplication, division and truncation.
It is a littleknow fact that 2.54cm are 72.2698pt in TeX, 1in is 72.26999pt, and both are different from 72.27pt, while 254cm and 100in perfectly well are 7227.0pt.
So no, you can't substitute a unit in TeX with a constant. Units are _fractions_ of sp, and applying a "multiplier" to them does not work like one would think.
Valid points. I don't see much use in a generic converter, but tex.sp "string" is a quick and simple extension. Although I do fear that dimen_to_number (in ltexlib.c) will need unfixing to be totally compatible with TeX82: I am using strtod() and the documented conversion ratios there instead of the actual code from Knuth. Best wishes, Taco
Thank you. In such moments I always wonder how far I can annoy you until being banned. One more try ;) Hans:
luatex is consistently scaled points and a helper can be written in luq quite easily
tex.scale() is just a multiplication and a range check, which is trivial, much more trivial then what dimen_to_number does. tex.round() function is also trivial, but we do have a helper! Nearly everything can be written in lua. Such a helper is needed not because it is difficult to implement, but to let the user have the same mechanism that is used internally. It is so generic, that everyone who will enter an intimate relationship with luatex will need to have his own dimentosp function, unless luatex provide one. Taco:
Although I do fear that dimen_to_number (in ltexlib.c) will need unfixing to be totally compatible with TeX82: I am using strtod() and the documented conversion ratios there instead of the actual code from Knuth.
And when it will happen, the effects of tex.dimen[100] = '123mm' image.bbox = {0,0,'100bp','200pt'} may be slightly different. So be it, noone will cry, because tex.sp() function would follow the new behaviour of dimen_to_number()... Here is what works for me. Accept or deny, I won't insist any longer. Index: ltexlib.c ===================================================================  ltexlib.c (revision 3434) +++ ltexlib.c (working copy) @@ 1269,6 +1269,24 @@ return 1; } +static int tex_scaledimen(lua_State * L) /* following vsetdimen() */ +{ + int sp; + if (!lua_isnumber(L, 1)) { + if (lua_isstring(L, 1)) { + sp = dimen_to_number(L, lua_tostring(L, 1)); + } else { + lua_pushstring(L, "argument must be a string or a number"); + lua_error(L); + return 0; + } + } else { + lua_number2int(sp, lua_tonumber(L, 1)); + } + lua_pushnumber(L, sp); + return 1; +} + #define hash_text(A) hash[(A)].rh static int tex_definefont(lua_State * L) @@ 1571,6 +1589,7 @@ {"getboxdp", getboxdp}, {"round", tex_roundnumber}, {"scale", tex_scaletable}, + {"sp", tex_scaledimen}, {"fontname", getfontname}, {"fontidentifier", getfontidentifier}, {"pdffontname", getpdffontname},  Pawe/l Jackowski P.Jackowski@gust.org.pl
On 21 February 2010 Arthur Reutenauer wrote:
In such moments I always wonder how far I can annoy you until being banned. One more try ;)
There's always the LuaTeX user list (luatex@tug.org) ;)
Since Paweł asked Hans and Taco explicitly, I suppose that it doesn't make any difference on which list he asks. Regards, Reinhard   Reinhard Kotucha Phone: +495113373112 Marschnerstr. 25 D30167 Hannover mailto:reinhard.kotucha@web.de  Microsoft isn't the answer. Microsoft is the question, and the answer is NO. 
Reinhard Kotucha
On 21 February 2010 Arthur Reutenauer wrote:
In such moments I always wonder how far I can annoy you until being banned. One more try ;)
There's always the LuaTeX user list (luatex@tug.org) ;)
Since Paweł asked Hans and Taco explicitly, I suppose that it doesn't make any difference on which list he asks.
There are significant statistical differences in sports depending on whether you are playing against the same team on their or on your home turf, to the unbalanced applause of the respective audience. And it is routine for comedy features to transmit canned or live audience laughter for the sake of listeners. Works too. "If you can make it there, you can make it anywhere. It's up to you, dev lu a tex..." Thanks, you are too kind.  David Kastrup
Hi, Paweł Jackowski wrote:
Taco:
Although I do fear that dimen_to_number (in ltexlib.c) will need unfixing to be totally compatible with TeX82: I am using strtod() and the documented conversion ratios there instead of the actual code from Knuth.
And when it will happen, the effects of
tex.dimen[100] = '123mm' image.bbox = {0,0,'100bp','200pt'}
may be slightly different. So be it, noone will cry, because tex.sp() function would follow the new behaviour of dimen_to_number()...
I did this, the string dimension scanner now follows the same rules as TeX82's dimension scanner for 'bare dimensions' (i.e. no control sequences and no fill units). So, the conversion from lua string to dimension now follows exactly the same rules as TeX82 and pdftex. It also means you can now use 'em', 'ex', 'mu' and 'px' units, that the conversion takes the current magnification factor into account (and you can use the 'true' keyword), and that hexadecimal and octal integer values (prefix " or ') are allowed.
Here is what works for me. Accept or deny, I won't insist any longer.
Applied, so there now is the new function tex.sp(<string>). Both changes are in revision #3435. Best wishes, Taco
Taco: [...]
Here is what works for me. Accept or deny, I won't insist any longer.
Applied, so there now is the new function tex.sp(<string>). Both changes are in revision #3435.
I confirm tex.sp function working as expected, including changed behaviour of dimen_to_number() . Thank you!  Pawe/l Jackowski P.Jackowski@gust.org.pl
participants (6)

Arthur Reutenauer

David Kastrup

Hans Hagen

Paweł Jackowski

Reinhard Kotucha

Taco Hoekwater