[Tex/LaTex] \hat and \widehat look terrible: fixing width and better positioning

accentsmath-modescalingsymbolsxetex

I want to make the use of quantum mechanical operators explicit in my text using \hat{A} or \widehat{A}. The problem is that depending on the symbol under the accent it looks either misplaced or too small, but see for yourself:

enter image description here

In the first case, using \hat, the accent above the B looks just slightly too much left-aligned. The other cases are just obviously bad. I have generated all examples using the code below.

I found some related questions:
Why does \widehat behave differently if I insert \hspace{0pt}?
And in particular:
How can I manually choose the size of a wide accent? (math mode)
I took some code from the latter, but it doesn't help. Also, the accepted answer in the latter does not compile – I think the accent package doesn't work with xelatex.

So, how can I…

  • better center the accents above the characters?
  • have the accents have a similar width?
  • maybe let them be on the same height (look at the
    first AB – the hat above the B seems a bit lower)?

.

\documentclass{scrartcl}

\usepackage[tbtags]{mathtools}
\usepackage[math-style=ISO,bold-style=ISO]{unicode-math}
\setmainfont[Ligatures={TeX,Common}]{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}

\usepackage{calc}
\newcommand{\styletofont}[1]{%
  \ifx\displaystyle#1\let\next\textfont\fi
  \ifx\textstyle#1\let\next\textfont\fi
  \ifx\scriptstyle#1\let\next\scriptfont\fi
  \ifx\scriptscriptstyle#1\let\next\scriptscriptfont\fi}

\newcommand{\innfwhat}[2]{%
  \styletofont{#1}%
  \dimen0 \fontcharic\next1 \skewchar\next1
  \advance\dimen0 -\fontcharic\next1`#2%
  \makebox[0pt][l]{$#1#2$}%
  \makebox[\widthof{$#1#2$}]{$#1\kern.5\dimen0 \widehat{\vphantom{#2}}$}}
\newcommand{\fwhat}{\mathpalette\innfwhat}

\pagestyle{empty}

\begin{document}

\begin{equation*}
  \begin{split}
    &\hat{A}\hat{B} \\
    &\widehat{A}\widehat{B}\\
    &\widehat{\hspace{0pt}A}\widehat{\hspace{0pt}B}\\
    &\fwhat{A}\fwhat{B}
  \end{split}
\end{equation*}

\end{document}

Best Answer

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}

enter image description here

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}

enter image description here

[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.]