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.

  • 3
    I think you are describing the varwidth environment from the package of that name Apr 18, 2015 at 21:16

1 Answer 1


As David Carlisle wrote above, the package varwidth is the way to go. It provides the environment varwidth which does what I was asking for. Here is a minimal example to draw text on a gradient shaded background using beamer.




  \advance\shadeboxwidth by 2\shadeboxsep%
  \advance\shadeboxheight by 2\shadeboxsep%

\strut\hfill\shadebox{titleshadeA}{titleshadeB}{1ex}{\raggedleft\LARGE Something short}\\
\strut\hfill\shadebox{titleshadeA}{titleshadeB}{1ex}{\raggedleft\LARGE Something very long which will be broken and aligned to the right}\bigskip
\hrule height 2pt\bigskip
Notice how it doesnt spawn the total text width (see the black rule above), but instead adjusts itself to the longest text line of the content.
  • It would be more courteous to ask DC to write an answer rather than taking credit for his comment! (It would be different if you posted code demonstrating the solution - but there is nothing here not in that comment.)
    – cfr
    Apr 18, 2015 at 21:57
  • 1
    @cfr i can probably survive without the rep:-) Vincent, to close the loop you should accept your own answer. Apr 18, 2015 at 22:17
  • Oops, my apologies, David. That was my very first post here and I did not realize one get credit for providing useful answers. If you want me to delete mine and provide your own, please tell me. Apr 18, 2015 at 23:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .