ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Getting dimensions in Lua
@ 2021-01-18  0:58 Jairo A. del Rio
  2021-01-18  9:45 ` Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Jairo A. del Rio @ 2021-01-18  0:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 300 bytes --]

Hi, list.

I want to get some dimensions in Lua. See, for instance

\starttext

\the\dimexpr\availablehsize\relax %say, 426pt

\stoptext

Is there an equivalent in ConTeXt for Lua, i.e. without passing values from
TeX or, at least, using only Lua commands? Thank you in advance.

Best wishes,

Jairo

[-- Attachment #1.2: Type: text/html, Size: 771 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

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

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting dimensions in Lua
  2021-01-18  0:58 Getting dimensions in Lua Jairo A. del Rio
@ 2021-01-18  9:45 ` Hans Hagen
  2021-01-18 10:02   ` Taco Hoekwater
  2021-01-18 10:12   ` Jairo A. del Rio
  0 siblings, 2 replies; 5+ messages in thread
From: Hans Hagen @ 2021-01-18  9:45 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Jairo A. del Rio

On 1/18/2021 1:58 AM, Jairo A. del Rio wrote:
> Hi, list.
> 
> I want to get some dimensions in Lua. See, for instance
> 
> \starttext
> 
> \the\dimexpr\availablehsize\relax %say, 426pt
> 
> \stoptext
> 
> Is there an equivalent in ConTeXt for Lua, i.e. without passing values 
> from TeX or, at least, using only Lua commands? Thank you in advance.
tex being tex, there is always a way ...

1: \number\dimexpr\availablehsize\relax

2: \startluacode
 
token.set_macro(tex.ctxcatcodes,"temp","\\scratchdimen\\availablehsize")
     token.expand_macro("temp")
     context(tex.getdimen("scratchdimen"))
\stopluacode

3: \startluacode
 
token.set_macro(tex.ctxcatcodes,"temp","\\scratchdimen\\availablehsize")
     tex.runlocal("temp")
     context(tex.getdimen("scratchdimen"))
\stopluacode

4: \startluacode
     tex.print("\\scratchdimen\\availablehsize")
     tex.pushlocal()
     tex.poplocal()
     context(tex.getdimen("scratchdimen"))
\stopluacode

5: \startluacode
     tex.print("\\dimensiondef\\temp\\availablehsize")
     tex.pushlocal()
     tex.poplocal()
     context(tex.getdimensionvalue("temp"))
\stopluacode

\def\calculateavailablehsize{\dimensiondef\temp\availablehsize}

6: \startluacode
     token.expand_macro("calculateavailablehsize")
     context(tex.getdimensionvalue("temp"))
\stopluacode

\newtoks\tcalculateavailablehsize 
\tcalculateavailablehsize{\dimensiondef\temp\availablehsize}

7: \startluacode -- mkiv
     tex.runtoks("tcalculateavailablehsize")
     context(tex.getdimensionvalue("temp"))
\stopluacode

i'm (still) considering a way to flag a macro to be an expression (or 
value) but there are several variants possible but normally it takes a 
while before i decide what makes most sense (as i need to apply it in 
real code to see if it looks nice)

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 / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting dimensions in Lua
  2021-01-18  9:45 ` Hans Hagen
@ 2021-01-18 10:02   ` Taco Hoekwater
  2021-01-18 10:10     ` Jairo A. del Rio
  2021-01-18 10:12   ` Jairo A. del Rio
  1 sibling, 1 reply; 5+ messages in thread
From: Taco Hoekwater @ 2021-01-18 10:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users



> On 18 Jan 2021, at 10:45, Hans Hagen <j.hagen@xs4all.nl> wrote:
> 
> On 1/18/2021 1:58 AM, Jairo A. del Rio wrote:
>> Hi, list.
>> I want to get some dimensions in Lua. See, for instance
>> \starttext
>> \the\dimexpr\availablehsize\relax %say, 426pt
>> \stoptext
>> Is there an equivalent in ConTeXt for Lua, i.e. without passing values from TeX or, at least, using only Lua commands? Thank you in advance.
> tex being tex, there is always a way ...

There is also this option:

   % function returns scaled points
   \startluacode
   function availablehsize() 
      local l = tex.dimen.hsize - tex.skip.leftskip.width - tex.skip.rightskip.width
      if tex.count.hangafter<0 then
         local h = tex.dimen.hangindent
         if h>0 then  l = l - h else l = l + h end
      end
      return l 
   end
   \stopluacode

But I suspect that there are multiple cases where this does not work
(like when used in horizontal mode, for example if the par setting changes 
before the \par is seen, or when there is data in the local par node)

Best wishes,
Taco

— 
Taco Hoekwater              E: taco@bittext.nl
genderfluid (all pronouns)







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

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting dimensions in Lua
  2021-01-18 10:02   ` Taco Hoekwater
@ 2021-01-18 10:10     ` Jairo A. del Rio
  0 siblings, 0 replies; 5+ messages in thread
From: Jairo A. del Rio @ 2021-01-18 10:10 UTC (permalink / raw)
  To: Taco Hoekwater, Hans Hagen; +Cc: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1666 bytes --]

