# glossaries-extra autoindex, manual sorting of entry

(Kind of a follow up question to this one.) The following example contains a definition of an abbreviation which requires some extra care: [Śatika-]Kālajñāna is supposed to be sorted, both in the list of abbreviations and the index, under Kālajñāna. (btw. I do have a xindy file taking care of all the diacritics, but of course that does not help here.) Probably creating an extra xindy file for exceptions like this could be a way of dealing with the problem, but I hope there's a cleaner way of doing it?

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

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

\usepackage[abbreviations,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{śkj}{{}[Ś]KJ}{\skti{[Śatika-]Kālajñāna}}

\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

\begin{document}

\gls{yh}
\gls{śkj}

\printglossaries
\printindex
\end{document}


I think, the simplest method is to just use the sort key:

\newabbreviation[sort={Kālajñāna}]{śkj}{{}[Ś]KJ}{\skti{[Śatika-]Kālajñāna}}


This deals with the glossary list. The sort value for the index is obtained in your document through the long field (Within \glsxtrautoindexassignsort) so one way of dealing with that is to provide a command that formats the square bracket material:

\newrobustcmd{\squarebracket}[1]{[#1]}


(I recommend choosing a better name for the command.) Then the abbreviation can be defined as:

\newabbreviation[sort={Kālajñāna}]{śkj}{{}[Ś]KJ}{\skti{\squarebracket{Śatika-}Kālajñāna}}


This custom command can locally be redefined to ignore its argument when obtaining the sort value:

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


The complete MWE:

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

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

\usepackage[abbreviations,xindy]{glossaries-extra}

\GlsSetQuote{+}
\GlsXtrSetEscChar{+}

\makeglossaries
\makeindex

\newrobustcmd{\squarebracket}[1]{[#1]}

\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[sort={Kālajñāna}]{śkj}{{}[Ś]KJ}{\skti{\squarebracket{Śatika-}Kālajñāna}}

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

\begin{document}

\gls{yh}
\gls{śkj}

\printglossaries
\printindex
\end{document}


The abbreviation glossary file (.glo-abr) contains:

(indexentry :tkey (("YH" "\\glossentry{yh}") ) :locref "{}{1}" :attr "pageglsnumberformat" )
(indexentry :tkey (("Kālajñāna" "\\glossentry{śkj}") ) :locref "{}{1}" :attr "pageglsnumberformat" )


The first argument of the :tkey part is the sort value. The index file contains:

\indexentry{Yoginīhṛdaya@\glsentryfirst{yh}}{1}
\indexentry{Kālajñāna@\glsentryfirst{śkj}}{1}


The part before the @ is the sort value. So in both cases, the bracketed part has been stripped.

(As an aside, I would use bib2gls rather than xindy for this type of document, for both the glossary and index, as then commands like \squarebracket can locally be redefined just for bib2gls's use, but I realise that switching in an existing document isn't simple.)