Getting the width of an expression inside lua
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
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 -----------------------------------------------------------------
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
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 -----------------------------------------------------------------
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
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
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 -----------------------------------------------------------------
participants (3)
-
Hans Hagen
-
Matthijs Kooijman
-
Wolfgang Schuster