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?






  title=\idxKeywordName{} index,

\newcommand{\idxKwEntryD}{Z} % why is this entry not in order?


  \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}



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:


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


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


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


and the ind-file:


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


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


Similar question: Correct sorting of \index entries containing macros


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:




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


  \item Item A \nindex[\idxKeywordName]{\idxKwEntryA}
  \item Item B \nindex[\idxKeywordName]{\idxKwEntryB}
  \item Item C \nindex[\idxKeywordName]{\idxKwEntryC}




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.