Horizontal boxes in TeX are often padded to the right by trailing pieces of glue and kerns. As such, their width returned by \wd is larger than what is actually printed on the page. I am interested in the following problem:
Given a horizontal box, is there a way to remove (arbitrarily many consecutive) trailing pieces of glue, kerns, and whatsits from the box, while preserving the glue set ratio for all other pieces of glue in the box?
I am interested also in the case where we merely have the width of the resulting box (I will call this the real width of the original box), which would suffice for most applications. For example, if I have the box
\hbox to20pt{a\hskip0pt plus10pt a\hskip0pt plus10pt} Where both glues of natural width 0pt are set at a glue set ratio of +0.8 (ie. their actual width comes out to be 20pt). I wish to remove the latter piece of glue without affecting the first. A picture for reference:
Of course, the naïve solution of \unhboxing and using \unskip immediately fails by destroying any glue set information. Not to mention \unskip and friends cannot deal with arbitrarily many glue, kern, or whatsit items.
I suspect a solution for the general case in classical (Knuth or pdf-) TeX is impossible. However, a LuaTeX solution seems much more possible, given the existence of packages like lua-visual-debug which are far more advanced. This would be a great example to showcase the power of LuaTeX to access TeX internals.
There is a related problem that I am also interested in. Namely, the width of a vertical list is normally computed as the maximum of the widths of individual boxes in the list (ignoring rules, \moverights, etc. for now), but it would be useful to have this as the maximum of the real widths instead.
It should be noted that even if we have an algorithm to compute the real width of arbitrary hboxes, it still does not suffice for this problem, since there are a myriad of issues with traversing vertical lists in classical TeX. Again, I expect LuaTeX will be able to handle this with much less difficulty.




\framedis just a fancy\hbox; I don't think that it shrinks itself to the size of the content while ignoring trailing whitespace. There might be some key/setting that makes it do this that I'm not aware of though.|with newlines for readability):\let|=\relax | \define\content{\llap{\the\hsize\ }a\hskip 0pt plus 10pt a\hskip 0pt plus 10pt} | \setupframed[offset=overlay] | \startTEXpage[offset=1ex, loffset=\widthofstring{000.00000pt}] | \framed{\hbox to 20pt{\content}} | \framed[width=20pt, align=disable]{\content} | \framed[width=20pt, align=disable, autowidth=force]{\content} | \framed[width=fit]{\content} | \stopTEXpage