[Tex/LaTex] Formated text in acronym sorted alphabetically in Index

acronymsglossariessorting

I am using glossaries package with the acronym option \usepackage[acronym,toc,shortcuts]{glossaries}.

An answer provided previously (Acronyms only shown in Index when used twice) works perfectly for listing acronyms in the Index with the instruction \gls{some_acronym}

Working on a new long thesis document I noticed that when an acronym text is formatted such as:
\newacronym{ita}{ITA}{\emph{italic text} acronym}
the acronym is listed in the Index under a "Symbols" category:

output of code

I would like to have formatted acronyms listed in the Index in alphabetical order. Any ideas?

MWE:

\documentclass[11pt]{article}
\usepackage{makeidx}
\makeindex
\usepackage[acronym,toc,shortcuts]{glossaries}
\makeglossaries

\renewcommand*{\CustomAcronymFields}{%
  name={\the\glsshorttok},%
  symbol={\the\glsshorttok},%
  text={\the\glsshorttok\protect\index{\the\glslongtok\space(\the\glsshorttok)}},%
  plural={\the\glsshorttok\noexpand\acrpluralsuffix\protect\index{\the\glslongtok\space(\the\glsshorttok)}},%
  first={\the\glslongtok\space(\the\glsshorttok)\protect\index{\the\glslongtok\space(\the\glsshorttok)}},%
  firstplural={\the\glslongtok\noexpand\acrpluralsuffix\space(\the\glsshorttok)\protect\index{\the\glslongtok\space(\the\glsshorttok)}},%
  description={\the\glslongtok}%
}

\SetCustomStyle

\newacronym{cd}{CD}{compact disk}
\newacronym{un}{UN}{United Nations}
\newacronym{ita}{ITA}{\emph{italic text} acronym}
\newacronym{mc}{MC}{4-methyl-compound}
\newacronym{hgh}{HGH}{human growth hormone}
\newacronym{pi}{pI}{isoelectric point}


\begin{document}
\noindent
First\index{first} use of \gls{cd}\\
subsequent\index{subsequent} use of \gls{cd}
, and
\gls{un} and
\gls{ita} and
\gls{mc} and
\gls{hgh} and again \gls{hgh} and
\gls{pi}.

\printglossaries
\printindex

\end{document}

Best Answer

The problem is that makeindex doesn't understand (La)TeX commands. The .idx file created by your MWE contains:

\indexentry{first}{1}
\indexentry{compact disk (CD)}{1}
\indexentry{subsequent}{1}
\indexentry{compact disk (CD)}{1}
\indexentry{United Nations (UN)}{1}
\indexentry{\emph  {italic text} acronym (ITA)}{1}
\indexentry{4-methyl-compound (MC)}{1}
\indexentry{human growth hormone (HGH)}{1}
\indexentry{human growth hormone (HGH)}{1}
\indexentry{isoelectric point (pI)}{1}

makeindex interprets \emph {italic text} acronym (ITA) as a string of characters \, e, m, p, h, etc. The first character is a backslash so makeindex puts it in the "Symbols" group. There are two options: provide makeindex with a different sort key that doesn't contain any formatting commands or use xindy instead (which simply ignores any LaTeX commands found in the sort key).

If you want to stick with makeindex, it's possible to modify your definition of \CustomAcronymFields so that it uses the same sort key that the glossary entry uses. The modified definition is:

\renewcommand*{\CustomAcronymFields}{%
  name={\the\glsshorttok},%
  symbol={\the\glsshorttok},%
  text={\the\glsshorttok\protect\index{\noexpand\glsentrysort{\the\glslabeltok}@\the\glslongtok\space(\the\glsshorttok)}},%
  plural={\the\glsshorttok\noexpand\acrpluralsuffix\protect\index{\noexpand\glsentrysort{\the\glslabeltok}@\the\glslongtok\space(\the\glsshorttok)}},%
  first={\the\glslongtok\space(\the\glsshorttok)\protect\index{\noexpand\glsentrysort{\the\glslabeltok}@\the\glslongtok\space(\the\glsshorttok)}},%
  firstplural={\the\glslongtok\noexpand\acrpluralsuffix\space(\the\glsshorttok)\protect\index{\noexpand\glsentrysort{\the\glslabeltok}@\the\glslongtok\space(\the\glsshorttok)}},%
  description={\the\glslongtok}%
}

This now creates the following .idx file:

\indexentry{first}{1}
\indexentry{CD@compact disk (CD)}{1}
\indexentry{subsequent}{1}
\indexentry{CD@compact disk (CD)}{1}
\indexentry{UN@United Nations (UN)}{1}
\indexentry{ITA@\emph  {italic text} acronym (ITA)}{1}
\indexentry{MC@4-methyl-compound (MC)}{1}
\indexentry{HGH@human growth hormone (HGH)}{1}
\indexentry{HGH@human growth hormone (HGH)}{1}
\indexentry{pI@isoelectric point (pI)}{1}

(The sort key is the bit before the @ character in each entry.) This now sorts according to the acronym rather than the full form. You can neaten things up a bit and reduce the chances of error by doing:

\newcommand*{\gindex}{%
  \protect\index{\noexpand\glsentrysort{\the\glslabeltok}@\the\glslongtok\space
    (\the\glsshorttok)}%
}

\renewcommand*{\CustomAcronymFields}{%
  name={\the\glsshorttok},%
  symbol={\the\glsshorttok},%
  text={\the\glsshorttok\gindex},%
  plural={\the\glsshorttok\noexpand\acrpluralsuffix\gindex},%
  first={\the\glslongtok\space(\the\glsshorttok)\gindex},%
  firstplural={\the\glslongtok\noexpand\acrpluralsuffix\space(\the\glsshorttok)\gindex},%
  description={\the\glslongtok}%
}

However, it would be better to sort on the long form, so if you still want to stick with makeindex, the following modification will strip \emph from the entry:

\makeatletter
\renewcommand{\newacronymhook}{%
  {%
    \let\emph\@firstofone
    \expandafter\protected@xdef\expandafter\gindexsort
       \expandafter{\the\glslongtok}%
  }%
}
\makeatother

\newcommand*{\gindex}{%
  \protect\index{\gindexsort @\the\glslongtok\space (\the\glsshorttok)}%
}

If you have other formatting commands, for example \textbf, they can be stripped in the same way:

\makeatletter
\renewcommand{\newacronymhook}{%
  {%
    \let\emph\@firstofone
    \let\textbf\@firstofone
    \expandafter\protected@xdef\expandafter\gindexsort
       \expandafter{\the\glslongtok}%
  }%
}
\makeatother

The .idx file now contains:

\indexentry{first}{1}
\indexentry{compact disk@compact disk (CD)}{1}
\indexentry{subsequent}{1}
\indexentry{compact disk@compact disk (CD)}{1}
\indexentry{United Nations@United Nations (UN)}{1}
\indexentry{italic text acronym@\emph  {italic text} acronym (ITA)}{1}
\indexentry{4-methyl-compound@4-methyl-compound (MC)}{1}
\indexentry{human growth hormone@human growth hormone (HGH)}{1}
\indexentry{human growth hormone@human growth hormone (HGH)}{1}
\indexentry{isoelectric point@isoelectric point (pI)}{1}

The resulting index now looks like:

Image of sorted index

Related Question