# Tikz: Using \pgfmathwidth (or \widthof) with \Large text causes crash

I;m trying to utilise this method for putting a box around text: http://tex.stackexchange.com/a/20326/17049

But it doesn't work with large text...

MWE:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\pgfmathwidth{\Large "Text"}
\end{tikzpicture}
\end{document}


Causes:

! TeX capacity exceeded, sorry [input stack size=5000].
\@nomath ...e \@font@warning {Command \noexpand #1
invalid in math mode}\fi
l.6     \pgfmathwidth{\Large "Text"}

-
From the PGF Manual: "It is important to remember that any expression is expanded with \edef before being parsed, so any macros (e.g., font commands like \tt or \Huge) will need to be \protected" (e.g., \noexpand\Huge is usually sufficient)." – Herr K. Jan 23 '14 at 17:55
@KevinC With \pgfmathwidth{"\noexpand\Large Text"}, \pgfmathresult is set to 0pt. On the other hand, \pgfmathwidth{\noexpand\Large "Text"} gives errors. – egreg Jan 23 '14 at 18:01
@egreg: That's strange... I get the correct measures with your first syntax. See my answer below. – Herr K. Jan 23 '14 at 18:04

From the PGF Manual:

It is important to remember that any expression is expanded with \edef before being parsed, so any macros (e.g., font commands like \tt or \Huge) will need to be "protected" (e.g., \noexpand\Huge is usually sufficient)."

# MWE

\documentclass{article}
\usepackage{tikz}
\setlength\parskip{20pt}
\begin{document}
Width of text: \pgfmathwidth{"Text"}\pgfmathresult

Width of {\Large Text}: \pgfmathwidth{"\noexpand\Large Text"}\pgfmathresult

Width of {\Huge Text}: \pgfmathwidth{"\noexpand\Huge Text"}\pgfmathresult
\end{document}


# Output

-
Did you try it inside a tikzpicture environment? – egreg Jan 23 '14 at 18:03
@egreg: You're right, \pgfmathwidth{"\noexpand\Large Text"} doesn't work inside a tikzpicture environment, except when issued in a \node. Could this be a bug? I'm still using PGF 2.10 though. – Herr K. Jan 23 '14 at 18:14
you need two more \noexpand to make it work. there must be a duplicate for this somewhere – percusse Jan 23 '14 at 18:25
@percusse: \pgfmathwidth{"\noexpand\noexpand\noexpand\Large Text"} gives a result of 20.00003, which is the same as \pgfmathwidth{"Text"}. But this is still incorrect :( – Herr K. Jan 23 '14 at 18:34