ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Getting width of text to be typeset
Date: Sat, 23 May 2020 00:18:55 +0200	[thread overview]
Message-ID: <639ff2c9-65f6-b818-1d8f-9249331a9b8a@xs4all.nl> (raw)
In-Reply-To: <e7ebbe94-0dbf-5113-8dc2-9af6cc6a8d82@xs4all.nl>

On 5/23/2020 12:03 AM, Hans Hagen wrote:
> On 5/22/2020 11:22 AM, context@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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

  reply	other threads:[~2020-05-22 22:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22  9:22 context
2020-05-22 22:03 ` Hans Hagen
2020-05-22 22:18   ` Hans Hagen [this message]
2020-05-22 23:49     ` Rik Kabel
2020-05-23 11:04       ` Gerben Wierda
2020-05-23 13:52       ` Rik Kabel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=639ff2c9-65f6-b818-1d8f-9249331a9b8a@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).