PGF/TikZ takes care that non-drawing material inside its picture environments is not typeset. The whole environment is stored in a box which is later discarded while (IMHO) the drawing commands draw in a different box which is then typeset.
This alone wouldn't be an issue for your code, but PGF/TikZ also sets the font to \nullfont as well redefines \selectfont to suppress font changes. Only for node contents and other "official" text like labels the font is restored.
Therefore the text in your box can't be typeset and the box is empty. A trace with \tracingall gives you the following messages:
{\setbox}
{the letter t}
Missing character: There is no t in font nullfont!
{the letter h}
Missing character: There is no h in font nullfont!
{the letter i}
...
You need to either set the box outside the picture environment or restore the font. You can use the pgfinterruptpicture environment to escape to a normal typesetting state with font.
\documentclass{article}
\usepackage{tikz}
\newbox\mybox
\begin{document}
\begin{tikzpicture}
\setbox\mybox=\hbox{\pgfinterruptpicture this is a text that I want to measure\endpgfinterruptpicture}
\node[draw] at (0,0) {text width is \the\wd\mybox, text heigh is \the\ht\mybox};
\node[draw] at (0,-1) {text contents is \box\mybox};
\end{tikzpicture}
\end{document}
Alternatively you could restore \selectfont and the font manually just for the box content:
\documentclass{article}
\usepackage{tikz}
\newbox\mybox
\let\origselectfont\selectfont
\newcommand{\restorefont}{%
\let\selectfont\origselectfont
\normalfont
}
\begin{document}
\begin{tikzpicture}
\setbox\mybox=\hbox{\restorefont this is a text that I want to measure}
\node[draw] at (0,0) {text width is \the\wd\mybox, text heigh is \the\ht\mybox};
\node[draw] at (0,-1) {text contents is \box\mybox};
\end{tikzpicture}
\end{document}
Some other notes:
- You shouldn't use plain numbers for boxes. Box #37 might be used by some other code. My first guess was that TikZ uses it and sets it to be empty :-)
- AFAIK with the
\setbox primitive there might be issues with color changes. Use \sbox\mybox{<content>} instead which handles them.
\global\setboxbut didn't know why it was necessary to do so. – Gonzalo Medina May 19 '11 at 14:59pgfinterruptpictureworks for me. – John Kirollos May 19 '11 at 16:09{pgfinterruptpicture}is intended for use inside a box. If you put it there, you no longer need to use\global(I tested this and it works). – Ryan Reich Jun 21 '11 at 17:23