TransWikia.com

Indexing in partial decimal order, Luke 10:25 after Luke 1:5

TeX - LaTeX Asked on August 10, 2021

I’m working on a document that makes a lot of references to the bible, and I have two indexes set up, one of topics and the other by verse. Currently, in the index by verse, Luke 10:25 is indexed before Luke 1:5, for example, because that’s the alphabetical order. Is there any easy way to get the entries to appear in the order a human would expect based on the three fields (alphabetically by book, numerically by chapter, verse)?

I came across the package bibleref, but it seems to not handle this. According to the docs, what it seems to be designed to do is allow you to index the books of the bible according to their biblical order rather than alphabetically, but that’s not what I care about.

3 Answers

enter image description here

documentclass{article}

usepackage{imakeidx}

makeindex

newcommandbindex[3]{index{#1:stringnumberstringnumexprthenumexpr1000+#2relax-1000:stringnumberstringnumexprthenumexpr1000+#3relax-1000}}
begin{document}

zzzbindex{Luke}{10}{25}  zzzbindex{Luke}{1}{5} zzzbindex{Matthew}{1}{1}

printindex
end{document}

If you look in the generated .ind file you see what it is doing:

begin{theindex}

  item Luke:numbernumexpr1001-1000:numbernumexpr1005-1000, 1
  item Luke:numbernumexpr1010-1000:numbernumexpr1025-1000, 1

  indexspace

  item Matthew:numbernumexpr1001-1000:numbernumexpr1001-1000, 1

end{theindex}

1000 gets added so all numbers end up the same length, then (after sorting) 1000 is subtracted and number applied to remove leading zeros.

Correct answer by David Carlisle on August 10, 2021

Pad the numbers with zeros and use the @ feature for sorting.

documentclass{article}
usepackage{imakeidx}

makeindex

ExplSyntaxOn
NewDocumentCommand{indexverse}{mmm}
 {% #1 = name, #2 = chapter, #3 = verse
  index
   {% the sorting part
    #1prg_replicate:nn{4-tl_count:n{#2}}{0}#2prg_replicate:nn{4-tl_count:n{#3}}{0}#3
    @
    #1~#2:#3
   }
 }
ExplSyntaxOff

begin{document}

text
indexverse{Luke}{10}{25}
indexverse{Luke}{10}{5}

printindex

end{document}

This is the .idx file

indexentry{Luke00100025@Luke 10:25}{1}
indexentry{Luke00100005@Luke 10:5}{1}

and the .ind file

begin{theindex}

  item Luke 10:5, 1
  item Luke 10:25, 1

end{theindex}

Answered by egreg on August 10, 2021

Thanks, egreg and David Carlisle, for your help! Having seen what technique you used, I decided to reimplement the same technique outside of latex, like this:

perl -i -pe 's/(d+)/$$1+1000/ge' jesus.idx
makeindex jesus.idx -o jesus.ind 
perl -i -pe 's/(dddd)/$$1-1000/ge' jesus.ind

A disadvantage is that this method won't work for people running Windows. Advantages are that it only adds two lines of code to my build script, it's pretty readable, and it correctly handles strings that are not quite in the same format, e.g., Luke 5 or Matthew 8:27-9:1. So I thought I would post this as a self-answer, and that way others coming across this can have one more method to choose from.

Answered by Ben Crowell on August 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP