1

I have a question concerning the order in an index created with imakeidx. I wanted to define my own commands for index entry names with \newcommand{$VARIABLENAME}{$VARIABLEVALUE} in order to reuse them in the whole document.

However, if I do that, the order in the index is created by the name of the command rather than the content. I found that the index-files use the command and not the content.

How can I circumvent this?


MWE

\documentclass{article}

\usepackage{imakeidx}

\indexsetup{%
  level=\section*,%
  toclevel=section,%
}

\newcommand{\idxKeywordName}{Keyword}

\makeindex[%
  name=\idxKeywordName,%
  title=\idxKeywordName{} index,
  intoc,
]

\newcommand{\idxKwEntryA}{A}
\newcommand{\idxKwEntryB}{B}
\newcommand{\idxKwEntryC}{C}
\newcommand{\idxKwEntryD}{Z} % why is this entry not in order?
\newcommand{\idxKwEntryE}{E}
\newcommand{\idxKwEntryF}{F}

\begin{document}

\begin{itemize}
  \item Item A \index[\idxKeywordName]{\idxKwEntryA}
  \item Item B \index[\idxKeywordName]{\idxKwEntryB}
  \item Item C \index[\idxKeywordName]{\idxKwEntryC}
  \item Item D \index[\idxKeywordName]{\idxKwEntryD}
  \item Item E \index[\idxKeywordName]{\idxKwEntryE}
  \item Item F \index[\idxKeywordName]{\idxKwEntryF}
\end{itemize}

\printindex[\idxKeywordName]

\end{document}

The resulting index looks like this:

enter image description here

where the Z entry is obviously not where it is supposed to be. I found the reason for the "wrong" order in the idx-file:

\indexentry{\idxKwEntryA}{1}
\indexentry{\idxKwEntryB}{1}
\indexentry{\idxKwEntryC}{1}
\indexentry{\idxKwEntryD}{1}
\indexentry{\idxKwEntryE}{1}
\indexentry{\idxKwEntryF}{1}

Here, the index entry is defined by the command \idxKwEntryX rather than its content. This gives the ind-file:

\begin{theindex}

  \item \idxKwEntryA, 1
  \item \idxKwEntryB, 1
  \item \idxKwEntryC, 1
  \item \idxKwEntryD, 1
  \item \idxKwEntryE, 1
  \item \idxKwEntryF, 1

\end{theindex}

Here is what I would suspect based on the MWE for the ilg-file:

\indexentry{A}{1}
\indexentry{B}{1}
\indexentry{C}{1}
\indexentry{Z}{1}
\indexentry{E}{1}
\indexentry{F}{1}

and the ind-file:

\begin{theindex}

  \item A, 1
  \item B, 1
  \item C, 1
  \item E, 1
  \item F, 1
  \item Z, 1

\end{theindex}

Why is that and how can I get an order based on the actual content?


Update

Similar question: Correct sorting of \index entries containing macros

2

To sort the index according to content, the arguments need to be expanded. An easy way to expand an argument is to pass it through an identity function:

MWE:

\documentclass{article}
\usepackage{imakeidx}
\newcommand{\idxKeywordName}{Keyword}

\newcommand{\nindex}[2][]{\index[#1]{#2}}

\makeindex[name=\idxKeywordName,title=\idxKeywordName{} index,columns=1]

\newcommand{\idxKwEntryA}{A}
\newcommand{\idxKwEntryB}{Z}
\newcommand{\idxKwEntryC}{C}

\begin{document}
\begin{itemize}
  \item Item A \nindex[\idxKeywordName]{\idxKwEntryA}
  \item Item B \nindex[\idxKeywordName]{\idxKwEntryB}
  \item Item C \nindex[\idxKeywordName]{\idxKwEntryC}
\end{itemize}

\printindex[\idxKeywordName]

\end{document}

Result:

enter image description here

Note that the entries are now separated by an \indexspace (i.e., small vertical space) because the different initial characters are recognized, as opposed to the original example where all the entries are in the same group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.