[NTG-context] Getting width of text to be typeset

Hans Hagen j.hagen at xs4all.nl
Sat May 23 00:18:55 CEST 2020


On 5/23/2020 12:03 AM, Hans Hagen wrote:
> On 5/22/2020 11:22 AM, context at vivaldi.net wrote:
>> Hello,
>>
>> is it possible to get somehow the width of the "material" (box?) of 
>> the current line which is "ready" to be typeset?
>>
>> See the case:
>>
>> ----
>> \starttext
>>
>>    some text
>>
>>    pqrs % Here I need to get width (or content) of the text from the 
>> begin of the current line,
>>         % i.e. width of the text "pqrs".
>>         % (Depending of the width I will decide what to do later.)
>>
>> \stoptext
>> ----
>>
>> I am too laical to know how to "inject" TeX workflow or whether to 
>> access LuaTeX internals (nodes?) to get the desired information.
>>
>> - Is it possible somehow?
> Too easy ...
> 
> \startluacode
>      function document.whatever()
>          context(nodes.hpack(tex.getnest().head.next).width)
>      end
> \stopluacode
> 
> \unexpanded\def\widthuptohere{\dimexpr\ctxlua{document.whatever()}sp\relax}
> 
> \starttext
> 
>      \dorecurse {10} {
>          snippet #1\scratchdimen\widthuptohere\ has \the\scratchdimen\ 
> width\par
>      }
> 
> \stoptext
> 
> but still you have to wikify it ... maybe i'll make it a low level 
> helper (but than you also need to wikify that because i have no clue 
> where to explain it)
Actually, one needs to flush a bit

\startluacode
     function document.whatever()
         local h = nodes.hpack(tex.getnest().head.next)
         local w = h.width
         h.list = nil
         nodes.free(h)
         context(w)
     end
\stopluacode

\unexpanded\def\widthuptohere{\dimexpr\ctxlua{document.whatever()}sp\relax}

\starttext

     \dorecurse {10} {
         snippet #1\scratchdimen\widthuptohere\ has \the\scratchdimen\ 
width\par
     }

\stoptext

not that it matters much, because it's unlikely that you leak more than 
a dozen nodes in a run.

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


More information about the ntg-context mailing list