# Macro for moving hat symbol up

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:

Can I move the hat symbol vertically upwards?

-
possible duplicate of Can I move the hat symbol vertically upwards? – Harish Kumar Jul 25 '14 at 0:32

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}


-
great answer, I don't have stackengine.sty on my windows latex install and installing packages manually seems to be quite a headache that I have yet to try (tex.stackexchange.com/questions/2063/…). Any solution that does not require stackengine? – bdeonovic Jul 24 '14 at 16:12
@Benjamin The package can be downloaded from ctan.org/tex-archive/macros/latex/contrib/stackengine. If you just place the .sty file in your working directory, all should work. – Steven B. Segletes Jul 24 '14 at 16:24

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:

-
This is excellent, thank you! I accepted @Steven's answer since he directly answered my question (for anyone in the future looking to change hat height, regardless of font used). I will probably use this solution though. – bdeonovic Jul 24 '14 at 16:21