ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Getting the width of an expression inside lua
@ 2009-11-02 11:43 Matthijs Kooijman
  2009-11-02 11:52 ` Hans Hagen
  0 siblings, 1 reply; 7+ messages in thread
From: Matthijs Kooijman @ 2009-11-02 11:43 UTC (permalink / raw)
  To: Mailing list for ConTeXt users


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

Hi all,

I'm wondering if there is any way to get the width of some TeX (or rather,
ConTeXt) expression inside lua, to do some math with it and output it again.

I've found the tex macro \setwidthof, which served me well at first, but I
need to do some more complex things with this widths (like taking the maximum
value of a bunch of widths and using that again in some other places).

Doing this in TeX would be my fallback option, but that would be a lot more
complex (I'd be generating the TeX code for it, but still...).

So, I'm looking for a function that accepts a string (and possible catcodes)
and returns the dimensions of that string as if it would be rendered directly
with tex.sprint & friends.

I've looked around the LuaTex manual a bit, and found the node.dimensions
function. However, that function requires a node object, which I have no clue
how to get at. 

Perhaps I could use \setwidthof and get at the value it assigned?

Any suggestions?

Gr.

Matthijs

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

[-- Attachment #2: Type: text/plain, Size: 486 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting the width of an expression inside lua
  2009-11-02 11:43 Getting the width of an expression inside lua Matthijs Kooijman
@ 2009-11-02 11:52 ` Hans Hagen
  2009-11-02 12:06   ` Matthijs Kooijman
  0 siblings, 1 reply; 7+ messages in thread
From: Hans Hagen @ 2009-11-02 11:52 UTC (permalink / raw)
  To: Mailing list for ConTeXt users

Matthijs Kooijman wrote:
> Hi all,
> 
> I'm wondering if there is any way to get the width of some TeX (or rather,
> ConTeXt) expression inside lua, to do some math with it and output it again.
> 
> I've found the tex macro \setwidthof, which served me well at first, but I
> need to do some more complex things with this widths (like taking the maximum
> value of a bunch of widths and using that again in some other places).

\startluacode
function document.step_one(str)
     tex.sprint("\\setbox0\\hbox{",str,"}")
     tex.sprint("\\ctxlua{document.step_two(0)}")
end
function document.step_two(n)
     tex.sprint("width: ",tex.wd[0])
end
\stopluacode

\starttext

\ctxlua{document.step_one("test {\\bf test} test")}

\box0

\stoptext


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: Getting the width of an expression inside lua
  2009-11-02 11:52 ` Hans Hagen
@ 2009-11-02 12:06   ` Matthijs Kooijman
  2009-11-02 12:31     ` Hans Hagen
  0 siblings, 1 reply; 7+ messages in thread
From: Matthijs Kooijman @ 2009-11-02 12:06 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

Hi Hans,

thanks for the instant reply :-) I haven't tried your suggestion yet, but I've
got a few questions first.

> \startluacode
> function document.step_one(str)
>     tex.sprint("\\setbox0\\hbox{",str,"}")
Would there be any danger of overwriting an existing box by doing this? I'm
not quite sure how this boxing thing works...

>     tex.sprint("\\ctxlua{document.step_two(0)}")
I guess this indirection is needed to force the \setbox command to be
expanded? I guess tex.sprint doesn't expand its arguments immediately, so just
putting document.step_two(0) instead of the entire tex.sprint("\\ctxlua...
won't work...

I'm using this from inside a pretty printer, so I'd have to think a bit about
how to set this up so things run inside the right context and stuff, but I
guess that should work out. Still slightly more complicated that a direct "How
wide is this thing" function, though.

> function document.step_two(n)
>     tex.sprint("width: ",tex.wd[0])
> end
I gues you mean tex.wd[n] here, but whatever :-)

> \ctxlua{document.step_one("test {\\bf test} test")}
> 
> \box0
Why is this \box0 needed? Just to show the test test test output? AFAICS, the
width of the box will be displayed always, even without this?

Gr.

Matthijs

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

