[Tex/LaTex] Improper alphabetic constant while using glossaries

glossaries

I get the error "Improper alphabetic constant" while using the package glossaries. My MWE:

main file:

\documentclass{article}
\usepackage{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries
\loadglsentries{gloss}
\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist]
\end{document}

glossaries entries:

\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots \rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N} \cup \lbrace 0 \rbrace$.},
type=symbolslist
}

I can handle this by adding a sort={…} to each glossary entry, but since I have a lot of entries, I don't want to add this everytime. Is there another solution for this?

Best Answer

I can handle this by adding a sort={...} to each glossary entry, but since I have a lot of entries, I don't want to add this everytime. Is there another solution for this?

The simplest solution is to sort by definition or sort by order of use:

\printnoidxglossary[type=symbolslist,sort=def]% order of definition

or

\printnoidxglossary[type=symbolslist,sort=use]% order of use

Another method is to define your own sort handler that will strip the maths commands. For example:

\documentclass{article}
\usepackage{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries

\makeatletter

\newcommand*{\@glo@sortmacro@math}[1]{%
  \@glo@sortentries{\@glo@sorthandler@math}{#1}%
}

\newcommand*{\@glo@sorthandler@math}[2]{%
  \begingroup
  \cslet{ensuremath }{\@firstofone}%
  \cslet{mathbb }{\@firstofone}%
  \let\protect\empty
  \edef\sortA{\glsentrysort{#1}}%
  \edef\sortB{\glsentrysort{#2}}%
  \edef\x{%
    \endgroup
    \noexpand\dtlicompare{\noexpand\dtl@sortresult}%
    {\expandonce\sortB}%
    {\expandonce\sortA}%
  }%
  \x
}

\makeatother

\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}

\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist,sort=math]
\end{document}

image of result

Any other commands that occur in the name will similarly need to be temporarily redefined. Note the spaces in

  \cslet{ensuremath }{\@firstofone}%
  \cslet{mathbb }{\@firstofone}%

This is because the sort key is in the form:

\protect \ensuremath  {\protect \mathbb  {N}}

The commands may look like \ensuremath and \mathbb, but they're actually \ensuremath and \mathbb (trailing space is part of the command name).

Edit: Another method is to use the sanitizesort package option. This will convert command names like \mathbb into the sequence of characters \ m a t h b b, so it will behave much like makeindex.

\documentclass{article}
\usepackage[sanitizesort]{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries

\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}

\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist]
\end{document}

The difference in these methods can be observed by adding a few extra sample entries:

Using sanitizesort:

\documentclass{article}
\usepackage[sanitizesort]{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries

\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}

\newglossaryentry{N}{name={\ensuremath{N}},description={sample},
type=symbolslist
}
\newglossaryentry{M}{name={\ensuremath{M}},description={sample},
type=symbolslist
}

\newglossaryentry{Z}{name={\ensuremath{Z}},description={sample},
type=symbolslist
}

\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist]
\end{document}

image of result ordering: M N Z \mathbb{N} \mathbb{N}_0

Using custom sort:

\documentclass{article}
\usepackage{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries

\makeatletter

\newcommand*{\@glo@sortmacro@math}[1]{%
  \@glo@sortentries{\@glo@sorthandler@math}{#1}%
}

\newcommand*{\@glo@sorthandler@math}[2]{%
  \begingroup
  \cslet{ensuremath }{\@firstofone}%
  \cslet{mathbb }{\@firstofone}%
  \let\protect\empty
  \edef\sortA{\glsentrysort{#1}}%
  \edef\sortB{\glsentrysort{#2}}%
  \edef\x{%
    \endgroup
    \noexpand\dtlicompare{\noexpand\dtl@sortresult}%
    {\expandonce\sortB}%
    {\expandonce\sortA}%
  }%
  \x
}

\makeatother

\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}

\newglossaryentry{N}{name={\ensuremath{N}},description={sample},
type=symbolslist
}
\newglossaryentry{M}{name={\ensuremath{M}},description={sample},
type=symbolslist
}

\newglossaryentry{Z}{name={\ensuremath{Z}},description={sample},
type=symbolslist
}

\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist,sort=math]
\end{document}

image of result order: M \mathbb{N} N \mathbb{N}_0 Z