\emptyset doesn't work with LuaTex + glossaries + unicode-math (bug?)

The following code doesn't work with LuaTex (I get an "undefined control sequence" error). It works if I either comment out \usepackage{unicode-math} or if I change the glossary name from $\emptyset$ to something else. Is this a bug? How can I fix this because I need all packages.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{unicode-math}

\usepackage{glossaries}
\newglossaryentry{symb}{name={$\emptyset$}, description={description}}

\begin{document}
$\emptyset$, \gls{symb}
\end{document}


A number, but not all, of the unicode-math definitions are performed at \begin{document}. In particular \emptyset is defined to be \varnothing early on, but the definition of \varnothing occurs at the start of the document body. Moving the definition of the glossary entry out of the preamble circumvents the problem.

Other ways to deal with this include

• issuing \glsnoexpandfields before the definition

• adding \protect to the glossary entry

\newglossaryentry{symb}{name={$\protect\emptyset$}, description={description}}

• providing a temporary definition of \varnothing in the preamble, e.g. \let\varnothing\relex just after \usepackage{unicode-math}

The first approach suggested by the glossaries author Nicola Talbot is the simplest approach for entries in the preamble. (According to the documentation it should be unnecessary, as that states that the name field is not expanded by default, but your example shows that expansion is occuring.) egreg suggested the second approach which is good for a one off entry. The final approach is what I originally suggested; I would not recommend this as a general practice, but it does demonstrate exactly where the problem lies:

\documentclass{article}
\usepackage{unicode-math}
\let\varnothing\relax

\usepackage{glossaries}
\newglossaryentry{symb}{name={$\emptyset$}, description={description}}

\makeglossaries

\begin{document}
$\varnothing$, $\emptyset$, \gls{symb}

\printglossaries
\end{document}

• Another strategy is using $\protect\emptyset$. – egreg Jan 6 '14 at 20:47
• @egreg Much preferable. – Andrew Swann Jan 6 '14 at 20:52
• @user4811 Alternatively, switch off the field expansions using \glsnoexpandfields (for version 4.0 and above). – Nicola Talbot Jan 6 '14 at 22:02
• @NicolaTalbot Very good. I have updated my answer to include this. – Andrew Swann Jan 7 '14 at 7:54
• @AndrewSwann You're right that the name field isn't expanded by default, but the text and first fields are (and plural and firstplural) which is what's causing the problem as they pick up their values from the name key if unspecified. (This behaviour is maintained for backwards compatibility and originates from the time when the name field was written to the external glossary file and so had to be sanitized to protect fragile commands.) – Nicola Talbot Jan 7 '14 at 9:09