I use mathpazo font package and I think the hat symbol is too low. Can I make a macro to change behavior of all my hats?
This question has a good example, but I don't want to have to retype all of my hats:
I use mathpazo font package and I think the hat symbol is too low. Can I make a macro to change behavior of all my hats?
This question has a good example, but I don't want to have to retype all of my hats:
Here I use stackengine to add a stacking gap on the argument of \hat. I also use scalerel package to preserve the math style, and to express the stacking gap, .3\LMpt, in terms of a unit that scales with the math style (as an argument to \ThisStyle{}, \LMpt is a scalable version of 1pt, that will scale with smaller math styles).
The hat kerning is lost, when I wrap the argument in an \addstackgap, so I re-add kerning to the hat, proportional to the height of the glyph, using \slantfactor = .2.
\documentclass{article}
\usepackage{mathpazo}
\usepackage{stackengine}
\usepackage{scalerel}
\def\slantfactor{.2}
\let\svhat\hat
\def\hat#1{%
\ThisStyle{\setbox0=\hbox{$\SavedStyle#1$}\kern\slantfactor\ht0%
\svhat{\addstackgap[.3\LMpt]{$\kern-\slantfactor\ht0\SavedStyle#1$}}}}
\begin{document}
$\svhat{x}\svhat{P}$ (original)
$\hat{x} \hat{P} \scriptstyle \hat{x} \hat{P} \scriptscriptstyle \hat{x} \hat{P}$
(revised)
\end{document}

Instead of using the mathpazo package, which is (a) quite old and (b) well known for having various font metric problems, you could use the newpxtext and newpxmath packages. These are derived from the mathpazo package but have much better font metrics. In particular, these packages produce well-spaced hat symbols. If you use these packages instead of mathpazo -- and I can honestly not think of a good reason for continuing to use mathpazo if newpxtext and newpxmath are available on your system -- there's simply no need to manually correct/improve the spacing of the stacked hat symbol.

\documentclass{article}
\usepackage{newpxmath,newpxtext}
\begin{document}
$\hat{u}$ $\hat{U}$
\end{document}
For comparison, here are the same two symbols with the mathpazo package loaded instead of newpxtext and newpxmath:
