I use the package glossaries and I have


in my preamble as suggested in the documentation to make each entry start with an upper-case letter in the glossary. But I have a handful entries that start with an inline-math command like

  name={$k$-nearest neighbor method},%
  sort={k-nearest-neighbor method}%
  Explanation here.

The second run of the compilation fails in the gls-file at the \glossentry command for the respective entry with

! Missing { inserted. <to be read again> $

Compilation is fine if I delete the gls-file and remove the $...$ in the entry's definition. But I need the inline-math to get proper typography.

Contrary it makes no sense to convert something to uppercase here. Although there is actually an upper case "K" this should be treated as an exception.

UPDATE: I found a partial solution (at least the errors disappears). Instead of $...$ one can wrap the "k" into \ensuremath{k}. The \makefirstuc command has the capability to skip nested macros and to convert the argument of the nested macro into uppercase. This means \makefirstuc{\ensuremath{k}} expands to \ensuremath{K} and the compiler errors vanish. But the principal question still remains open. How can I completely disable \makefirstuc for this entry, because I do not want any conversion here.

  • Can you expand the code to a minimal compilable example so we don't need to guess? Maybe name={\ignore{x}$k$-nearest neighbor method} along with \newcommand\ignore[1]{} might do.
    – egreg
    Commented Nov 17, 2014 at 11:10

1 Answer 1


Following egreg's suggestion from the comment above I found the solution. You do not even need to define a new macro, it is sufficient to start the entry with an empty {} which is eaten by \makefirstuc. But it is necessary to separately set the text variable so that citing the reference works with correct spacing. In summary, the full solution is

  name={{}$k$-nearest-neighbor},%  Start with empty token so that \makefirstuc ignores the inline math
  text={$k$-nearest-neighbor},%  Must be set separately without leading {} to avoid false additional spaces in citing
  sort={k-nearest-neighbor}%  The sort algorithm needs a plain-text variant
  Explanantion here.%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .