# glossaries-extra autoindex and sorting of entries with language switching command

Follow up question to this one.

\documentclass{memoir}
\usepackage{polyglossia}
\setdefaultlanguage{english}
\setotherlanguage{sanskrit}
\setotherlanguage{german}
\setotherlanguage{french}
\newfontfamily\sanskritfont{Latin Modern Roman}[Ligatures=TeX]

\newcommand{\skti}[1]{\emph{\textsanskrit{#1}}}

\usepackage[xindy]{glossaries-extra}

\GlsSetQuote{+}
\GlsXtrSetEscChar{+}

\makeglossaries
\makeindex

\glssetcategoryattribute{abbreviation}{dualindex}{true}
\glssetcategoryattribute{general}{glossname}{firstuc}
\GlsXtrEnableIndexFormatOverride
\renewcommand*{\glsxtrautoindexentry}[1]{\string\glsentryfirst{#1}}
%\renewcommand*{\glsxtrautoindexassignsort}[2]{%
%  \ifglshaslong{#2}%
%  {\glsletentryfield{#1}{#2}{long}}%
%  {\glsletentryfield{#1}{#2}{sort}}%
%}
\setabbreviationstyle{long-short}
\newabbreviation{yh}{YH}{\skti{Yoginīhṛdaya}}
%\newabbreviation{yh}{YH}{Yoginīhṛdaya}

\begin{document}

\gls{yh}

\printglossaries
\printindex
\end{document}


This example compiles fine. If I comment in the redefined \glsxtrautoindexassignsort it stops working,

Writing index file test.idx
(./test.aux)
! Undefined control sequence.
\in@ #1#2->\begingroup \def \in@@
##1#1{}\toks@ \expandafter {\in@@ #2{}{}#1...
l.34 \gls{yh}

?


unless I remove my language switching command, by changing to the commented out version.

The redefinition of \glsxtrautoindexassignsort has been copied from sample-autoindex.tex, it should have the effect of the entry being sorted according to its long form (Yoginīhṛdaya), not the short one (YH), which is something one would want. I compile this with latexmk calling xelatex and xindy (texindy).

EDIT: changing the definition of \skti to use \newrobustcmd it compiles, but the "Yoginīhṛdaya" entry gets sorted under S, presumably because of the presence of \skti. \indexentry{\\skti {Yoginīhṛdaya}@\glsentryfirst{yh}}{1} is the line in the idx file. Curiously in my real file it appears under Y, but before other entries, suggesting that it has been sorted under YH. The idx file line there is \indexentry{\skti {Yoginīhṛdaya}@\glsentryfirst{yh}}{1}, for some reason the testing file has two backslashes, \\skti.

• Try defining \skti as a robust command. (That is, replace \newcommand with \newrobustcmd.) – Nicola Talbot Jun 14 '18 at 14:26
• @NicolaTalbot Please see my edit above. – muk.li Jun 14 '18 at 19:04

The glossaries package's xindy option uses xindy's native format (rather than the makeindex emulation provided with texindy). This means that it has to escape xindy's special characters rather than makeindex's. For example, if you have:

\newglossaryentry{sample}{name={\skti{Yoginīhṛdaya}},description={}}


with the xindy option, the sort value (obtained from the name) has to have the backslash escaped so that it's written to the xindy file as:

(indexentry :tkey (("\\skti {Yoginīhṛdaya}" "\\glossentry{sample}") ) :locref "{}{1}" :attr "pageglsnumberformat" )


The autoindexing function uses makeindex syntax in \index and also tries to escape any special characters. It should just escape the special characters identified by commands like \GlsXtrSetEscChar but it seems it's also still trying to escape the backslash because the base glossaries code is in native xindy mode. That's why you get the double backslash \\skti in the indexing file.

One way around this is to provide a definition of \glsxtrautoindexassignsort that strips \skti out when obtaining the sort value from the long field:

\makeatletter
\renewcommand*{\glsxtrautoindexassignsort}[2]{%
\glsxtrifhasfield*{long}{#2}%
{%
\let\orgskti\skti
\let\skti\@firstofone
\protected@edef#1{\glscurrentfieldvalue}%
\let\skti\orgskti
}%
{\glsletentryfield{#1}{#2}{sort}}%
}
\makeatother


This temporarily defines \skti to \@firstofone, which means that the command will be removed during the \protected@edef.

Another possibility is to move the formatting command out of the long field and into the abbreviation style:

\renewcommand*{\glsxtrautoindexassignsort}[2]{%
\ifglshaslong{#2}%
{\glsletentryfield{#1}{#2}{long}}%
{\glsletentryfield{#1}{#2}{sort}}%
}

\setabbreviationstyle{long-short}

\renewcommand{\glslongfont}[1]{\skti{#1}}
\newabbreviation{yh}{YH}{Yoginīhṛdaya}