Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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 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 at 15:47

2 Answers 2

up vote 6 down vote accepted

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

\documentclass{article}
\usepackage{calc}
\newcommand{\printtowidth}[2]{%
  \makebox[\widthof{#1}]{#2}%
}

\begin{document}
Some words before abc def ghi and some words after.

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

enter image description here

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

share|improve this answer
    
Does the trick, cheers! –  mab Jul 27 at 16:26

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:

    \documentclass[12pt]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{lipsum}
    \usepackage{makebox}
    \usepackage{eqparbox}
    \setlength\fboxsep{12pt}

    \newcommand\xmakebox[2]{\makebox*{#1}{\vphantom{#1}#2}}

    \begin{document}

    \begin{tabular}{c}
    \eqframebox[boxa]{Blahblah}\\\\
    \eqframebox[boxa]{Blahblahblah, blahblahblah, blahblahblah…}\\\\
    \fbox{\parbox{\eqboxwidth{boxa}}{\lipsum[2]}}
    \end{tabular}
    \vskip 0.5cm
    \setlength{\fboxsep}{0pt}

    \noindent\fbox{\makebox*{Blahblahblah, blahblahblah, blahblahblah…}{\begin{tabular}{|l|}Is that OK?\\Is that OK?\\Is that OK?
    \end{tabular}}}\\[0.5cm]
    \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?
    \end{tabular}

    \end{document} 

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}}{…}.

share|improve this answer
    
This would solve my example application, but I would still like some \phantomwrapper-like function. –  mab Jul 27 at 16:25
    
@mab: The wrapper already exists with the makebox package. See my updated answer. –  Bernard Jul 27 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 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 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 at 22:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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