# How to fix superscripts in glossaries with parameters

Inspired by this question I created a glossary entry with an optional parameter as a subscript. However when I try to have a superscript there as well I can't use the trick where I pass the superscript as an optional argument because I already have one. Is there any workaround?

# MWE

\documentclass{article}
\usepackage{glossaries-extra}
\glsnoexpandfields

\newglossaryentry{A}{
category={arg},
user1={1},
name={$$A_{\glsarg}$$},
text={A_{\glsarg}},
description={A}}

\preto\glsentryfmt{%
\glsifcategory{\glslabel}{arg}% if category set to "arg"
{%
\ifdefempty\glsinsert
{\glsfieldfetch{\glslabel}{useri}{\glsarg}}%
{%
\let\glsarg\glsinsert
\let\glsinsert\empty
}%
}%
{}%
}

\begin{document}
Tried:
$\gls{A}[ij]^T$
$\gls{A}[ij^T]$
Desired outcome:
$A^T_{ij}$
\end{document}


You essentially have this:

\documentclass{article}

\begin{document}
\newcommand{\glsarg}{ij^T}
$A_{\glsarg}$
\end{document}


which puts ij^T as the subscript (that is, A_{ij^T}) where ^T is the superscript of j.

The following, which moves the braces, works:

\documentclass{article}

\begin{document}
\newcommand{\glsarg}{{ij}^T}
$A_\glsarg$
\end{document}


Applying this to the MWE:

\documentclass{article}
\usepackage{glossaries-extra}
\glsnoexpandfields

\newglossaryentry{A}{
category={arg},
user1={1},
name={$$A_{\glsarg}$$},
text={A_\glsarg},
description={A}}

\preto\glsentryfmt{%
\glsifcategory{\glslabel}{arg}% if category set to "arg"
{%
\ifdefempty\glsinsert
{\glsfieldfetch{\glslabel}{useri}{\glsarg}}%
{%
\let\glsarg\glsinsert
\let\glsinsert\empty
}%
}%
{}%
}

\begin{document}
Compare
$\gls{A}[{ij}^T]$
With:
$A^T_{ij}$
\end{document}


Note that with this method you must remember to always apply the grouping in the optional argument if there's more than one subscript. For example:

$\gls{A}[{ij}]$