# How to use a user-defined command in glossary file?

I've a user-defined symbol like this:

\newcommand{\myexclam}{\mathrel{\stackinset{c}{}{t}{}{\rotatebox{-45}{\scalebox{.5}{$/$}}}{$!$}}}


This symbol should be used in one of my glossary's entries as below (gloss.tex):

\newglossaryentry{mykey}{name={\ensuremath{\delta_{\text{act}}(a,\tau)\myexclam}}, description={Activity automaton}}


and here is main.tex:

\documentclass[doublespaced,12pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage[symbols]{glossaries}
\usepackage{stackengine}
\usepackage{hyperref}

\newcommand{\myexclam}{\mathrel{\stackinset{c}{}{t}{}{\rotatebox{-45}{\scalebox{.5}{$/$}}}{$!$}}}

\makenoidxglossaries

\begin{document}

Here is my formula \gls{mykey}

\printnoidxglossary[sort=standard,title={List of Symbols}]

\end{document}


But the compilation throws an undefined control sequence corresponding to \myexclam.

How can I properly use this command in my glossary?

• What if you paste the command's code into the glossary entry instead of \myexclam? – A.Loc Oct 12 '17 at 5:49
• @A.Loc: The result would be the same. – Roboticist Oct 12 '17 at 5:50
• @Roboticist: I get the same error using \myexclam directly, i.e. without glossaries at all, so there error is inside \myexclam, not with \gls etc. – user31729 Oct 12 '17 at 7:04
• I've written a comment to Steven B. Segletes -- it's possibily an issue with \stackinset from his stackengine package – user31729 Oct 12 '17 at 7:12

In this case, \myexclam must be \protected in the \newglossaryentry.

EDITED to add a sort= key to \newglossaryentry, since the name is non-alphabetic, per Nicola's instruction. Also, loaded hyperref prior to glossaries.

\documentclass[doublespaced,12pt]{article}
\usepackage{filecontents}
\begin{filecontents*}{gloss.tex}
\newglossaryentry{mykey}{name={\ensuremath{\delta_{\text{act}}(a,\tau)\protect\myexclam}},
description={Activity automaton},sort={delta activity}}
\end{filecontents*}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{hyperref}
\usepackage[symbols]{glossaries}
\usepackage{stackengine}

\newcommand{\myexclam}{\mathrel{\stackinset{c}{}{t}{}{\rotatebox{-45}{%
\scalebox{.5}{$/$}}}{$!$}}}

\makenoidxglossaries

\begin{document}

Here is my formula \gls{mykey}

\printnoidxglossary[sort=standard,title={List of Symbols}]

\end{document}


In the event that there were multiple macros needing protection (for example, if \myexclam were used more than once), it might just be easier to define it this way, so that only a single \protect is required:

\begin{filecontents*}{gloss.tex}
\newcommand\newname{\ensuremath{\myexclam\delta_{\text{act}}(a,\tau)\myexclam}}
\newglossaryentry{mykey}{name={\protect\newname}, description={Activity automaton},
sort={delta activity}}
\end{filecontents*}

• The sort key is always needed in \newglossaryentry for entries that contain any (non-accent) commands when used with \makenoidxglossaries. – Nicola Talbot Oct 12 '17 at 12:37
• @NicolaTalbot Not being a glossary user, that key would be in the form of ,sort={? ? ?}, what, exactly? Maybe ,sort={delta actual}? – Steven B. Segletes Oct 12 '17 at 12:53
• \printnoidxglossary sorts according to the value of the sort key provided with each entry and sort=standard means an alphabetical sort. Since it uses TeX to sort, it only sorts according to (lowercase) character code. If sort is missing it will be taken from the name key but the noidx method used here locally modifies accent commands to allow them to be stripped when the sort is obtained from the name (hence the expansion problem). I think sort={delta actual} sounds like a reasonable value. – Nicola Talbot Oct 12 '17 at 14:02
• This type of glossary entry would be much better with \makeglossaries and makeindex. (name={\protect\newname} won't work with xindy without the sort key.) By the way, didn't notice it earlier: hyperref needs to be loaded before glossaries. – Nicola Talbot Oct 12 '17 at 14:05