是否有一個指令可以給出索引條目的頁碼?

是否有一個指令可以給出索引條目的頁碼?

是否有一個命令——例如,類似的命令\pages{pangolins}——代表索引條目“pangolins”的頁碼(和頁面範圍)?如果沒有,是否有任何優雅的方法來編寫這樣的命令,無論是否使用現有的索引包(如 imakeidx)?

以下是如何使用此類命令的最小範例:

\documentclass{book}

\usepackage{imakeidx}
\makeindex

\begin{document}

Here is a paragraph about pangolins\index{pangolins}.  Pangolins are the only mammals covered in scales.

Here is a paragraph about meerkats.  Meerkats are immune to the venom of scorpions and snakes.

Here is another paragraph about pangolins.  A single pangolin can consume 20,000 ants per day.  If you want to learn more about pangolins, see page \pages{pangolins}.

\end{document}

答案1

這也支援頁面範圍和多個頁面(但頁面範圍不應與單一引用混合)。它在對該問題的評論中使用了芭​​芭拉·比頓的想法。

\documentclass{book}
\usepackage{imakeidx}
\usepackage{xparse}

\usepackage{lipsum}

\ExplSyntaxOn
\NewDocumentCommand{\readindex}{O{\c_sys_jobname_str}}
 {
  \group_begin:
  \cs_set_eq:NN \indexentry \readindexentry
  \file_if_exist_input:n { #1.idx }
  \group_end:
 }

\NewDocumentCommand{\pages}{m}
 {
  \prop_item:Nn \g_readindex_prop { #1 }
 }

\NewDocumentCommand{\readindexentry}{>{\SplitArgument{1}{|}}m m}
 {
  \readindex_process:nnn #1 { #2 }
 }

\prop_new:N \g_readindex_prop

\cs_new_protected:Nn \readindex_process:nnn
 {
  \str_case:nnF { #2 }
   {
    { ( }{ \readindex_start:nn { #1 } { #3 } }
    { ) }{ \readindex_end:nn   { #1 } { #3 } }
   }
   { \readindex_standard:nn { #1 } { #3 } }
 }

\cs_new_protected:Nn \readindex_standard:nn
 {
  \prop_if_in:NnTF \g_readindex_prop { #1 }
   {
    \prop_gput:Nnx \g_readindex_prop { #1 }
     { \prop_item:Nn \g_readindex_prop { #1 } , ~ #2 }
   }
   {
    \prop_gput:Nnn \g_readindex_prop { #1 } { #2 }
   }
 }

\cs_new_protected:Nn \readindex_start:nn
 {
  \prop_gput:Nnn \g_readindex_prop { #1 } { #2 }
 }
\cs_new_protected:Nn \readindex_end:nn
 {
  \prop_gput:Nnx \g_readindex_prop { #1 }
   { \prop_item:Nn \g_readindex_prop { #1 } -- #2 }
 }

\ExplSyntaxOff

\readindex
\makeindex % must be ***after*** \readindex

\begin{document}

For elephants, see \pages{elephants}.

Here we also talk about unicorns\index{unicorns}, treated on \pages{unicorns}.

Here is a paragraph about pangolins\index{pangolins}.  
Pangolins are the only mammals covered in scales.

Here is a paragraph about meerkats.  Meerkats are immune to 
the venom of scorpions and snakes.

Here is another paragraph about pangolins.  A single pangolin 
can consume 20,000 ants per day.  If you want to learn more about 
pangolins, see page \pages{pangolins}.

Now we talk about elephants\index{elephants|(}
\lipsum[1-10]
End of elephant talk\index{elephants|)}.

Again a unicorn\index{unicorns}.

\end{document}

在此輸入影像描述

人們可以根據儲存在屬性清單中的值添加自動“page”-“pages”前綴。

支援多個索引,但\readindex每個索引都應使用特定的命令:\readindex[<index name>],其中名稱是相應.idx檔案的檔案名稱。

答案2

鑑於相當精確的您所做的承諾(每個項目只有一個索引條目,只有一頁參考等)您可以更改命令,\index以便它也自動創建一個具有相同名稱的標籤。然後你只需使用普通\pageref命令來交叉引用它即可。

\documentclass{article}
\usepackage{makeidx}

\makeatletter
\def\@wrindex#1{%
\protected@write\@indexfile{}%
{\string\indexentry{#1}{\thepage}}%
\protected@write\@auxout{}%
{\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
\endgroup
\@esphack}
\makeatother

\makeindex

\begin{document}


If \index{pangolins} are referred to here, we can wait a little while and then

\clearpage


Refer to the place (\pageref{pangolins}) where we previously referred to them!

\printindex

\end{document}

答案3

為了完善答案,在嘗試各種選項時出現的一種簡單方法是使用 來標記特定單詞\phantomsection\label然後使用\pageref來將頁碼放置在需要的位置:

\documentclass{book}

\newcommand{\ps}{\phantomsection}

\begin{document}

Here is a paragraph about {\ps}pangolins\label{pangolins}.

Here is another paragraph about pangolins.  If you want to learn more about pangolins, see page \pageref{pangolins}.

\end{document}

這似乎在我嘗試過的情況下有效,但我對 LaTeX 幕後發生的事情了解有限;這種方法可能存在我沒有意識到的缺陷。 (鼓勵批評。)

相關內容