How can glossaries handle the article 'a' or 'an' before abbreviations?

I am wondering how would glossaries handle the article 'a' that comes before abbreviations. For e.g.,

Fox Mulder is a Federal Bureau of Investigation special agent educated at Oxford who believes in the existence of extraterrestrials and a government conspiracy to hide the truth regarding them.

However, if I use glossaries to abbreviate FBI later, it would read:

Dana Scully is an FBI special agent, a medical doctor, and scientist who is Mulder's partner. In contrast to his credulity, Scully is a skeptic, basing her beliefs on scientific explanations.

If I move the second statement up it would read:

Scully is an Federal Bureau of Investigation special agent, a medical doctor, and scientist who is Mulder's partner. In contrast to his credulity, Scully is a skeptic, basing her beliefs on scientific explanations.

How can this be prevented with glossaries?

MWE

\documentclass{article}
\usepackage{glossaries}
\newacronym{fbi}{FBI}{Federal Bureau of Investigation}
\begin{document}
Fox Mulder is a \gls{fbi} special agent educated at Oxford
who believes in the existence of extraterrestrials and a
government conspiracy to hide the truth regarding them.

Dana Scully is an \gls{fbi} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on
scientific explanations.
\end{document}


You can use glossaries-prefix which is supplied with the glossaries package:

\documentclass{article}
\usepackage{glossaries-prefix}
\newacronym[
prefixfirst={a\ },% prefix used on first use
prefix={an\ }% prefix used on subsequent use
]
{fbi}{FBI}{Federal Bureau of Investigation}
\begin{document}
Fox Mulder is \pgls{fbi} special agent educated at Oxford
who believes in the existence of extraterrestrials and a
government conspiracy to hide the truth regarding them.

Dana Scully is \pgls{fbi} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on
scientific explanations.
\end{document}


The glossaries-prefix package loads glossaries and adds four new keys: prefix, prefixplural, prefixfirst and prefixfirstplural. You can determine if the prefix field has been provided for a particular entry with \ifglshasprefix{label}{true}{false}. This tests if the prefix field is empty. There are similar commands for the other prefix fields.

The command \pgls¹ has the same syntax as \gls and performs the same test as in Ulrike's answer, so \pgls{fbi} is essentially the same as

\ifglsused{fbi}{\glsentryprefix{fbi}}{\glsentryprefixfirst{fbi}}\gls{fbi}


but is more convenient to use.

Note that there's no space automatically inserted between the prefix and \gls to allow for things like prefix={l'} in French. If you want a second set of prefixes, then you'll have to add the fields manually using \glsaddkey and define a command similar to \pgls.

¹ Similarly for the upper case and plural variants. See the glossaries user manual for the complete list of provided commands.

• One might want to refer to “the FBI”. Sep 14, 2017 at 12:48
• @egreg In that case one would just write the \gls{fbi} since then it doesn't matter whether or not the abbreviation has been expanded. Sep 14, 2017 at 13:35
• @NicolaTalbot prefix versions of the \ac commands would be very appreciated ;) Jul 2, 2019 at 8:48
• Minimal version would be something like \let\pac\pgls \let\Pac\Pgls \let\pacp\pglspl \let\Pacp\Pglspl Jul 2, 2019 at 9:32

You can test if an entry has been used:

\documentclass{article}
\usepackage{glossaries}
\newacronym{fbi}{FBI}{Federal Bureau of Investigation}
\begin{document}
Fox Mulder is \ifglsused{fbi}{an}{a} \gls{fbi} special agent educated at Oxford
who believes in the existence of extraterrestrials and a
government conspiracy to hide the truth regarding them.

Dana Scully is \ifglsused{fbi}{an}{a} \gls{fbi} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on
scientific explanations.
\end{document}