# Add custom key to display units in glossary with siunitx

I decided to enrich my list of symbols by adding a unit column with a newly defined key "unit".

\glsaddkey
{unit}
{}
{\glsentryunit}
{\Glsentryunit}
{\glsunit}
{\Glsunit}
{\GLSunit}


A new entry looks like

\newglossaryentry{insu}{
type=symbols,
name=thermal resistance,
symbol={\ensuremath{R\mathrm{c}}},
description={ratio of the temperature difference across an insulator and the heat flux for a particular material or assembly of materials},
unit={\ensuremath\protect\si{\watt}},
}


and produces following error with the last curly brace, that closes the \newglossaryentry.

> ! Undefined control sequence.
\@glo@tmp ->\ensuremath \protect \si {\watt }


Moving the code inside the unit key, for testing purpose, to the key symbol works like a charm, but unfortunately not the intended place. Changes around the protect and ensuremath have no effect. The asterisk variant of \glsaddkey* doesn't help either.

What is the conflict between adding a new key to a glossary entry and the \si command?

Here a short MWE:

\documentclass{scrreprt}%
\usepackage{siunitx}
\usepackage{glossaries}

{unit}
{}
{\glsentryunit}
{\Glsentryunit}
{\glsunit}
{\Glsunit}
{\GLSunit}

\makeglossaries

\newglossaryentry{test}{
name=Test,
unit=\si{\meter},
description=sample description}

%-------------------------

\begin{document}

\printglossaries
\gls{test}

\end{document}

-
Why are you using \ensuremath and \protect with siunitx? The error here is happening has \watt is undefined outside of siunitx's commands: could you make a full minimal working example (MWE) so we can see exactly what is happening. –  Joseph Wright Jul 20 '14 at 7:36

The glossaries package expands the input for all entries by default. This won't work with 'local' definitions for commands, as used by siunitx for unit commands (which can me something entirely different in other context). Possible solutions are to use \glsnoexpandfields:

\glsnoexpandfields
\newglossaryentry{test}{
type=symbols,
name=Test,
unit=\si{\metre},
description=sample description}


or to inhibit expansion of just the unit

\newglossaryentry{test}{
type=symbols,
name=Test,
unit=\unexpanded{\si{\metre}},
description=sample description}


or turn off expansion for the field

\glssetnoexpandfield{unit}

-
Could I set the unexpanded flag already when defining the glsaddkey? –  Burcardo Jul 20 '14 at 9:31
@Burcardo That's probably one for Nicola Talbot (hopefully will pop up soon!) –  Joseph Wright Jul 20 '14 at 9:35
@Burcardo You can put \glssetnoexpandfield{unit} before or after \glsaddkey. You just need to make sure it's set before any instance of \newglossaryentry where the expansion needs to be suppressed. The starred version of \glsaddkey forces expansion on for that key even if \glsnoexpandfields is used (but can later be overridden with \glssetnoexpandfield{unit}). –  Nicola Talbot Jul 20 '14 at 10:55