[Tex/LaTex] the plain TeX equivalent of \settowidth

dimensionslengthstex-core

The \settowidth command, which sets a width equal to the width of some text, is described at Get width of a given text as length and How to set the width of the label of a description to the width of a string of text in ConTeXt?. What is the plain TeX equivalent of this command?

Best Answer

There is none other than doing the same thing by copying the LaTeX definition:

\catcode`@=11
\newbox\@tempboxa
\def\@settodim#1#2#3{\setbox\@tempboxa\hbox{{#3}}#2#1\@tempboxa
   \setbox\@tempboxa\box\voidb@x}
\def\settoheight{\@settodim\ht}
\def\settodepth {\@settodim\dp}
\def\settowidth {\@settodim\wd}
\catcode`@=12

Now \settowidth\mylen{xyz} would become

\@settodim\wd\mylen{xyz}

so transformed into

\setbox\@tempboxa\hbox{{xyz}}
\mylen\wd\@tempboxa
\setbox\@tempboxa\box\voidb@x

and the trick is done, assuming you already allocated

\newdimen\mylen
Related Question