Take the 2-minute tour ×

Possible Duplicate:
Get width of a given text as length

In an attempt to Compute the x distance between two nodes, the question arose whether it is at all possible to devise A macro that expands to the length of its argument, or to any length. (The actual value or the way of obtaining it is irrelevant here.) Would it be necessary to generate a decimal representation of the length and parse it? How and why does the \widthof macro referred to in the linked question work?


  % Should expand to the natural width of the argument

    A test for the macro.

    A \parbox{\footextwidth{test}}{~} for the macro.
share|improve this question

marked as duplicate by krlmlr, Kurt, Werner, Andrew Swann, Martin Schröder Jan 9 '13 at 7:12


Or use \newcommand{\footextwidth}[1]{\setbox0=\hbox{#1}\the\wd0} –  Marco Daniel Jul 19 '12 at 18:04
The direct answer to the question is no - you can't have a macro which expands to the length of some text, because you absolutely need to assign a box inbetween. The nearest you can get is the \setlength approach used below. While doing an assignment anyway, you can as well assign a box inbetween, which is what \widthof does. –  Stephan Lehmke Jul 19 '12 at 18:22
@StephanLehmke: Then perhaps expands is the wrong word for this. I want to use an invocation of this macro in place of an actual length. Have to look at what Marco provided. –  krlmlr Jul 19 '12 at 18:24
@MarcoDaniel: Care to write this as an answer? –  krlmlr Jul 19 '12 at 18:25
@MarcoDaniel Did you try that? I don't think it works. –  Stephan Lehmke Jul 19 '12 at 18:43

1 Answer 1

up vote 3 down vote accepted

With \usepackage{calc} in your preamble, you could use \parbox{\widthof{test}}. The \wdithof{<text>} returns the width of the given <text>.

A better alternative for this is hphantom{test} which will take up as much horizontal space as required by test. There is a corresponding vphantom{} for vertical space, and \phantom{}.

If you want to store the width for latter use you can use:


and then the value of \MyLength can be used whereever a lenght is required. Int he last example below, the space is prodced by \hspace{\MyLength}.

enter image description here






\begin{tabular}{l l}
    A test for the macro.                           & no spacing\\
    A \parbox{\MyFixedWidth}{~} for the macro. & use length \verb|\MyFixedWidth| = \MyFixedWidth\\
    A \parbox{\widthof{test}}{~} for the macro.     & use \verb|\widthof{test}|\\
    A \hphantom{test} for the macro.                & use \verb|\hphantom{test}|\\
    A \hspace{\MyLength} for the macro.             & use \verb|\hspace{\MyLength}|\\
share|improve this answer
Right. I'm more interested in how you would code such a beast. –  krlmlr Jul 19 '12 at 18:05
@user946850: Not sure I understand what you want to code. –  Peter Grill Jul 19 '12 at 18:08
I mean, just like \widthof -- a macro that "prints" the natural length of the argument so that it can be "processed" by a command that requires a dimension. I understand that \widthof is a command which is defined only in, e.g., \parbox by the calc package, and that it just doesn't seem to be possible to implement a generic \widthof which always can be used directly (that's what I was interested in). –  krlmlr Jan 9 '13 at 1:19
Still not understanding. You want to implement \widthof, then just copy the definition of \widthof? Perhaps an actual example usage is required to fully understand what you desire. –  Peter Grill Jan 9 '13 at 3:16
No. I want to use \widthof{} where a dimension is required. Other questions and answers seem to indicate that this won't work and that it's impossible to implement such a macro. –  krlmlr Jan 9 '13 at 8:06
