there is a problem I have been trying to solve for a while but without success so far. As far as I know, TeX is very good at appropriately breaking text lines with respect to a fixed container width. What I am trying to achieve is different: I would like to be able to compute an "optimal" width for a given content.
By optimal, I mean that there is an underlying constraint. For instance, I would like to be able to write a command
\settooptimalwidth#1#2#3 which takes:
- #1 as a length to adjust
- #2 as a maximum width
- #3 as a content
If #3 is very short (shorter than #2), it would simply do the same as
\settowidth. If #3 is longer than #2, it would break #3 into lines as if it was put in a minibox, and (that's the tricky part) return the length of the widest lines amongst those. I'm assuming here that the line breaking algorithm is not using the usual "justified" align, but instead raggedleft/right or centering (otherwise the problem is irrelevant).
Let me give a few examples where such a command would be useful: * auto-adjusting the width of an array column whose content is to be centered, with potential line breaking, * some kind of right-aligned verse environment with a fancy frame. If the verse is very short then the frame would be small. If the verse is very long, it would break the text to an "optimal" width.
I think that the way to go is with
\unhbox, but I'm not sure how to use it. Any help would be appreciated.