0

This is a follow-up question to Page range truncation with imakeidx. The solution presented there does not work if there are any "see" or "see also" entries; is there a way to modify it to take this into account?

MWE:

\documentclass[12pt]{memoir}

\begin{filecontents*}{Test.ist}
delim_0 ", \\checkrange{"
delim_1 ", \\checkrange{"
delim_2 ", \\checkrange{"
delim_t "}"
\end{filecontents*}

\usepackage{xparse}
\usepackage{imakeidx}

\makeindex[options=-s Test]

\ExplSyntaxOn
\NewDocumentCommand{\checkrange}{m}
 {
  \seq_set_from_clist:Nn \l__ssind_in_seq { #1 }
  \seq_set_map:NNn \l__ssind_out_seq \l__ssind_in_seq { \checkrangeaux{##1} }
  \seq_use:Nn \l__ssind_out_seq {,~}
 }
\seq_new:N \l__ssind_in_seq
\seq_new:N \l__ssind_out_seq

\NewDocumentCommand{\checkrangeaux}{>{\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}

\index{consectetur|see {dolor}}

\setcounter{page}{95}Lorem\index{lorem|(} ipsum dolor\index{dolor}
\newpage
\setcounter{page}{96}sit amet\index{lorem|)}, consectetur

\printindex

\end{document}

Your Answer

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

Browse other questions tagged or ask your own question.