I would like to be able to scale text to the width of a TikZ node.
Using \widthof within tikzpicture as a starting point, I've arrived at somewhat of a solution, except it has incorrect spacing, and can probably be done more elegantly.
\documentclass{article}
\usepackage{tikz}
\usepackage{calc}
\usepackage{xstring}
\usepackage{ifthen}
\newlength{\scaleratio}
\makeatletter
\newcommand{\settowidthofnode}[2] {%
\pgfextractx{#1}{\pgfpointanchor{#2}{east}}%
\pgfextractx{\pgf@xa}{\pgfpointanchor{#2}{west}}%
\addtolength{#1}{-\pgf@xa}%
}%
\makeatother
\makeatletter
\newcommand{\shrinktowidthofnode}[2] {%
\settowidthofnode{\pgf@xb}{#2}%
\setlength{\scaleratio} {%
{1.0pt * \ratio{\pgf@xb}{\widthof{#1}}}%
}%
\ifthenelse{\lengthtest{\scaleratio < 1.0pt}} {%
\setlength{\scaleratio}{\scaleratio}%
\tokenize{\tokenized}{\the\scaleratio}%
\StrBefore{\tokenized}{pt}[\result]%
\scalebox{\result}{#1}%
} {%
#1%
}%
}%
\makeatother
\begin{document}
\begin{tikzpicture}[every node/.style={draw,rectangle}]
\draw[step=0.5cm,gray,very thin] (-3,-3) grid (3,3);
\node (n) {blah};
\node (m) [below of=n] {\shrinktowidthofnode{AAAAAAAA}{n}};
\node (o) [below of=m] {\scalebox{0.43259}{AAAAAAAA}};
\end{tikzpicture}
\end{document}
The output looks like this:

It's hard to see, but if you zoom in, the middle node's bounding box is larger than it should be, and there's some extra space on its left (there was more extra space earlier, but for some reason adding % to the end of each line in the \newcommands helped).
The bottom node is the ground truth. Any ideas?
