# Redefine a letter in mathmode

I'd like to do the same thing that is been done to the greek letter \chi in this post to the letter g. In particular I'd like to redefine g in mathmode to work as \chi here:

\makeatletter
\renewcommand\chi{\@ifnextchar_\sub@chi\latexchi}
\newcommand{\sub@chi}[2]{% #1 is _, #2 is the subscript
\@ifnextchar^{\subsup@chi{#2}}{\latexchi^{}_{#2}}%
}
\newcommand{\subsup@chi}[3]{% #1 is the subscript, #2 is ^, #3 is the superscript
\latexchi_{#1}^{#3}%
}
\makeatother

You can do better than six years ago.

I define a generic macro for lowering the subscript as if a superscript is present.

For g, one needs to make it math active and supply a definition for it, with the common \lowercase trick. In order to save the standard math g, we need to copy its math code.

\documentclass{article}
\usepackage{amsmath,xparse}

\linespread{1.1} % high subscripts and low subscripts

\NewDocumentCommand{\movedownsub}{e{^_}}{%
\IfNoValueTF{#1}{%
\IfNoValueF{#2}{^{}}% neither ^ nor _, do nothing; if no ^ but _, add ^{}
}{%
}%
}
% chi
\NewCommandCopy{\latexchi}{\chi}
\RenewDocumentCommand{\chi}{}{\latexchi\movedownsub}
% g
\mathchardef\latexmathg=\mathcodeg
\begingroup\lccode~=g \lowercase{\endgroup\def~}{\latexmathg\movedownsub}
\mathcodeg="8000

\begin{document}

\textbf{Chi}

$\latexchi_{A}\chi_{A}$

$\latexchi_{A_{n}}\chi_{A_{n}}$

$\chi_{A}\quad\chi_{A}^{2}\quad\chi^{2}_{A}\quad\chi^{2}\quad\chi$

\medskip

\textbf{g}

$\latexmathg_{A}g_{A}$

$\latexmathg_{A_{n}}g_{A_{n}}$

$g_{A}\quad g_{A}^{2}\quad g^{2}_{A}\quad g^{2}\quad g$

\end{document}