是否有命令可以提供索引条目的页码?

是否有命令可以提供索引条目的页码?

是否有命令(例如,类似命令\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

这也支持页面范围和多个页面(但页面范围不应与单个引用混合)。它使用了 Barbara Beeton 在问题评论中的想法。

\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 幕后发生的事情了解有限;这种方法可能存在一些我尚未意识到的缺陷。(欢迎批评。)

相关内容