6

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} \hvar{X}

\gbp \gbp

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

\hvar{\gbp} \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}
\begin{equation}
\hvar{X} \qquad \gbp \qquad \hvar{\gbp}
\end{equation}
\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}
\begin{equation}
\hvar{X} \qquad \gbp \qquad \hvar{\gbp} \qquad A_{\hvar{\gbp}}
\end{equation}

\end{document}

Looks like:

enter image description here

Miguel
  • 504
  • 1
    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 09 '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 09 '13 at 18:24
  • Are you planning on using this symbol combination in subscripts/superscripts? – Werner Sep 09 '13 at 18:48
  • Yes. I will need a \dot{\gbp} in a subscript, which has the same problem. – Miguel Sep 09 '13 at 18:53
  • 1
    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 http://tex.stackexchange.com/questions/30327/why-do-arguments-to-nested-tilde-or-breve-commands-reappear-when-amsmath-is-us – egreg Sep 09 '13 at 19:48
  • Thank you egreg! I'll Edit my question to mention the bug and the workaround from your link! – Miguel Sep 09 '13 at 22:18

1 Answers1

1

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

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

\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}

enter image description here

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