索引中的条目顺序

索引中的条目顺序

我正在为一本诗集制作首行索引,希望诗行的标点符号不会影响它们在索引中的显示顺序。例如:

现在条目按以下顺序出现(我认为是因为逗号位于分号之前):

Winter has fled, grass..., 234

Winter has fled; already..., 178

我希望它们按以下顺序出现:

Winter has fled; already..., 178

Winter has fled, grass..., 234

当我运行以下命令时,我得到了第一个选项,并且我想得到第二个选项。

\documentclass[10pt]{octavo}
\usepackage{imakeidx}
\makeindex[columns=1]

\begin{document}

Winter has fled, grass \index{Winter has fled, grass}

Winter has fled; already \index{Winter has fled; already}

\printindex
\end{document}

答案1

你可以定义一个命令\indexnp(无标点符号),它会自动删除标点符号,并使用说明符形成正确的结构@,因此

\indexnp{Winter has fled, grass}

变成

\index{Winter has fled grass@Winter has fled, grass}

以下是代码:参数存储在一个标记列表变量中,其中标点符号被删除。然后所需的

\index{<text, no punctuation>@<text>}

已生成。

\documentclass[10pt]{octavo}
\usepackage{imakeidx}
\makeindex[columns=1]

\ExplSyntaxOn
\NewDocumentCommand{\indexnp}{m}
 {
  \egreg_indexnp:n { #1 }
 }

\tl_new:N \l__egreg_indexnp_tl

\cs_new_protected:Nn \egreg_indexnp:n
 {
  \tl_set:Nn \l__egreg_indexnp_tl { #1 }
  \regex_replace_all:nnN { [\.\,\;\:] } { } \l__egreg_indexnp_tl
  \__egreg_indexnp_do:Vn \l__egreg_indexnp_tl { #1 }
 }

\cs_new_protected:Nn \__egreg_indexnp_do:nn
 {
  \index{#1@#2}
 }
\cs_generate_variant:Nn \__egreg_indexnp_do:nn { V }

\ExplSyntaxOff

\begin{document}

Winter has fled, grass \indexnp{Winter has fled, grass}

Winter has fled; already \indexnp{Winter has fled; already}

\printindex
\end{document}

在此处输入图片描述

答案2

您可以使用以下方式将用于排序的字符串与排版文本分开:@

在此处输入图片描述

\documentclass[10pt]{octavo}
\usepackage{imakeidx}
\makeindex[columns=1]

\begin{document}

Winter has fled, grass \index{Winter has fled grass@Winter has fled, grass}

Winter has fled; already \index{Winter has fled already@Winter has fled; already}

\printindex
\end{document}

相关内容