1

Suppose I would like to sort an index file called "xchp.idx" that includes the following information:

    \indexentry{{Chp  1 } cherry}{1}
    \indexentry{{Chp  1 } apple}{1}
    \indexentry{{Chp  1 } banana}{1}
    \indexentry{{Chp  2 } banana}{1}
    \indexentry{{Chp  2 } cherry}{1}
    \indexentry{{Chp  2 } apple}{1}
    \indexentry{{Chp  10 } banana}{1}
    \indexentry{{Chp  10 } cherry}{1}
    \indexentry{{Chp  10 } apple}{1}
    \indexentry{{Chp  12 } apple}{1}
    \indexentry{{Chp  12 } cherry}{1}
    \indexentry{{Chp  12 } banana}{1}

If I attempt such a sort operation using

    makeindex xchp.idx

then I get

    \item {Chp  1 } apple, 1  
    \item {Chp  1 } banana, 1 
    \item {Chp  1 } cherry, 1 
    \item {Chp  10 } apple, 1 
    \item {Chp  10 } banana, 1
    \item {Chp  10 } cherry, 1
    \item {Chp  12 } apple, 1 
    \item {Chp  12 } banana, 1
    \item {Chp  12 } cherry, 1
    \item {Chp  2 } apple, 1  
    \item {Chp  2 } banana, 1 
    \item {Chp  2 } cherry, 1 

This is sorting everything lexicographically, whereas I would like the second token in each string to be sorted numerically like this:

    \item {Chp  1 } apple, 1  
    \item {Chp  1 } banana, 1 
    \item {Chp  1 } cherry, 1 
    \item {Chp  2 } apple, 1  
    \item {Chp  2 } banana, 1 
    \item {Chp  2 } cherry, 1 
    \item {Chp  10 } apple, 1 
    \item {Chp  10 } banana, 1
    \item {Chp  10 } cherry, 1
    \item {Chp  12 } apple, 1 
    \item {Chp  12 } banana, 1
    \item {Chp  12 } cherry, 1

To that end, I might try to use the "page_precedence" option in a style file called "xchp.ist" containing this:

    page_precedence "anaan"

where a=alphabetic lower and n=numeric.

Here I might be tempted to complain, that while this option is mentioned here and there on stackexchange, there seems to be no clear documentation to its use...not even in Table 11.1 of "The LaTeX Companion" second edition. But then I would stop myself from complaining, and profusely apologize, because I am getting makeindex for free ... and so who am I to complain?

Now if I try

    makeindex -s xchp.ist xchp.idx

I get

    \item {Chp  1 } apple, 1  
    \item {Chp  1 } banana, 1 
    \item {Chp  1 } cherry, 1     
    \item {Chp  10 } apple, 1 
    \item {Chp  10 } banana, 1
    \item {Chp  10 } cherry, 1
    \item {Chp  12 } apple, 1 
    \item {Chp  12 } banana, 1
    \item {Chp  12 } cherry, 1
    \item {Chp  2 } apple, 1  
    \item {Chp  2 } banana, 1 
    \item {Chp  2 } cherry, 1 

Which is still not what I want. Can anyone help me out? I suspect that xindy may have a solution. But that may be another post. For now, is there a way to perform the desired operation with makeindex?

  • If the sort value is purely numerical, makeindex will perform a numerical sort, so you'd need the entries to be in the form \indexentry{1@{Chp 1 } cherry}{1}. – Nicola Talbot Nov 23 '17 at 22:44
  • Assume the xchp.idx file is non-editable (carved in stone or burned into ROM, or a protected world heritage site). Is there a solution under this constraint? – Daniel J. Greenhoe Nov 23 '17 at 22:56
  • Can't think of how to do it in makeindex without changing the index file. Are you willing to use a pipe (sed etc)? The page_precedence setting relates to the page number ordering (in the second argument of \indexentry). So if you have \indexentry{foo}{IX}, \indexentry{foo}{10} and \indexentry{foo}{xii} then page_precedence "rnR" will put the lower-case roman numerals at the start of the page list, followed by the arabic numerals, followed by upper case roman numerals. So you end up with foo, xii, 10, IX. – Nicola Talbot Nov 23 '17 at 23:13
  • What about using xindy? Interestingly, it seems to work out of the box. Try texindy xchap.idx – David Purton Nov 24 '17 at 4:31
  • Oh, I just read your last sentence! Sorry! Never mind, perhaps my answer will give you enough information to use xindy. – David Purton Nov 24 '17 at 4:56
1

You can sort it correctly using xindy.

Compile it using pdflatex -shell-escape. If it doesn't run xindy, you might have to do that manually (texindy), then run pdflatex again.

\documentclass{article}
\usepackage[nonewpage,xindy]{imakeidx}
\makeindex
\indexsetup{firstpagestyle=empty}
\begin{document}
Filler text.
\index{{Chp  1 } cherry}
\index{{Chp  1 } apple}
\index{{Chp  1 } banana}
\index{{Chp  2 } banana}
\index{{Chp  2 } cherry}
\index{{Chp  2 } apple}
\index{{Chp  10 } banana}
\index{{Chp  10 } cherry}
\index{{Chp  10 } apple}
\index{{Chp  12 } apple}
\index{{Chp  12 } cherry}
\index{{Chp  12 } banana}
\printindex
\end{document}

enter image description here

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.