自定义关键人物条目索引的子条目

自定义关键人物条目索引的子条目

考虑由以下代码生成的后续索引:

\documentclass[12pt]{book}
\usepackage{imakeidx}
\makeindex
\usepackage{idxlayout}

    \begin{document}
    \large
    \par Here are some words.\index{Person, Key@{\scshape{Person, Key}}! In the beginning {\bf EVENT 1}}
    \newpage abc \newpage abc \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! This is followed by {\bf EVENT 2}}
    \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! Later, we have {\bf EVENT 3}}
    \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! Continuing with {\bf EVENT 4}}
    \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! So follows {\bf EVENT 5}}
    \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! Later comes {\bf EVENT 6}}
    \newpage abc \newpage abc \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! Followed by {\bf EVENT 7}}
    \newpage abc \newpage abc \newpage abc \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! Then comes {\bf EVENT 8}}
    \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! Shortly thereafter Event 8 comes {\bf EVENT 9}}
    \newpage abc 
    \par More words.\index{Person, Key@{\scshape{Person, Key}}! The end: {\bf EVENT 10}}
      
    
    \idxlayout{columns=1}
        \renewcommand\indexname{\color{RoyalRed}Index}
    %\renewcommand\indexname{Index.}
        \printindex
    \end{document}

在此处输入图片描述

我想要做的是简单地按时间顺序排列子条目,以便包含事件 1首先出现,然后是包含事件 2等等,直到包含活动 10。这样,相关页面也将按升序显示。

我该如何实现这一点?谢谢。

答案1

你可以使用类似

\index{Person, Key@\textsc{Person, Key}!\NOOP{<number>}<description>}

where<number>是自动生成的,\NOOP是一个不执行任何操作的宏,但会记录在.idx文件中。该数字用零填充为五位数字,因此将用于排序。

任何“Person, Key”对都会定义一个计数器,该计数器在每次使用时都会递增。当然,我们将使用宏来自动化该过程。

\documentclass[12pt]{book}
%\usepackage{xparse} % not needed with LaTeX 2020-10-01 or later
\usepackage{imakeidx}
\makeindex[columns=1]

\ExplSyntaxOn
\NewDocumentCommand{\NOOP}{m}{}

\NewDocumentCommand{\personevent}{mm}
 {
  \mlchristians_personevent:nn { #1 } { #2 }
 }

\cs_new_protected:Nn \mlchristians_personevent:nn
 {
  \int_if_exist:cF { g_mlchristians_personevent_#1_int }
   {
    \int_new:c { g_mlchristians_personevent_#1_int }
   }
  \int_gincr:c { g_mlchristians_personevent_#1_int }
  % emit the index entry
  \index{ #1 @ \textsc{#1}! \NOOP { \__mlchristians_pad:n { #1 } } #2 }
 }

\cs_new:Nn \__mlchristians_pad:n
 {
  \prg_replicate:nn { 5 - \tl_count:v { g_mlchristians_personevent_#1_int } } { 0 }
  \int_use:c { g_mlchristians_personevent_#1_int }
 }
\cs_generate_variant:Nn \tl_count:n { v }

\ExplSyntaxOff

\begin{document}

Here are some words.\personevent{Person, Key}{In the beginning \textbf{EVENT 1}}

\newpage abc \newpage abc \newpage abc

More words.\personevent{Person, Key}{This is followed by \textbf{EVENT 2}}
\newpage abc \newpage abc \newpage abc \newpage abc \newpage abc

More words.\personevent{Person, Key}{Later, we have \textbf{EVENT 3}}

\newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc

More words.\personevent{Person, Key}{Continuing with \textbf{EVENT 4}}

\newpage abc

More words.\personevent{Person, Key}{So follows \textbf{EVENT 5}}

\newpage abc

More words.\personevent{Person, Key}{Later comes \textbf{EVENT 6}}

\newpage abc \newpage abc \newpage abc

More words.\personevent{Person, Key}{Followed by \textbf{EVENT 7}}

\newpage abc \newpage abc \newpage abc \newpage abc

More words.\personevent{Person, Key}{Then comes \textbf{EVENT 8}}

\newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc \newpage abc

More words.\personevent{Person, Key}{Shortly thereafter Event 8 comes \textbf{EVENT 9}}

\newpage abc 

More words.\personevent{Person, Key}{The end: \textbf{EVENT 10}}

\printindex

\end{document}

请注意,这\textsc{text}是正确的语法,而{\scshape{text}}不是\bf已被弃用 25 年的语法。

在此处输入图片描述

.idx文件是

\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00001}In the beginning \textbf  {EVENT 1}}{1}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00002}This is followed by \textbf  {EVENT 2}}{4}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00003}Later, we have \textbf  {EVENT 3}}{9}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00004}Continuing with \textbf  {EVENT 4}}{16}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00005}So follows \textbf  {EVENT 5}}{17}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00006}Later comes \textbf  {EVENT 6}}{18}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00007}Followed by \textbf  {EVENT 7}}{21}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00008}Then comes \textbf  {EVENT 8}}{25}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00009}Shortly thereafter Event 8 comes \textbf  {EVENT 9}}{32}
\indexentry{Person, Key@\textsc  {Person, Key}!\NOOP {00010}The end: \textbf  {EVENT 10}}{33}

.ind文件是

\begin{theindex}

  \item \textsc  {Person, Key}
    \subitem \NOOP {00001}In the beginning \textbf  {EVENT 1}, 1
    \subitem \NOOP {00002}This is followed by \textbf  {EVENT 2}, 4
    \subitem \NOOP {00003}Later, we have \textbf  {EVENT 3}, 9
    \subitem \NOOP {00004}Continuing with \textbf  {EVENT 4}, 16
    \subitem \NOOP {00005}So follows \textbf  {EVENT 5}, 17
    \subitem \NOOP {00006}Later comes \textbf  {EVENT 6}, 18
    \subitem \NOOP {00007}Followed by \textbf  {EVENT 7}, 21
    \subitem \NOOP {00008}Then comes \textbf  {EVENT 8}, 25
    \subitem \NOOP {00009}Shortly thereafter Event 8 comes \textbf  {EVENT 9}, 
        32
    \subitem \NOOP {00010}The end: \textbf  {EVENT 10}, 33

\end{theindex}

相关内容