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?

As a side comment, I've always felt helpless when it comes to latex. Say, for instance, I would like to know exactly what \unskip does, how would I go about doing that (google doesn't always work for this)? [I just made a CSE with several sites, hopefully that'll be useful]
It looks like there are a few lines in pgfmoduleshapes.code.tex that need %s after them, however adding the %'s didn't seem to help (there's no compilation step for latex, is there?) possibly because several of the commands it uses are also lacking %s.
– Greg Apr 08 '11 at 07:31Also, I just found out that code search indexes latex!
– Greg Apr 08 '11 at 07:38\unskipremoves the last skip caused by the space and is a TeX primitive, i.e. a low level command. The%s add the end are most likely the cause (as always). I might have a look on it when I find time. The image was done by opening the PDF in Acrobat Reader, zooming and doing a screenshot. I just Shutter under Ubuntu Linux which allows me to select a rectangle area. – Martin Scharrer Apr 08 '11 at 07:56