Function determining the length of text, usable where something with internal units (e.g. "em") is expected

Hmm arguably this is a calc and/or LaTeX bug.

using calc and \widthof would work in most of the places you mention (as in LaTeX2e such places explicitly changed to use \setlength internally so that they would work with calc. However \hspace appears to have escaped that conversion, the following patch fixes that.....

\documentclass{article}

\usepackage{calc} 

\makeatletter
\def\@hspace#1{\begingroup\setlength\dimen@{#1}\hskip\dimen@\endgroup}
\makeatother

\begin{document}

 a\hspace{\widthof{some text}}b 

\end{document}

The above answers the original question, but the later edits have clarified that this is not needed at all. \widthof is a relatively expensive operation and doing every row just to make space to insert the same text you measured is really just torturing your computer for no real gain. The formulation in the second table matches the version you gave in the first without the over full boxes and without measuring anything.

enter image description here

\documentclass{article}
\usepackage{fixltx2e}
\usepackage{calc}

\begin{document}

\begin{tabular}{p{0em}@{\hspace{1.0em}\quad}l@{\qquad}l}
  \(\bullet\) & \(x = y\) & \(z = w\) \\
  \(\bullet\) & \(a = b\) & \(c = d\) \\
\end{tabular}

\begin{tabular}{@{\hspace\tabcolsep\rlap{$\bullet$}\hspace{1.0em}\quad}l@{\qquad}l}
   \(x = y\) & \(z = w\) \\
   \(a = b\) & \(c = d\) \\
\end{tabular}

\end{document}

calc patches some of the main LaTeX constructs so that an extended syntax is accepted. However, it doesn't do the same for \hspace.

The command \widthof can be used in \setlength and in the argument to \parbox, minipage and also in the p-column specifier.

You can get a version of \hspace that works with \widthof, but I wouldn't use it as a replacement.

\documentclass{article}
\usepackage{xparse,calc}
\newlength\Hspacelen
\NewDocumentCommand{\Hspace}{sm}
 {\setlength\Hspacelen{#2}%
  \IfBooleanTF{#1}
    {\hspace*{\Hspacelen}}
    {\hspace{\Hspacelen}}%
 }

\makeatletter
%\def\Hspace#1#{\@Hspace{#1}}
%\def\@Hspace#1#2{\setlength\@tempdima{#2}\hspace#1{\@tempdima}}
\makeatother

\begin{document}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

a\Hspace{\widthof{text}}b

\Hspace*{\widthof{text}}ab
\end{document}

The commented version is a faster alternative.

However, since you probably don't want a space that disappears at page breaks, the faster method is to say

\leavevmode\hphantom{text}

Don't forget \leavevmode at the start of a paragraph; it's irrelevant if \hphantom is in the middle of a paragraph.


Does \wd works ?

E.g:

\newsavebox{\mymeasure} % only 1 time, in the preamble

Then:

\sbox{\mymeasure}{this particular piece of text}
X\hspace{\wd\mymeasure}X

So, to have just 1 command, in your preamble:

\newsavebox{\mymeasure}
\newcommand{\measure}[1]{\sbox{\mymeasure}{#1}\wd\mymeasure}

Usage:

X\hspace{\measure{this particular piece of texte}}X

Tags:

Boxes

Lengths