索引エントリのページ番号を表示するコマンドはありますか?

索引エントリのページ番号を表示するコマンドはありますか?

たとえば、索引エントリ「pangolins」のページ番号 (およびページ範囲) を表す --- のようなコマンドはありますか\pages{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

むしろ正確な約束したこと (項目ごとに 1 つのインデックス エントリのみ、ページ参照は 1 つだけなど) を実行するには、コマンドを変更して\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 の舞台裏で何が起こっているかについては私にはあまり理解がないので、このアプローチには私が理解していない欠陥があるかもしれません。(批判を歓迎します。)

関連情報