[Tex/LaTex] \overline and \widehat with subscript

math-modesubscriptssymbols

I would like to create commands to produce overlines (respectively wide hats or arrows) with a subscript at the end. The commands

\overline{X}^{1}   \widehat{X}^{2}  \overrightarrow{X}^{3}

do not suit my needs since the subscripts should be controlled by the overline (respectively wide hat, right arrow). Ideally I would like to have a command like

\myoverline_{1}{X}   \mywidehat_{2}{X}  \myoverrightarrow_{3}{X}

What would be the cleanest way to do so?

P.S. The closest thing I was able to find is this question.

Update Here is a picture of what I wish to obtain:

enter image description here

Best Answer

This approach gives the subscript to the "overline" as you seemed to indicate. With this implementation, the overline thickness is .4pt which can be changed. The bottom of the subscript is 1.5pt below the top of the argument. The 2pt setting determines the vertical location of the overbar relative to the subscript.

\documentclass{article}
\usepackage{stackengine}
\stackMath
\newcommand\subline[2]{\stackon[-1.5pt]{#1}{\rule[2pt]{\widthof{$#1$}}{.4pt}_{#2}}}
\begin{document}
\renewcommand\stackalignment{l}
$A = \subline{X}{1} \subline{g}{2} \subline{Xyzq}{12}$
\end{document}

enter image description here


For the widehat, I adopt a bit of a different approach, making added use of the scalerel package. The parameter \hatgap gives the vertical gap between item and hat, and \subdown gives the relative placement of the subscript relative to the hat.

\documentclass{article}
\usepackage{scalerel}
\usepackage{stackengine}
\stackMath
\def\hatgap{2pt}
\def\subdown{-2pt}
\newcommand\reallywidehat[2][]{%
\renewcommand\stackalignment{l}%
\stackon[\hatgap]{#2}{%
\stretchto{%
    \scalerel*[\widthof{$#2$}]{\kern-.6pt\bigwedge\kern-.6pt}%
    {\rule[-\textheight/2]{1ex}{\textheight}}%WIDTH-LIMITED BIG WEDGE
}{0.5ex}% THIS SQUEEZES THE WEDGE TO 0.5ex HEIGHT
_{\smash{\belowbaseline[\subdown]{\scriptstyle#1}}}%
}}
\begin{document}
$\reallywidehat[1]{zbcdklm} \times
\reallywidehat[ijk]{zbcdefghijk} = 
\reallywidehat{zb}$
\end{document}

enter image description here

The widehat EDIT is very loosely based on my solution at Really wide hat symbol, though this implementation is superior to that one.