如何引用索引词

如何引用索引词

给定一个带有索引词的文本,例如minimum,是否有可能有一个命令,称为\DEX{},它将打印该词的后续出现并将其链接到原始出现?对于屏幕上的 PDF,这将允许读者只需单击他/她想要检查的单词即可绕过索引,而\Cpageref{minimum}\Cpageref{index:minimum}?)将允许读者类似地绕过打印副本上的索引。

这能适用于在 中定义的单词tcolorbox吗?不过,就我而言,这将是锦上添花,因为这部巨作是为初学者编写的,大多数单词都没有正式定义,只有极少数单词有定义。此外,我已经使用cleveref它了。所以,只是问问。

这是一个 MWE(不是最小的,因为我需要 Heiko Oberdiek 提供的“快速而肮脏的破解”来加载 showidx,并且我还包括了一个 tcolorbox,以防有人想尝试它。)

    \documentclass[11pt]{book}  
    \usepackage{makeidx}
    \makeindex
    \usepackage{tcolorbox}
%==================Begin HYPERREF
    \usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]

%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx

    \usepackage{cleveref}
%Begin the Sharpe code as modified by Schulz (2016-02-1)            
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
\newcommand{\INDEX}[1]{\index{#1}}
%End the Sharpe code as modified by Schulz (2016-02-1)          

\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={definition}{definitions},
Crefname={Definition}{Definitions},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=DEFINITION \thetcbcounter,%
#1,%Required for references.
}%


    \begin{document}

A \textbf{minimum}\INDEX{minimum} of a function is blah blah.

\begin{greement}[label={abc:3-1}]{Maximum}
  An input whose output is larger than the outputs of nearby inputs
\end{greement}


\newpage

A function can have more than one $\backslash$DEX\{minimum\} ($\backslash$Cpageref\{minimum\}.)


(The command $\backslash$DEX would print  the word minimum and link it to the above occurrence. Hopefully, $\backslash$Cpageref would still work too.)

\printindex
    \end{document}

这是 pdf(不含\newpage和不含索引页)。请注意右上角的 swhowidx:

在此处输入图片描述

答案1

也许我不明白你想做什么,但在我看来,你需要做的就是让你的\INDEX命令\label{...}为你的索引条目添加一个,然后你的命令会引用它。因为你想在 PDF文件\DEX中创建一个链接,你应该使用\hyperref超链接包来创建此链接,而不是\ref。为了保险起见,\DEX还应插入\Cpageref命令。

把它们放在一起,你的\INDEX\DEX命令变成:

\newcommand{\INDEX}[1]{\index{#1}\label{ind:#1}}
\newcommand\DEX[1]{\hyperref[ind:#1]{#1} (\Cpageref{ind:#1})}

\label我已经使表单的索引自动生成,ind:...以便它们不太可能与其他标签发生冲突。

到此为止,这是第 2 页的输出,显示了鼠标悬停在单词最低限度

在此处输入图片描述

(我使用 skim 查看 PDF,它的众多优秀功能之一就是鼠标悬停功能。例如,Acrobat 就不具备此功能。)

编辑

正如评论中所述,这种方法在索引数学时会出错,例如\INDEX{$\pm$}。这实际上并不奇怪,因为在索引或标签键中包含数学几乎肯定会导致问题,但这意味着为了应对此类情况,我们需要能够手动覆盖正在使用的标签。最容易做到这一点的是使用解析允许使用定义\INDEX\DEX作为的包

\NewDocumentCommand\INDEX{ O{#2} m}{\index{#2}\label{ind:#1}}
\NewDocumentCommand\DEX{ O{#2} m }{\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})}

这意味着\INDEX现在有一个可选参数,默认为第二个参数的值,用于标签中。因此,要索引,$\pm$我们现在可以输入

\INDEX[pm]{$\pm$}

类似地,\DEX也接受可选参数,因此要引用 $\pm$,我们将使用\DEX[pm]{$\pm$}。我们只需要在索引的内容在某种程度上“复杂”时(例如在数学模式下)使用可选参数和\INDEX\DEX否则我们像以前一样使用这两个宏。

[顺便说一下,\newcommand也适用于可选参数(参见定义带参数的宏)。使用的唯一原因\NewDocumentCommand是它具有更简单的处理默认值的机制。

完整代码如下:

\documentclass[11pt]{book}
    \usepackage{makeidx}
    \makeindex
    \usepackage{tcolorbox}
%==================Begin HYPERREF
    \usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]

%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx

    \usepackage{cleveref}
%Begin the Sharpe code as modified by Schulz (2016-02-1)
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
%End the Sharpe code as modified by Schulz (2016-02-1)

\newcommand{\INDEX}[2][]{%
\if\relax\detokenize{#1}\relax\index{#2}\label{ind:#2}%
   \else\index{#2}\label{ind:#1}\fi%
}
\newcommand{\DEX}[2][]{%
   \if\relax\detokenize{#1}\relax\hyperref[ind:#2]{#2} (\Cpageref{ind:#2})%
   \else\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})\fi%
}

\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={definition}{definitions},
Crefname={Definition}{Definitions},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=DEFINITION \thetcbcounter,%
#1,%Required for references.
}%



    \begin{document}

A \textbf{minimum}\INDEX{minimum} of a function is blah blah.

\begin{greement}[label={abc:3-1}]{Maximum}
  An input whose output is larger than the outputs of nearby inputs
\end{greement}

\INDEX[pm]{$\pm$}
\newpage

A function can have more than one \DEX{minimum}.

A function can have more than one \DEX{minimum}.

\chapter{one} \textbf{Andy}\INDEX[]{Andy} \chapter{two} \textbf{Beth}\INDEX[]{Beth} \chapter{three} \textbf{Carla}\INDEX[]{Carla} \chapter`{four}` \DEX[]{Andy} \DEX[]{Beth} \DEX[]{Carla}.

\printindex


    \end{document}

相关内容