It would be very handy for me if I could write something like:

\phantomwrapper{long word or text that fits in one line}{shorter text}

and the phantomwrapper would create just the amount of whitespace around its second argument so that result needs as much horizontal space as its first argument. An example application are tikz nodes that I want to draw boxes around with fit and I want to keep all boxes the same size.

  • 1
    How should the second argument be placed within the "box" with width given by the first argument: flush-left, centered, or flush-right? What should be done if the "box" defined by the first argument is too long for the available space (say, the current line)? – Mico Jul 27 '14 at 15:42
  • @Mico, good points. I guess the alignment in the box could be a further parameter, I was thinking of centered. The other question is tough, throwing an error is not very elegant is it? Can't think of a good solution either, maybe that is why it doesn't exist? – mab Jul 27 '14 at 15:47

The eqparbox package is perhaps what you're after: it defines variants of the usual box commands, eqparbox, eqmakebox, eqframebox, that accept a tag instead of the width parameter. All boxes with the same tag will have the width of the longest text, and an eqboxwidth{tag} length is defined and can be used in defining the width of another box.

If one wants to define a box of the width and height of something else, I define a xmakebox macro, based on the very small makebox backage. Of course it supposes that width and height of the shorter text are indeed less than those of the longer text. Note that the shorter text is not necessarily a one-lined text. Here is a short demo:




    \eqframebox[boxa]{Blahblahblah, blahblahblah, blahblahblah…}\\\\
    \vskip 0.5cm

    \noindent\fbox{\makebox*{Blahblahblah, blahblahblah, blahblahblah…}{\begin{tabular}{|l|}Is that OK?\\Is that OK?\\Is that OK?
    \fbox{Blahblahblah, blahblahblah, blahblahblah…}\\[0.5cm]
    \fbox{\xmakebox{\begin{tabular}{@{}l@{}}Blahblahblah, blahblahblah, blahblahblah…\\Is that OK?\\Is that OK?\\Is that OK?\\Is that OK?
    \end{tabular}}{Blahblahblah}}\qquad\begin{tabular}{@{}l@{}}Blahblahblah, blahblahblah, blahblahblah…\\Is that OK?\\Is that OK?\\Is that OK?\\Is that OK?


enter image description here

Added: it seems to give a partial solution to @Mico's second question: if the remaining space on the current line isn't enough, you can reduce the width of the box by writing, for instance: \parbox{0.5\eqboxwidth{tag}}{…}.

  • This would solve my example application, but I would still like some \phantomwrapper-like function. – mab Jul 27 '14 at 16:25
  • @mab: The wrapper already exists with the makebox package. See my updated answer. – Bernard Jul 27 '14 at 17:28
  • thanks, does \makebox also adjust the height at the same time? I upvoted your answer before already, but now its a at least a good alternative to egreg's answer. – mab Jul 27 '14 at 18:35
  • I don't know what you mean exactly with adjusting the height. It makes no scaling, only defines the width of the box that will contain its second argument. If you mean the shorter text can be several lines long, see my updated answer. – Bernard Jul 27 '14 at 18:55
  • 1
    I added to my answer a very simple \xmakebox macro that also takes into account the height of the first argument. – Bernard Jul 27 '14 at 22:41

If you don't need to cope with line breaks, it's just an application of the calc package:


Some words before abc def ghi and some words after.

Some words before \printtowidth{abc def ghi}{xyz} and some words after.

enter image description here

Line breaks would pose the problem of where to print the inside text.

  • Does the trick, cheers! – mab Jul 27 '14 at 16:26
  • This does now not work anymore within tikzpicture. I think it is the \widthof command. The \xmakebox command by @Bernard below using the makebox package still works. – mab Jun 28 '16 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.