The math
environment, identified with \begin{math}...\end{math}
, is fragile (but $...$
isn't) and so can break in an expandable context. By default, expansion is on for all fields except name
, description
, descriptionplural
, symbol
and symbolplural
. (The exceptions are for backward-compatibility because early versions of the glossaries
package had to write the name, description and symbol to the external glossary file, so those fields were sanitized instead of being expanded on definition.)
So in this case, \begin{math}...\end{math}
doesn't get expanded when the name
field is assigned, but it does get expanded when the other implicit fields (such as text
) that take their default values from the name
key are assigned.
As mentioned by Christian in the comments, it's much better to use \ensuremath
in this situation. Although \ensuremath
can cause problems in other contexts, it's the most convenient way of ensuring maths mode within glossary fields so that \gls
can be used in either maths or text mode.
\documentclass{article}
\usepackage{glossaries}
\makeglossaries
\newcommand{\jbv}[0]{j_{bv}}
\newcommand{\txtjbv}[0]{\begin{math}\jbv\end{math}}
\newglossaryentry{jbv}{name={\ensuremath{\jbv}},sort={a03},description={Bla}}
\begin{document}
In text: \gls{jbv}. In an equation:
\begin{equation}\gls{jbv}\end{equation}
In the glossary:
\printglossaries
\end{document}
\ensuremath{\jbv}}
or\glssetnoexpandfield{name}
but I don't recommend thisname
field defaults to no expansion. It's thetext
field that's expanding and causing the problem, but I agree it's much simpler just to usename={\ensuremath{\jbv}}
.