4

The following MWE

\documentclass{book}
\usepackage{splitidx}
\makeindex
\newindex{idx}
\newindex{aut}    
\begin{document}    
\setcounter{page}{124}\index{Topic1|(}\index{Topic2|(}Text\newpage    
\setcounter{page}{126}\index{Topic1|)}Text\newpage    
\setcounter{page}{130}\index{Topic2|)}Text\newpage     
\printindex[idx][Subject Index]     
\end{document}

produces these index entries:

Topic1, 124-126

Topic2, 124-130

I would like to have:

Topic1, 124-6

Topic2, 124-30

Can this be done using splitidx?

From egreg's comment it seems to me I didn't ask a clear question. I'm happy to have the same style for all indices. What I'd like is a different format of the page range. Instead of "124-126", I'd like the output to be "124-6", so the "12" from "126" is dropped. Similarly, instead of "124-130", drop the "1" from "130", to get "124-30".

  • With splitindex you can apply only one index style to all indices. – egreg Nov 14 '18 at 23:18
1

With splitindex you can use only one index style for every index. This is not a problem if you use ranges only for an index or want that all indices share the same property.

The solution here uses imakeidx, because it's easier to specify different styles for the various indices and doesn't require running separately splitindex or makeindex.

I define an index style (using filecontents* to make the example self-contained); it encapsulates the page number(s) as argument to \checkrange. If -- doesn't appear, the page number(s) are simply printed. Otherwise the check is performed.

If the number of digits differ, both numbers are printed (separated by --). Otherwise the first number is printed and digits are compared with the second number. Until the digits are the same, nothing is printed. Once a different digit is found, it and the remaining digits in the second number are printed.

\begin{filecontents*}{subjind.ist}
delim_0 ", \\checkrange{"
delim_t "}"
\end{filecontents*}

\documentclass{book}
\usepackage{xparse}
\usepackage{imakeidx}

\makeindex[title=Subject Index,options=-s subjind]

\ExplSyntaxOn
\NewDocumentCommand{\checkrange}{>{\SplitArgument{1}{--}}m}
 {
  \formatrange#1
 }
\NewDocumentCommand{\formatrange}{mm}
 {
  \IfNoValueTF { #2 }
   { % no --, it's not a range
    #1
   }
   { % it's a range
    \egreg_range:nn { #1 } { #2 }
   }
 }

\bool_new:N \l__egreg_range_compare_bool

\cs_new_protected:Nn \egreg_range:nn
 {
  % print the start number
  #1--
  \int_compare:nNnTF { \str_count:n { #1 } } = { \str_count:n { #2 } }
   {% same number of digits, remove equal ones at the start
    \__egreg_range_compare:nn { #1 } { #2 }
   }
   {% different number of digits, print both
    #2
   }
 }
\cs_new_protected:Nn \__egreg_range_compare:nn
 {
  \bool_set_true:N \l__egreg_range_compare_bool
  % do a loop on the digits in the first number
  \int_step_inline:nn { \str_count:n { #1 } }
   {
    \bool_if:NTF \l__egreg_range_compare_bool
     {
      % if the digits coincide, print nothing
      \str_if_eq:eeF { \str_item:nn { #1 } { ##1 } } { \str_item:nn { #2 } { ##1 } }
       {
        \str_item:nn { #2 } { ##1 }
        % at the first differing digit, don't compare any longer
        \bool_set_false:N \l__egreg_range_compare_bool
       }
     }
     { \str_item:nn { #2 } { ##1 } }
   }
 }
\ExplSyntaxOff

\begin{document}
\setcounter{page}{95}\index{Topic0|(}Text\newpage
\setcounter{page}{101}\index{Topic0|)}Text\newpage
\setcounter{page}{124}\index{Topic1|(}\index{Topic2|(}Text\newpage
\setcounter{page}{126}\index{Topic1|)}Text\newpage
\setcounter{page}{134}\index{Topic2|)}Text\newpage
\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.