Woah, nice! I was trying to pass some values to MetaPost via Lua (usual
laziness to learn macro languages). Thank you a lot, especially because I
was looking at the wrong functions in the token namespace. I'll upload a
more specific example soon while I polish some code. Very thankful.

Jairo :)

El lun, 18 de ene. de 2021 a la(s) 05:02, Taco Hoekwater (taco@bittext.nl)
escribió:

>
>
> > On 18 Jan 2021, at 10:45, Hans Hagen <j.hagen@xs4all.nl> wrote:
> >
> > On 1/18/2021 1:58 AM, Jairo A. del Rio wrote:
> >> Hi, list.
> >> I want to get some dimensions in Lua. See, for instance
> >> \starttext
> >> \the\dimexpr\availablehsize\relax %say, 426pt
> >> \stoptext
> >> Is there an equivalent in ConTeXt for Lua, i.e. without passing values
> from TeX or, at least, using only Lua commands? Thank you in advance.
> > tex being tex, there is always a way ...
>
> There is also this option:
>
>    % function returns scaled points
>    \startluacode
>    function availablehsize()
>       local l = tex.dimen.hsize - tex.skip.leftskip.width -
> tex.skip.rightskip.width
>       if tex.count.hangafter<0 then
>          local h = tex.dimen.hangindent
>          if h>0 then  l = l - h else l = l + h end
>       end
>       return l
>    end
>    \stopluacode
>
> But I suspect that there are multiple cases where this does not work
> (like when used in horizontal mode, for example if the par setting changes
> before the \par is seen, or when there is data in the local par node)
>
> Best wishes,
> Taco
>
> —
> Taco Hoekwater              E: taco@bittext.nl
> genderfluid (all pronouns)
>
>
>
>
>
>
>
>

[-- Attachment #1.2: Type: text/html, Size: 2267 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

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

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting dimensions in Lua
  2021-01-18  9:45 ` Hans Hagen
  2021-01-18 10:02   ` Taco Hoekwater
@ 2021-01-18 10:12   ` Jairo A. del Rio
  1 sibling, 0 replies; 5+ messages in thread
From: Jairo A. del Rio @ 2021-01-18 10:12 UTC (permalink / raw)
  To: Hans Hagen, mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2512 bytes --]

To be wikified (by me, obviously). Thank you for such a great number of
alternatives.

Jairo :)

El lun, 18 de ene. de 2021 a la(s) 04:45, Hans Hagen (j.hagen@xs4all.nl)
escribió:

> On 1/18/2021 1:58 AM, Jairo A. del Rio wrote:
> > Hi, list.
> >
> > I want to get some dimensions in Lua. See, for instance
> >
> > \starttext
> >
> > \the\dimexpr\availablehsize\relax %say, 426pt
> >
> > \stoptext
> >
> > Is there an equivalent in ConTeXt for Lua, i.e. without passing values
> > from TeX or, at least, using only Lua commands? Thank you in advance.
> tex being tex, there is always a way ...
>
> 1: \number\dimexpr\availablehsize\relax
>
> 2: \startluacode
>
> token.set_macro(tex.ctxcatcodes,"temp","\\scratchdimen\\availablehsize")
>      token.expand_macro("temp")
>      context(tex.getdimen("scratchdimen"))
> \stopluacode
>
> 3: \startluacode
>
> token.set_macro(tex.ctxcatcodes,"temp","\\scratchdimen\\availablehsize")
>      tex.runlocal("temp")
>      context(tex.getdimen("scratchdimen"))
> \stopluacode
>
> 4: \startluacode
>      tex.print("\\scratchdimen\\availablehsize")
>      tex.pushlocal()
>      tex.poplocal()
>      context(tex.getdimen("scratchdimen"))
> \stopluacode
>
> 5: \startluacode
>      tex.print("\\dimensiondef\\temp\\availablehsize")
>      tex.pushlocal()
>      tex.poplocal()
>      context(tex.getdimensionvalue("temp"))
> \stopluacode
>
> \def\calculateavailablehsize{\dimensiondef\temp\availablehsize}
>
> 6: \startluacode
>      token.expand_macro("calculateavailablehsize")
>      context(tex.getdimensionvalue("temp"))
> \stopluacode
>
> \newtoks\tcalculateavailablehsize
> \tcalculateavailablehsize{\dimensiondef\temp\availablehsize}
>
> 7: \startluacode -- mkiv
>      tex.runtoks("tcalculateavailablehsize")
>      context(tex.getdimensionvalue("temp"))
> \stopluacode
>
> i'm (still) considering a way to flag a macro to be an expression (or
> value) but there are several variants possible but normally it takes a
> while before i decide what makes most sense (as i need to apply it in
> real code to see if it looks nice)
>
> 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
> -----------------------------------------------------------------
>

[-- Attachment #1.2: Type: text/html, Size: 3387 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

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

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2021-01-18 10:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-18  0:58 Getting dimensions in Lua Jairo A. del Rio
2021-01-18  9:45 ` Hans Hagen
2021-01-18 10:02   ` Taco Hoekwater
2021-01-18 10:10     ` Jairo A. del Rio
2021-01-18 10:12   ` Jairo A. del Rio

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