The answer is very font dependent, and I can't access your particular font for which the problem arises. But it seems to me that the kind of kerning you seek almost needs to be reset for each letter. If one were prepared to do that once for an oft-used font, here might be a way.
REVISED SOLUTION (SLOWER, BUT HANDLES \scriptsize
, \scriptscriptsize
):
Lines beginning with %%%% indicate measures you can alter to conform to your particular font. First I create a stacked dot arrangement (\talldot
), merely to have something the vertical height of the desired hat, and which will change size with the math style. I also create \hatglyphCONTENT
which is a box containing a cropped \hat
. I use that box and scale it to the size of \talldot
, designating the result \hatglyph
. Note that, since \talldot
scales with the math style, so will \hatglyph
.
The macro \shifthat
stacks the scaled hat over the desired letter in the appropriate math style, employing a horizontal shift of the hat that was passed as an argument.
The macro \newhat
is where you define the horizontal shift of the hat for each individual glyph, relative to the centered (non-italic) configuration. Rather than specifying that shift in absolute measure, it is given as a multiplier on \glyphwidth
which is 1ex
, measured at the appropriate math style.
One of the keys to this solution is scalerel
package's \ThisStyle{...\SavedStyle...}
syntax, which imports (via \SavedStyle
) the math style at invocation to places where that math style is otherwise lost, for example inside boxes or as arguments to other macros.
\documentclass{article}
\usepackage{stackengine}
\usepackage{scalerel}
\usepackage{verbatimbox}
\newlength\glyphwidth
\newcommand\talldot{%
\ThisStyle{%
%%%% 1ST ARGUMENT OF \stackengine (GAP BETWEEN STACKED DOTS)
\stackengine{.0ex}{$\SavedStyle.$}{$\SavedStyle.$}{O}{c}{F}{F}{S}%
}}
\newsavebox\hatglyphCONTENT
\sbox\hatglyphCONTENT{%
%%%% 1ST OPTIONAL ARGUMENT OF \addvbuffer (CROP OFF TOP OF STACKED hat)
%%%% 2ND OPTIONAL ARGUMENT OF \addvbuffer (CROP OFF BOTTOM OF STACKED hat)
\addvbuffer[-0.05ex -1ex]{%
\stackengine{-.1ex}{ }%
{\(\hat{}\)}{O}{c}{F}{F}{L}%
}}
\newcommand\hatglyph{\ThisStyle{\scalerel*{\usebox{\hatglyphCONTENT}}%
{\SavedStyle\talldot}}}
\newcommand\shifthat[2]{%
\ThisStyle{%
%%%% 1ST ARGUMENT OF \stackengine (GAP BETWEEN GLYPH AND \hatglyph)
\stackengine{0.0ex}{\(\SavedStyle#2\)}%
{\(\rule{#1}{0ex}\SavedStyle\hatglyph\)}{O}{c}{F}{T}{S}%
}%
}
\newcommand\newhat[1]{%
\ThisStyle{\setlength\glyphwidth{\widthof{$\SavedStyle x$}}%
\if A#1\shifthat{0.5\glyphwidth}{#1}\else
\if B#1\shifthat{0.2\glyphwidth}{#1}\else
\if x#1\shifthat{0.1\glyphwidth}{#1}\else
\shifthat{0.15\glyphwidth}{#1}% THIS IS THE DEFAULT VALUE
\fi
\fi
\fi
}%
}
\fboxsep=0pt\fboxrule=.25pt
\begin{document}
\centering
{\tiny How hatglyph is being scaled to \verb|\talldot| (stacked dots)\\
at different mathstyles}
\[
x
\scalerel{\fbox{\hatglyph}}{\ThisStyle{\fbox{$\SavedStyle\talldot$}}}
\scriptstyle
\scalerel{\fbox{\hatglyph}}{\ThisStyle{\fbox{$\SavedStyle\talldot$}}}
\scriptscriptstyle
\scalerel{\fbox{\hatglyph}}{\ThisStyle{\fbox{$\SavedStyle\talldot$}}}
\]
\[
\begin{array}{l}
\hat A \hat B \hat x \hat y A_{{\hat x}_{\hat B \hat A \hat x}}\\
\newhat A \newhat B \newhat x \newhat y A_{{\newhat x}_{\newhat B
\newhat A \newhat x}}\\
\end{array}
\]
\end{document}
ORIGINAL SOLUTION (FAST BUT CAN'T HANDLE \scriptsize
, \scriptscriptsize
):
The macro \newhat
does a test on what letter is the argument and uses a horizontal shift on the hat symbol to set the location for that letter. If a letter has not been "rekerned" in this fashion, it defaults to a setting (as shown for the letter "y" in my example. The value of \Sstackgap
determines the vertical spacing betwen the top of the letter and the bottom of the hat. The negative value was required, because my hat is actually an elevated had over a null symbol, and I had to bring it down a bit, when stacked over an additional letter.
The MWE shows the normal \hat
on the first line, and \newhat
on the second. As I said, since I don't have your font, my font doesn't really suffer the problems yours does, so there is not a great difference.
Note: instead of using a "default" shift, as I do in this MWE, you could alternately use \hat{#1}
as the default, which would get you back to where you were. Also, I use pts as units of my shift. You might prefer ex's, so as to retain usage when the font is scaled.
\documentclass{article}
\usepackage[oldsyntax]{stackengine}
\def\stacktype{S}\Sstackgap=-4.3pt
\newcommand\shifthat[2]{%
\stackengine{\Sstackgap}{$#2$}{\(\hspace{#1}\hat{}\)}{O}{l}{F}{T}{S}}
\newcommand\newhat[1]{%
\if A#1\shifthat{5.2pt}{#1}\else
\if B#1\shifthat{4.8pt}{#1}\else
\if x#1\shifthat{3.6pt}{#1}\else
\shifthat{3.4pt}{#1}% THIS IS THE DEFAULT VALUE
\fi
\fi
\fi
}
\begin{document}
\[
\begin{array}{l}
\hat A \hat B \hat x \hat y\\
\newhat A \newhat B \newhat x \newhat y
\end{array}
\]
\end{document}
[EDIT: This MWE uses obsolete stackengine syntax for setting stackgap lengths (e.g., \Sstackgap=1ex), which prevented scalable lengths from scaling under a fontsize change. Version 2 of the package (submitted JUL-11-13) remedies the problem with a small syntax change.]