# \hat and \bar compatibility issue EDIT: It's a bug, workaround provided

I have two \newcommand defined as follows:

\newcommand{\hvar}[1]{\hat{\delta #1}}
\newcommand{\gbp}{{{\bar{\gamma}}_p}}


And they look like this:

\hvar{X}

\gbp

The problem is that, if I use them together I get this:

\hvar{\gbp}

It seems like the \gamma was shifted to the right. Is this a bug? Is there a way to fix it?

Here is a MWE:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\hvar}[1]{\hat{\delta #1}}
\newcommand{\gbp}{{{\bar{\gamma}}_p}}
\begin{document}
$$\hvar{X} \qquad \gbp \qquad \hvar{\gbp}$$
\end{document}


# EDIT:

As egreg mentioned, this is a bug on the accent implementation of amsmath. A work around is provided here: Why do arguments to nested \tilde or \breve commands reappear when amsmath is used? and a MWE presented below:

\documentclass{article}
\usepackage{amsmath}

\newsavebox{\accentbox}
\newcommand{\compacc}[2]{\sbox\accentbox{$#2$}#1{\usebox\accentbox}}
\newcommand{\compaccX}[2]{\let\accenttemp#1\mathpalette\docompacc{#2}}
\def\docompacc#1#2{\compacc\accenttemp{#1#2}}

\newcommand{\hvar}[1]{\compaccX{\hat}{\delta #1}}
\newcommand{\gbp}{{{\bar{\gamma}}_p}}
\begin{document}
$$\hvar{X} \qquad \gbp \qquad \hvar{\gbp} \qquad A_{\hvar{\gbp}}$$

\end{document}


Looks like:

-
I can't really provide any technical insights on this one, but I will observe that replacing \hat with \widehat seems to work, if that's an acceptable solution. – You Sep 9 '13 at 18:05
\hat and \bar are accents. Combining multiple accents onto one character is one of the "scary" parts of TeX. – kahen Sep 9 '13 at 18:24
Are you planning on using this symbol combination in subscripts/superscripts? – Werner Sep 9 '13 at 18:48
Yes. I will need a \dot{\gbp} in a subscript, which has the same problem. – Miguel Sep 9 '13 at 18:53
I believe this is a bug in the accent implementation of amsmath. :-( Some macros that are set globally disrupt the working of the inner accent. This can be seen by trying \hvar{\text{$\gbp$}} which produces the dreaded undefined control sequence error about \macc@kerna. See also tex.stackexchange.com/questions/30327/… – egreg Sep 9 '13 at 19:48

Here's an alternative using a homegrown \althat routine, with three defining parameters (\Ht, \Wd, and \Dp).

\documentclass{article}
\usepackage{amsmath}
\usepackage{scalerel}

\newcommand{\hvar}[1]{\althat{\delta #1}}
\newcommand{\gbp}{{{\bar{\gamma}}_p}}

\newlength\svarraycolsep
\newcommand\althat[1]{%
\renewcommand\arraystretch{\Dp}%
\setlength\svarraycolsep{\arraycolsep}%
\setlength\arraycolsep{0in}%
\begin{array}{c}%
\vstretch{\Ht}{%
\hstretch{\Wd}{%
\trimbox{.15ex .75ex .15ex .2ex}{\scalerel*{\char'136}{\rule{1ex}{1ex}}}%
}%
}\\%
#1\\%
\rule{1ex}{0ex}\\%
\end{array}%
\renewcommand\arraystretch{1.0}%
\setlength\arraycolsep{\svarraycolsep}%
}

\parindent 0in
\begin{document}

\def\Ht{1.8}
\def\Wd{4.5}
\def\Dp{.3}
$$\althat {\mathrm{H}} = \althat {\mathrm{T}} + \althat {\mathrm{V}}$$

$$\hvar{X} \qquad \gbp \qquad \hvar{\gbp}$$

\end{document}


As originally proposed at How to make circumflexes wider but still of a fixed size in XeTeX?

-