# What is the plain TeX equivalent of \settowidth?

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?

-

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

-
In the middle line of the three-line code snippet, should that be \wd rather than \ht? By the way, is \@tempboxa defined in Plain-TeX? (If not, one can just create a new "scratch" box with that name, right?) Separately, it may be useful to remind readers that they'll need to "wrap" these instructions in the usual \catcode@=11 ... \catcode@=12 pair. –  Mico Apr 22 '12 at 11:30
You're right about \ht that should be \wd; the second part is just to show the working of the macros, not actual input. –  egreg Apr 22 '12 at 12:19

You can set your wanted content into a box with \setbox

\setbox0\hbox{some horizontal material}


where 0 is a “scratch” box register, or you can assign a new box register number with \newbox

\newbox\mybox


after which you can use that, as in

\setbox\mybox\vbox{some vertical material}


Now you can ask for its dimensions with

\the\ht\mybox for its height,
\the\wd\mybox for its width, and
\the\dp\mybox for its depth


(where the prefix \the typesets the quantity)

\newbox\mybox
\newdimen\mydim
\def\setMyDimToMyBoxWidth#1{%
\setbox\mybox\hbox{#1}%
\global\mydim=\wd\mybox% global so the dim can be used outside
\leavevmode% in case it's the first thing in a paragraph
\box\mybox% typeset the contents of the box
}

Some text \setMyDimToMyBoxWidth{some horizontal material} more text
now, the width is \hbox to\mydim{\hfil\the\mydim}.
\bye

-