I'm not sure what you're doing to get
\hat to extend—it doesn't for me—but the following sort of works. I'm not totally happy with the spacing. Note that I used
\overbrace instead of
\hat since it actually does extend.
The braces don't seem to line up in the two cases. If you make the vinculum (the fraction bar) larger than 0pt, you get a line, of course, but then the braces line up. Try
to see what I mean.
I'm leaving my old solution because I suspect that with some modifications, someone can make it work. Here's my current solution.
\dimen0 \wd\ifdim\wd0<\wd2 2\else0\fi
It has one problem, which I hope is minor enough that you can use it. The first is that the numerator and denominator are typeset with a
\strut for spacing. What should happen instead is the numerator (resp. denominator) should be moved up (resp. down) some amount dependent on the font and its height+depth. In principle, this is doable, but I'd need to spend a lot more time reading Appendix G of the TeXbook.
EDIT: There was an additional problem regarding cramped math styles that has been resolved thanks to this answer. You need the
mathtools package for the
\cramped macro. Actually, a similar issue exists with the numerator. The denominator should always be cramped, so using
\cramped we can solve it. The numerator should be cramped only when the entire fraction is cramped. I don't know how to test if the current style is cramped or not.