将索引术语链接到索引条目

将索引术语链接到索引条目

我正在尝试创建从\index命令到相应索引条目的链接。hyperref加载包时,索引中的页码默认链接到\index该条目的命令所在的页面。但反过来(正向链接)则行不通。

在检查 的正确性时index,如果我可以单击 PDF 中的索引词(在下面的 MWE 中的宏#2ati),它会将我带到索引中出现相应条目的位置,这将很有用。实际索引有很多页,因此仅确定索引开始的页码是不够的,因为给定的条目实际上可能出现在几页之后。

我的第一个想法是向索引条目添加标签,然后从索引词创建超链接。尝试这样做反映在atiwl下面注释的宏中。不幸的是,这没有编译。(即使编译了,向每个条目添加唯一标签也会有问题)。测试使用是否label可行的第二个想法是直接向.ind文件添加一个标签,如下所示:

\item Index entry 1, \hyperpage{1}\label{linkedentry}

虽然它已编译,但生成的链接在 PDF 中却不起作用。

梅威瑟:

\documentclass{book}
\usepackage{blindtext}
\usepackage{makeidx}
\makeindex
\def \ati#1#2{#2\index{#1}} % "Add to index" macro
% \def \atiwl#1#2{\hyperlink{linkedentry}{#2}\index{#1\label{linkedentry}}} % "Add to index with label" macro
\usepackage{hyperref}
\begin{document}
\blindtext

Some text and \ati{Index entry 1}{indexed term}.

\blindtext

Some text and \ati{Index entry 2}{another indexed term}.

% \blindtext

% Some text and \atiwl{Entry with label}{some indexed text}.

\printindex

\end{document}

我不确定这是否与解决方案相关,但我使用texindy索引处理器。实际上,我使用splitidx包和附带的splitindex命令行工具,因为我需要许多索引。然而,在 MWE 中,我曾经makeidx让它对有同样问题的其他人更通用。

有没有办法从宏创建#2ati相应索引条目的链接?

答案1

这也许就是所要求的:一个\ati- 宏,它允许根据特殊计数器使用自动生成的标签进行前向-后向链接。

只要此计数器仅被更改\ati而不被其他方式操纵,标签就是唯一的。

使用带星号的命令可以防止超链接到索引,并使用可选参数(与 结合)来实现 的 -macroimakeidx的特殊功能。\indeximakeidx

\documentclass{book}
\usepackage{blindtext}
\usepackage{imakeidx}
\usepackage{xparse}
\newcounter{indcntr}
\makeindex


\NewDocumentCommand{\ati}{somm}{%
  \IfBooleanTF{#1}{%
    \IfValueTF{#2}{%
      #4\index[#2]{#3}%
    }{%
      #4\index{#3}%
    }%
  }{%
    \stepcounter{indcntr}%
    \protect\hyperlink{ind::\number\value{indcntr}}{#4}%
    \IfValueTF{#2}{%
      \index[#2]{\protect\hypertarget{ind::\number\value{indcntr}}{#3}}%
    }{%
      \index{\protect\hypertarget{ind::\number\value{indcntr}}{#3}}%
    }%
  }%
}

\usepackage{hyperref}
\begin{document}
\blindtext

Some text and \ati{Index entry 1}{indexed term}

\blindtext

Some text and \ati*{Index entry 2}{another indexed term}.

\blindtext[4]

 Some text and \ati{Entry with label}{some indexed text}.

\printindex

\end{document}

答案2

这是否更符合您的想象?

\documentclass{book}
\usepackage{blindtext}
\usepackage{makeidx}
\makeindex
\usepackage{hyperref}

\newcounter{indexlink}

\newcommand{\Index}[1]{\stepcounter{indexlink}%
  \index{#1\string\raisebox{\baselineskip}[0pt]{\string\hypertarget{indexfrom\theindexlink}{}}}%
  \hyperlink{indexfrom\theindexlink}{#1}}

\makeatletter
\def\@wrindex#1{%
  \raisebox{\baselineskip}[0pt]{\hypertarget{indexto\theindexlink}{}}%
  \protected@write\@indexfile{}%
     {\string\indexentry{#1|hyperlink{indexto\theindexlink}}{\thepage}}%
\endgroup\@esphack}% started by \index
\makeatother

\begin{document}
\blindtext

Some text and \Index{indexed term}.

\blindtext

Some text and \Index{another indexed term}.

% \blindtext

% Some text and \atiwl{Entry with label}{some indexed text}.

\printindex

\end{document}

答案3

我需要与@Rafal类似的功能,包括希望在索引中的一行中列出同一索引条目的多次出现。我意识到这可以通过稍微修改@Christian Hupfer提供的解决方案来实现:可以使用索引条目本身来生成超链接,而不是定义索引计数器。以下是使用自制命令 \xindex 实现此功能的代码。出于我自己的目的,我添加了一些附加功能:* 版本 \xindex* 为添加到正文中的单词和索引中的相应页码生成粗体。可选参数允许将附加信息写入索引(我使用它来提供某些索引条目的翻译)。并且可以使用其他参数在索引中创建子条目或进行交叉引用。

\documentclass{book}
\usepackage{xparse}
\usepackage{color}
\definecolor{darkred}{rgb}{0.7,0,0}
\usepackage{imakeidx}
\makeindex[columns=1]

\NewDocumentCommand{\xindex}{sommmmm}{% 
    %  s: star = \xindex*: generates boldface entries in text and for page number in index
    %  o: optional argument  
    %  mmmmm: five main arguments 
    %  #2 = 'optional' : optional information, formatted as 'index' ('optional') in index,
    %  #3 = 'text'  : 'text' is printed in the main text
    %  #4 = 'sort'  : 'sort' is used for sorting the index, and generating hyperlinks
    %  #5 = 'index' : 'index' is printed in the index
    %  #6 = 'parent!' : 'index' is printed as subindex of 'parent'
    %  #7 = '|see{crossref}' : produces 'index' see 'crossref', or 
    %       '|seealso{crossref}' : produces 'index' see also 'crossref'
  \IfBooleanTF{#1}%
  {%   with *, us boldface for 'text' in main text and page number in index:
    \protect\hyperlink{ind:#4}{\textbf{#3}}% write hyperlinked #3 'text' into main text
    \IfValueTF{#2}{%  if optional info is present:  
      \index{#6#4@\protect\hypertarget{ind:#4}{#5 \textit{(#2)}}|textbf}% 
                                           %write #5 #2 'index' ('optional') to index
    }{%  if no optional info is present: 
      \index{#6#4@\protect\hypertarget{ind:#4}{#5}|textbf}% write #5 'index' to index
    }%
  }%
  {%  without *, use nonboldface for 'text' in main text and page number in index:
    \protect\hyperlink{ind:#4}{#3}% write hyperlinked #3 'text' into main text
    \IfValueTF{#2}{%  if optional info is present:  
      \index{#6#4@\protect\hypertarget{ind:#4}{#5 \textit{(#2)}}#7}% 
                                     % write #5 #2 'index' ('optional') to index
    }{%  if no optional info is present: 
      \index{#6#4@\protect\hypertarget{ind:#4}{#5}#7}% write #5 'index' to index
    }%
  }%
} 


%Format of arguments of \xindex:
%[optional]{text}{sort}{index}{parent!}{|see{crossref} or |seealso{crossref}}
% use * to generate boldface, e.g. \xSet*\
% omit * to generate nonboldface, e.g. \xSet\

\NewDocumentCommand{\xSet}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
[Menge]{set}{set}{set}{set!}{}}

\NewDocumentCommand{\xSubset}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
[Teilmenge]{subset}{subset}{subset}{set!}{}}

\NewDocumentCommand{\xUnion}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
[Vereinigungsmenge]{union}{union}{union}{set!}{}}

\NewDocumentCommand{\xUnionSeeSet}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
{}{unionseeset}{union}{}{|see{set}}}

\NewDocumentCommand{\xIntersection}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
[Schnittmenge]{intersection}{intersection}{intersection}{set!}{}}

\NewDocumentCommand{\xIntersectionSeeSet}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
{}{intersectionseeset}{intersection}{}{|see{set}}}

\NewDocumentCommand{\xCardinality}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
{cardinality}{cardinality}{cardinality}{}{}}

\NewDocumentCommand{\xCountable}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
{countable}{countable}{countable}{}{}}

\NewDocumentCommand{\xEquivalenceClasses}{s}{\IfBooleanTF{#1}{\xindex*}{\xindex}%
[Equivalenzklassen]{equivalence classes}{equivalence classes}{equivalence classes (of sets)}{}{}}

\usepackage[hyperindex]{hyperref}
\hypersetup{linktocpage=true,colorlinks,linkcolor=darkred,urlcolor=darkred,citecolor=darkred}

\begin{document}
A \xSet*\ is a collection of objects. A \xSubset*\ of a set contains
some of its elements. The \xUnion*\xUnionSeeSet\ of two sets contains
all elements of both. Their \xIntersection*\xIntersectionSeeSet\ contains
only the elements contained in both. A set is \xCountable*\ if you can 
count its elements. The number of
elements of a set is called its \xCardinality*. Sometimes
it is useful to organize sets in terms of \xEquivalenceClasses*.

\bigskip 

Please turn the page!
\newpage

Let the \xSet\ $C$ be the \xUnion\ of sets $A$ and $B$,
written as $C = A \cup B$, and $D$ be the \xIntersection\
of $A$ and $B$, written as $D = A \cap B$.

\printindex

\end{document}

我的代码确实实现了既定目标,即同一索引条目的不同调用应列在索引的同一行中,但要实现这一点,一个严格的先决条件是,对于所有这些调用,\xindex 的所有参数必须相同。由于有 5 个参数(以及可选的第 6 个参数),我发现为每个索引条目定义一个 \NewDocumentCommand 很方便,例如 \NewDocumentCommand{\xSet}{....},然后在正文中使用 \xSet\ 或 \xSet*(用于粗体)。可能有点麻烦,但它确实会生成所需的输出,例如:

示例输出

相关内容