[Tex/LaTex] \uline within a pgfmath expression produces an error

errorsexpansionpgfmath

Why does using \uline within a pgfmath expression result in an error?

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{tikz}

\begin{document}
\pgfmathparse{width("\uline{blabla}")}
\end{document}

The error:

! Undefined control sequence.
\ULset ->\UL@setULdepth \def \UL@leadtype 
                                          {\leaders \hrule \@height \UL@heig...
l.6 ...se{width("\let\uline\relax\uline{blabla}")}

! TeX capacity exceeded, sorry [input stack size=5000].
\UL@on #1->\leavevmode 
                       \UL@ender \let \UL@on \UL@onin \everymath {\UL@hrest ...
l.6 ...se{width("\let\uline\relax\uline{blabla}")}

!  ==> Fatal error occurred, no output PDF file produced!

My \uline is embedded in some other macro, so I can't simply not write it there. I had the idea to temporarily undefine \uline within the math expression (I don't need it to calculate the width, right!), but it only made matters more misterious: the error persists (it seems that \let is never executed).

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{tikz}

\begin{document}
\pgfmathparse{width("\let\uline\relax\uline{blabla}")}
\end{document}

Best Answer

It has all the signs of premature expansion, but the usual \protect methods don't seem to work here. You can make things safe by using a box register:

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{tikz}

\begin{document}
\sbox{0}{\uline{blabla}}
\typeout{\the\wd0}
\pgfmathparse{width("\usebox{0}")}
\typeout{\pgfmathresult}
\end{document}

runs without error and \pgfmathresult gets the right value:

26.66675pt
26.66675