如何定义一个更适合段落的新索引命令?

如何定义一个更适合段落的新索引命令?

假设在我的文档中间有一段关于猫头鹰的段落: Here is a new paragraph about owls. Owls are extraordinary.

我想使用 imakeidx 为该段落建立索引,因此我写道, \index{owls}Here is a new paragraph about owls. Owls are extraordinary.

然而,问题在于,如果此段落最终出现在新页面 k 的顶部,则索引中的 owls 条目指的是上一页;它旁边有一个页面“k-1”,而不是“k”(例如,参见下面的最小工作示例)。

最简单的解决方案是将\index命令移到段落内: Here\index{owls} is a new paragraph about owls. Owls are extraordinary.

但对我来说,这并不理想。我的文档很长,许多段落都有许多相关\index命令。将这些\index命令移到其相应的段落内会使段落的可读性降低,难以编辑。

虽然上面描述的是 的最简单情况\index,但在指定页面范围时也存在类似的问题。我想简单地写成 , \index{owls|(}Here is a new paragraph about owls. Owls are extraordinary.\index{owls|)}而不是 Here\index{owls|(} is a new paragraph about owls. Owls are extraordinary.\index{owls|)}

这就引出了我的问题。如何定义一个新命令\indexn,以便在以下两种情况下生成索引中的正确页码: \indexn{owls}Here is a new paragraph about owls. Owls are extraordinary.\indexn{owls|(}Here is a new paragraph about owls. Owls are extraordinary.\indexn{owls|)}

以下是该问题的一个最小工作示例。

\documentclass{book}

\usepackage{imakeidx}
\makeindex

\begin{document}

% This code block just generates 552 copies of the word "lorem".
\newcount\zz
\loop
lorem
\advance\zz1
\ifnum\zz<552
\repeat

\index{owls}Here is a paragraph about owls.  Owls are extraordinary.
%\index{owls|(}Here is a new paragraph about owls.  Owls are extraordinary.\index{owls|)}

\printindex

\end{document}

答案1

\mbox{}\index{owls}或者\leavevmode\index{owls}会起作用

相关内容