# \hat with \bar in subscript when using amsmath

I want to have the variance symbol (sigma squared) with an {X with bar} in the subscript and a hat on top of everything. The following code produces a mess (The X is not subscripted). Can anyone tell me why? (Using \overline instead of \bar is not an option.)

\documentclass{standalone}

\usepackage{amsmath}

\begin{document}

$\hat{\sigma^2_{\bar{X}}}$

\end{document}


I can't explain why the problem arises in the first place, but one solution is to box the initial item, and apply the \hat at the end. I also provide a 2nd alternative where I apply the \hat only to the \sigma; that may be preferable.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\setbox0=\hbox{$\sigma^2_{\bar{X}}$} \hat{\box0}$
$\setbox0=\hbox{$\hat\sigma$}\box0^2_{\bar{X}}$
\end{document}


If one wanted the first approach, but with a really wide hat, my answer at Really wide hat symbol might be applicable:

\documentclass{article}
\usepackage{scalerel,stackengine}
\stackMath
\newcommand\reallywidehat[1]{%
\savestack{\tmpbox}{\stretchto{%
\scaleto{%
\scalerel*[\widthof{\ensuremath{#1}}]{\kern-.6pt\bigwedge\kern-.6pt}%
{\rule[-\textheight/2]{1ex}{\textheight}}%WIDTH-LIMITED BIG WEDGE
}{\textheight}%
}{0.5ex}}%
\stackon[1pt]{#1}{\tmpbox}%
}
\usepackage{amsmath}
\begin{document}
$\reallywidehat{\sigma^2_{\bar{X}}}$
\end{document}


• Thank you very much, Steven. Your first solution produces what I wanted. I was waiting for a nicer solution, but since, according to egreg, this is a bug, I accept your workaround :) Aug 7 '15 at 12:58

Like this?

\documentclass{article}

\begin{document}

$\widehat{\sigma_{\bar X}^2}$
\end{document}


or are you referring to the fact that \bar X is not aligned with the 2. For that you can use \! albeit that there are more sophisticated solutions (kerning).

• apparently it only happens when I include amsmath. I updated the question. The problem is that the X is aligned with sigma, it doesn't look like a subscript Aug 7 '15 at 11:46