如何定義一個更適合段落的新索引指令?

如何定義一個更適合段落的新索引指令?

假設在我的文檔中間有一段關於貓頭鷹的段落: 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}會起作用

相關內容