3

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?

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand{\footextwidth}[1]{%
  % Should expand to the natural width of the argument
  1cm
}

\begin{document}
    A test for the macro.

    A \parbox{\footextwidth{test}}{~} for the macro.
\end{document}
krlmlr
  • 12,530
  • 2
    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
  • 1
    @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
  • @user946850 The answer is still more or less no. The \setlength trick is more or less the only solution. – Stephan Lehmke Jul 19 '12 at 18:46
  • \widthof (or the \wd primitive) are for reporting the length of a box but there are no boxes between Tikz nodes to measure. As far as I understand it (having not used the system) Tikz nodes (like latex picture mode) are already positioned at specific coordinates, so you just need to ask tikz what those cordinates are and then use pythagoras. – David Carlisle Jul 19 '12 at 20:22
  • @DavidCarlisle: In fact, as seen in the answers to the linked question, it is possible to compute the distance between two nodes, even as a dimension. What I am looking for is how to write a macro that evaluates to this dimension. – krlmlr Jul 20 '12 at 01:39
  • Another direct answer to your question: Your original version of \footextwidth in the MWE is a macro which expands to a length. You really must say precisely what you are asking in this question. – Stephan Lehmke Jul 20 '12 at 05:00
  • @user946850 since (in a TeX context) neither compute nor evaluate are defined terms you will need to explain what you mean by the difference between compute and evaluate – David Carlisle Jul 20 '12 at 07:30

1 Answers1

3

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:

 \newlength{\MyLength}
 \settowidth{\MyLength}{<text>}         

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

Notes:

Code:

\documentclass{article}
\usepackage{calc}

\newcommand{\MyFixedWidth}{1cm}

\newlength{\MyLength}

\begin{document} \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}|\ \settowidth{\MyLength}{test}%
A \hspace{\MyLength} for the macro. & use \verb|\hspace{\MyLength}|\ \end{tabular} \end{document}

Peter Grill
  • 223,288
  • 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 09 '13 at 01: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 09 '13 at 03: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 09 '13 at 08:06