[-- Attachment #2: Type: text/plain, Size: 486 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting the width of an expression inside lua
  2009-11-02 12:06   ` Matthijs Kooijman
@ 2009-11-02 12:31     ` Hans Hagen
  2009-11-05 14:38       ` Matthijs Kooijman
  0 siblings, 1 reply; 7+ messages in thread
From: Hans Hagen @ 2009-11-02 12:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Matthijs Kooijman wrote:
> Hi Hans,
> 
> thanks for the instant reply :-) I haven't tried your suggestion yet, but I've
> got a few questions first.
> 
>> \startluacode
>> function document.step_one(str)
>>     tex.sprint("\\setbox0\\hbox{",str,"}")
> Would there be any danger of overwriting an existing box by doing this? I'm
> not quite sure how this boxing thing works...

well, you can allocate a box for that purpose

>>     tex.sprint("\\ctxlua{document.step_two(0)}")
> I guess this indirection is needed to force the \setbox command to be
> expanded? I guess tex.sprint doesn't expand its arguments immediately, so just
> putting document.step_two(0) instead of the entire tex.sprint("\\ctxlua...
> won't work...
> 
> I'm using this from inside a pretty printer, so I'd have to think a bit about
> how to set this up so things run inside the right context and stuff, but I
> guess that should work out. Still slightly more complicated that a direct "How
> wide is this thing" function, though.
> 
>> function document.step_two(n)
>>     tex.sprint("width: ",tex.wd[0])
>> end
> I gues you mean tex.wd[n] here, but whatever :-)
> 
>> \ctxlua{document.step_one("test {\\bf test} test")}
>>
>> \box0
> Why is this \box0 needed? Just to show the test test test output? AFAICS, the
> width of the box will be displayed always, even without this?

just try ..

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: Getting the width of an expression inside lua
  2009-11-02 12:31     ` Hans Hagen
@ 2009-11-05 14:38       ` Matthijs Kooijman
  2009-11-05 14:40         ` Wolfgang Schuster
  2009-11-05 17:25         ` Hans Hagen
  0 siblings, 2 replies; 7+ messages in thread
From: Matthijs Kooijman @ 2009-11-05 14:38 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

Hi Hans,

> >Would there be any danger of overwriting an existing box by doing this? I'm
> >not quite sure how this boxing thing works...
> well, you can allocate a box for that purpose

I couldn't find any docs on allocating boxes. The only thing I could come up
with, is using #tex.box. Is that what you mean?

> just try ..
I'll do that, as soon as I've written enoug of my report so I can warrant
fiddling around with the formatting and lua again (if I'm not careful, I'll
have a very neatly formatted, but empty, report in the end :-p)

Gr.

Matthijs

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

[-- Attachment #2: Type: text/plain, Size: 486 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting the width of an expression inside lua
  2009-11-05 14:38       ` Matthijs Kooijman
@ 2009-11-05 14:40         ` Wolfgang Schuster
  2009-11-05 17:25         ` Hans Hagen
  1 sibling, 0 replies; 7+ messages in thread
From: Wolfgang Schuster @ 2009-11-05 14:40 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 05.11.2009 um 15:38 schrieb Matthijs Kooijman:

> Hi Hans,
>
>>> Would there be any danger of overwriting an existing box by doing  
>>> this? I'm
>>> not quite sure how this boxing thing works...
>> well, you can allocate a box for that purpose
>
> I couldn't find any docs on allocating boxes. The only thing I could  
> come up
> with, is using #tex.box. Is that what you mean?

I guess he means \newbox\mybox

Wolfgang

___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: Getting the width of an expression inside lua
  2009-11-05 14:38       ` Matthijs Kooijman
  2009-11-05 14:40         ` Wolfgang Schuster
@ 2009-11-05 17:25         ` Hans Hagen
  1 sibling, 0 replies; 7+ messages in thread
From: Hans Hagen @ 2009-11-05 17:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Matthijs Kooijman wrote:
> Hi Hans,
> 
>>> Would there be any danger of overwriting an existing box by doing this? I'm
>>> not quite sure how this boxing thing works...
>> well, you can allocate a box for that purpose
> 
> I couldn't find any docs on allocating boxes. The only thing I could come up
> with, is using #tex.box. Is that what you mean?

\newbox\keepyourhandsofmybox

tex.box.keepyourhandsofmybox

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

end of thread, other threads:[~2009-11-05 17:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-02 11:43 Getting the width of an expression inside lua Matthijs Kooijman
2009-11-02 11:52 ` Hans Hagen
2009-11-02 12:06   ` Matthijs Kooijman
2009-11-02 12:31     ` Hans Hagen
2009-11-05 14:38       ` Matthijs Kooijman
2009-11-05 14:40         ` Wolfgang Schuster
2009-11-05 17:25         ` Hans Hagen

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