Formated text in acronym sorted alphabetically in Index
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: