# Expanding glossaries abbreviations/acronyms in commands

I want to implement \usepackage{glossaries-extra} for handling abbreviations and auto-expanding them. I've also created a math command that makes use of an acronym and am having some difficulty getting the command to work well with the abbreviation when the command is the first usage of the abbreviation.

MWE

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

\newabbreviation{ae}{\text{a.e.}}{\text{almost everywhere}}
\newcommand*{\convae}{\xrightarrow{ \gls{ae} }}

\begin{document}
Expected behavior (not using glossaries' abbreviation)
$f_n \xrightarrow{\text{almost everywhere (a.e.)}}$
First use of the abbreviation, missing the almost everywhere'' and
parentheses. Notice the arrow size
$f_n \convae f.$
Second occurrence behaves correctly since acronym is now being used.
$g_n \convae g.$

\end{document}


This is the output:

Thank you :)

What you are seeing here is that \xrightarrow actually typesets the text above it twice: Once just to measure it and once more to actually typeset it. \gls marks the abbreviation as used on the first pass and then uses the short form on the second. (By the way: \text has a full four typesetting passes. It has \iffirstchoice@ to manage problems like this, but \xrightarrow does not seem to provide such a switch.)

In order to get around this, you can take explicit control of setting the flag marking the abbreviation as used. Also, let's move the \text to the definition of \convae so the space between the long and the short form is also affected by it.

\documentclass{article}

\usepackage{glossaries-extra}

\newabbreviation{ae}{a.e.}{almost everywhere}
\makeatletter
\newcommand*\convae{%
\ifglsused{ae}{%
\let\@convae@cleanup\relax
}{%
\def\@convae@cleanup{\glsreset{ae}}%
}%
\xrightarrow{%
\text{%
\gls{ae}%
\@convae@cleanup
}%
}%
\glsunset{ae}%
}
\makeatother

\begin{document}

Expected behavior (not using \texttt{glossaries}' abbreviation):
$f_n \xrightarrow{\text{almost everywhere (a.e.)}} f$

First use of the abbreviation:
$f_n \convae f$

Second use of the abbreviation:
$g_n \convae g$

\end{document}


• This is super helpful thank you! I've been tinkering with this implementation to try to add the space between the "almost everywhere" and "(a.e.)" of the gls call in the first use of the abbreviation. I can't seem to figure out this last detail. Thank you so much Apr 23 at 2:24
• A, missed that. I updated my answer. Apr 23 at 7:09
• You're amazing thank you so much Apr 23 at 21:46