ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Getting width of text to be typeset
@ 2020-05-22  9:22 context
  2020-05-22 22:03 ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: context @ 2020-05-22  9:22 UTC (permalink / raw)
  To: ConTeXt

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?

Best regards,

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

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

* Re: Getting width of text to be typeset
  2020-05-22  9:22 Getting width of text to be typeset context
@ 2020-05-22 22:03 ` Hans Hagen
  2020-05-22 22:18   ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2020-05-22 22:03 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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)

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
___________________________________________________________________________________

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

* Re: Getting width of text to be typeset
  2020-05-22 22:03 ` Hans Hagen
@ 2020-05-22 22:18   ` Hans Hagen
  2020-05-22 23:49     ` Rik Kabel
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2020-05-22 22:18 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________

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

* Re: Getting width of text to be typeset
  2020-05-22 22:18   ` Hans Hagen
@ 2020-05-22 23:49     ` Rik Kabel
  2020-05-23 11:04       ` Gerben Wierda
  2020-05-23 13:52       ` Rik Kabel
  0 siblings, 2 replies; 6+ messages in thread
From: Rik Kabel @ 2020-05-22 23:49 UTC (permalink / raw)
  To: ntg-context


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


On 5/22/2020 18:18, Hans Hagen wrote:
> 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
>
If the OP simply wants the width of a string, one can use 
\setwidthof#1\to#2.

    \define\String{pqrs}
    \setwidthof{\String}\to\Wdth
    \String\ is \Wdth\ wide.

    \setwidthof{{\tfb\em\String}}\to\Wdth
    {\tfb\em\String} is \Wdth\ wide.

I do not know if that is different from the width of the same string 
unboxed from the paragraph. There may be some adjustments made in 
justification, expansion, and such that are not treated.

-- 
Rik


[-- Attachment #1.2: Type: text/html, Size: 4291 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting width of text to be typeset
  2020-05-22 23:49     ` Rik Kabel
@ 2020-05-23 11:04       ` Gerben Wierda
  2020-05-23 13:52       ` Rik Kabel
  1 sibling, 0 replies; 6+ messages in thread
From: Gerben Wierda @ 2020-05-23 11:04 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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



> On 23 May 2020, at 01:49, Rik Kabel <ConTeXt@rik.users.panix.com> wrote:
> 
> If the OP simply wants the width of a string, one can use \setwidthof#1\to#2.
> 
> \define\String{pqrs}
> \setwidthof{\String}\to\Wdth
> \String\ is \Wdth\ wide.
> 
> \setwidthof{{\tfb\em\String}}\to\Wdth
> {\tfb\em\String} is \Wdth\ wide.
> 
> I do not know if that is different from the width of the same string unboxed from the paragraph. There may be some adjustments made in justification, expansion, and such that are not treated.

Additionally: suppose I am in lua. Would it be possible to get the width (and height) of a string back in lua as a value?

So, in lua something like this:

width = tex.widthofstring(“I have a string with some $\frac{1}2{}$ (math) in it")

G

[-- Attachment #1.2: Type: text/html, Size: 2990 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Getting width of text to be typeset
  2020-05-22 23:49     ` Rik Kabel
  2020-05-23 11:04       ` Gerben Wierda
@ 2020-05-23 13:52       ` Rik Kabel
  1 sibling, 0 replies; 6+ messages in thread
From: Rik Kabel @ 2020-05-23 13:52 UTC (permalink / raw)
  To: ntg-context


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


On 5/22/2020 19:49, Rik Kabel wrote:
>
> If the OP simply wants the width of a string, one can use 
> \setwidthof#1\to#2.
>
>     \define\String{pqrs}
>     \setwidthof{\String}\to\Wdth
>     \String\ is \Wdth\ wide.
>
>     \setwidthof{{\tfb\em\String}}\to\Wdth
>     {\tfb\em\String} is \Wdth\ wide.
>
> I do not know if that is different from the width of the same string 
> unboxed from the paragraph. There may be some adjustments made in 
> justification, expansion, and such that are not treated.
>
>
Correcting my post (thank you, Floris), the format is a bit different 
than I had written. The following works:

    \starttext
    \define\String{pqrs}
    \setwidthof\String\to\WdthA
    {\String} is \WdthA\ wide.

    \define\String{\tfx\em pqrs}
    \setwidthof\String\to\WdthB
    {\String} is \WdthB\ wide.

    \define\String{\ss pqrs}
    \setwidthof\String\to\WdthC
    {\String} is \WdthC\ wide.
    \stoptext


Giving:

-- 
Rik


[-- Attachment #1.2.1: Type: text/html, Size: 1931 bytes --]

[-- Attachment #1.2.2: janpnilaphcbkjac.png --]
[-- Type: image/png, Size: 14894 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2020-05-23 13:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-22  9:22 Getting width of text to be typeset context
2020-05-22 22:03 ` Hans Hagen
2020-05-22 22:18   ` Hans Hagen
2020-05-22 23:49     ` Rik Kabel
2020-05-23 11:04       ` Gerben Wierda
2020-05-23 13:52       ` Rik Kabel

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