색인 항목의 페이지 번호를 제공하는 명령이 있습니까?

색인 항목의 페이지 번호를 제공하는 명령이 있습니까?

\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}

여기에 이미지 설명을 입력하세요

속성 목록에 저장된 값을 기반으로 자동 "페이지"-"페이지" 접두사를 추가할 수 있습니다.

여러 인덱스가 지원되지만 \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의 뒤에서 무슨 일이 일어나고 있는지에 대한 이해가 제한적입니다. 이 접근 방식은 내가 인식하지 못한 방식으로 결함이 있을 수 있습니다. (비판을 권장합니다.)

관련 정보