Setting and measuring boxes TeX/Lua
Hello! I would like to set framed boxes, measure their dimensions and proceed based on the measurement. It is a combination of TeX and Lua ends. - Boxes are set on the TeX side. Stacking boxes (hbox, framed, vbox) in Lua looked too complex. - The content of the inner box is set on the Lua side (some computations). On TeX side boxes and their dimensions are accessible, but not at Lua side, until luacode is finished (iterations over boxes happen here). I would say Hans already named the problem in the list: *that is always tricky as what happens is postponing till the group ends*. Do you have an idea how to expand the box sooner or would you use a different approach? Sorry for longer MWE (attached also). Thank you, Jano \def\StartBox{\setbox0=\hbox\bgroup% open hbox \framed[frame=on]\bgroup% open framed \vbox\bgroup% open vbox (\par needed) } \def\StopBox{\egroup\egroup\egroup% close vbox, framed, hbox \setvariable{Test}{Height}{\the\ht0}% }% \startluacode ctx = context function TestBox() ctx("TeX Box: ") ctx.copy(false,0)ctx.par() ctx("TeX Getvar: ") ctx.getvariable("Test", "Height")ctx.par() var = tokens.getters.macro(tokens.getters.macro("??variables") .. "Test:Height") ctx("Lua Type: ") ctx(type(var))ctx.par() ctx("Lua Value: ") ctx(var)ctx.par() end \stopluacode \starttext \startluacode ctx.bold("First luacode (more boxes)")ctx.par() ctx.StartBox() ctx("AAA")ctx.par() ctx("BBB")ctx.par() ctx.StopBox() TestBox() ctx.StartBox() ctx("CCC")ctx.par() ctx.StopBox() TestBox() \stopluacode \blank[4*big] \startluacode ctx.bold("Second luacode (last content of box0)")ctx.par() TestBox() \stopluacode \stoptext
On 11/25/2020 7:31 PM, Jano Kula wrote:
Hello!
I would like to set framed boxes, measure their dimensions and proceed based on the measurement. It is a combination of TeX and Lua ends.
* Boxes are set on the TeX side. Stacking boxes (hbox, framed, vbox) in Lua looked too complex. * The content of the inner box is set on the Lua side (some computations).
On TeX side boxes and their dimensions are accessible, but not at Lua side, until luacode is finished (iterations over boxes happen here). I would say Hans already named the problem in the list: /that is always tricky as what happens is postponing till the group ends/.
Do you have an idea how to expand the box sooner or would you use a different approach?
Sorry for longer MWE (attached also).
Thank you, Jano
\def\StartBox{\setbox0=\hbox\bgroup% open hbox \framed[frame=on]\bgroup% open framed \vbox\bgroup% open vbox (\par needed) } \def\StopBox{\egroup\egroup\egroup% close vbox, framed, hbox \setvariable{Test}{Height}{\the\ht0}% }%
\startluacode ctx = context
function TestBox() ctx("TeX Box: ") ctx.copy(false,0)ctx.par() ctx("TeX Getvar: ") ctx.getvariable("Test", "Height")ctx.par() var = tokens.getters.macro(tokens.getters.macro("??variables") .. "Test:Height") ctx("Lua Type: ") ctx(type(var))ctx.par() ctx("Lua Value: ") ctx(var)ctx.par() end \stopluacode
\starttext
\startluacode ctx.bold("First luacode (more boxes)")ctx.par() ctx.StartBox() ctx("AAA")ctx.par() ctx("BBB")ctx.par() ctx.StopBox() TestBox() ctx.StartBox() ctx("CCC")ctx.par() ctx.StopBox() TestBox() \stopluacode
\blank[4*big]
\startluacode ctx.bold("Second luacode (last content of box0)")ctx.par() TestBox() \stopluacode
\stoptext \startluacode context.stepwise (function() ctx.bold("First luacode (more boxes)")ctx.par() ctx.StartBox() ctx("AAA")ctx.par() ctx("BBB")ctx.par() ctx.StopBox() context.step() TestBox() context.step() ctx.StartBox() ctx("CCC")ctx.par() ctx.StopBox() context.step() TestBox() context.step() end) \stopluacode
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Hello Hans!
On Wed, 25 Nov 2020 at 21:25, Hans Hagen
\startluacode context.stepwise (function() context.step() end) \stopluacode
Thank you very much! I was trying context.stepwise several times (and \dowithnextbox, \nextboxht and other things). I bet there was no example of empty context.step(). The usage was: stepping in TeX end with context.step("\\texcommands\\with\\doubleslashes") without leaving Lua. What is the purpose of empty context.step()? Knocking on TeX's door? Jano
On 11/25/2020 10:28 PM, Jano Kula wrote:
Hello Hans!
On Wed, 25 Nov 2020 at 21:25, Hans Hagen
mailto:j.hagen@xs4all.nl> wrote: \startluacode context.stepwise (function() context.step() end) \stopluacode
Thank you very much!
I was trying context.stepwise several times (and \dowithnextbox, \nextboxht and other things). I bet there was no example of empty context.step().
The usage was: stepping in TeX end with context.step("\\texcommands\\with\\doubleslashes") without leaving Lua. What is the purpose of empty context.step()? Knocking on TeX's door? no, kind of quit, goto tex and then pickup again (coroutine magic)
(this mechanism is already kind of old, there are also other cheats possible) 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 -----------------------------------------------------------------
Hello again, Hans!
On Thu, 26 Nov 2020 at 09:18, Hans Hagen
(this mechanism is already kind of old, there are also other cheats possible)
Diving into functions and using context.stepwise() worked, until I've got to: /cldf-stp.lua:48: bad argument #1 to 'resume' (thread expected) and other errors. It was triar/error approach, not exactly knowing how context.stepwise() works, and I was some more lua functions and iterations before needing expansion. Therefore I've changed the strategy (cannot give up): - Create named buffers with formatted content (and store measures) before they are needed. - When needed, use the reference to the buffer. Tests looked promising. After \starttext buffers and measures are known (both TeX & Lua). Also tested in functions, etc. But inside the first \xmlsetup, they are not expanded anymore, they are typeset (like \typebuffer). - How to have them expanded in \xmlsetup? - Is other mechanism better suited for storing typeset blocks than buffers (and will survive \xmlsetup)? - If none of them is possible, what are the *other **cheats *then? Thanks, Jano MWE attached % BUFFERS \startluacode -- fill buffers for i=1,1000 do local content = "framed-" .. i context.startbuffer( { i } ) context.StartBox() context.bold(i)context.par() context.color( { "red" }, content ) context.StopBox() context.copy(false,0) context.stopbuffer() print(">>", i) end \stopluacode \def\StartBox{\setbox0=\hbox\bgroup\framed[width=50mm, background=color, backgroundcolor=gray, frame=off,align={flushleft}]% \bgroup% \vbox\bgroup} \def\StopBox{\egroup\egroup\egroup}% close vbox, framed, hbox \def\MeasureBuffer[#1]{\setbox0\hbox{\getbuffer[#1]}% ht: \the\ht0\par} % XML \startbuffer[doc] <?xml version "1.0"?> <document> <section>section</section> <section>section</section> </document> \stopbuffer \startxmlsetups xml:name \xmlsetsetup{\xmldocument}{*}{-} \xmlsetsetup{\xmldocument}{document}{xml:name:*} \stopxmlsetups \xmlregistersetup{xml:name} \startxmlsetups xml:name:document \Title{inside xml:name:document} \Title{TeX} \MeasureBuffer[333] \getbuffer[333] \Title{Lua} \startluacode context.MeasureBuffer({"444"}) context.getbuffer({"444"}) \stopluacode \stopxmlsetups \def\Title#1{\blank{\bf #1}\par} \starttext \Title{TeX} \MeasureBuffer[111] \getbuffer[111] \Title{Lua} \startluacode context.MeasureBuffer({"222"}) context.getbuffer({"222"}) \stopluacode \xmlprocessbuffer{xml:name}{doc}{} \stoptext
Hi Jano, It's not clear to me what you want to measure when but here is a weekend playground (you're locked down anyway). \def\MacroJano#1% {\writestatus{JANO}{ONE} \setbox0=\hbox\bgroup\xmlflush{#1}\egroup} \startluacode function document.FunctionJano(id) context.writestatus("JANO","ZERO") token.expand_macro("MacroJano",true,id) context("%p",tex.box[0].width) context.writestatus("JANO","TWO") end \stopluacode \startbuffer[doc] <?xml version "1.0"?> <document> <section>one</section> <section>one two</section> <section>one two three</section> </document> \stopbuffer \startxmlsetups xml:name \xmlsetsetup{#1}{*}{xml:*} \stopxmlsetups \xmlregistersetup{xml:name} \startxmlsetups xml:section width: \ctxlua{document.FunctionJano("#1")}\par \stopxmlsetups \startxmlsetups xml:document \xmlflush{#1} \stopxmlsetups \starttext \xmlprocessbuffer{xml:name}{doc}{} \stoptext A clue: token.expand_macro will kind of 'run a local tex job'. If this works for you. you know the price you have to pay ... wikify 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 -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Jano Kula