如何引用索引?

如何引用索引?

我如何引用包创建的索引makeidx

最小工作示例:

\documentclass{scrbook}

\usepackage{hyperref} % creates links on refs
\usepackage{makeidx}
\makeindex

\begin{document}
    % add something to the index...
    The foobar\index{foobar} is quite impressive!

    % some reference to the index...
    Please follow the \ref{index}.

    % print index...
    \printindex 
\end{document}

显然,\label{index}在 之前和 之后都不起作用\printindex

编辑(感谢 daleif):我忘了说一个重要事实:我希望索引链接到\ref{index}。这就是为什么我不能只说See index.

答案1

\setindexpreamble类中有一个钩子,scr用于在索引开头添加材料。您可以使用它来设置标签。使用 just\ref会给出一个空白链接,您需要一个\pageref或一个\nameref

示例输出

\documentclass{scrbook}

\usepackage{makeidx}
\usepackage{hyperref} % creates links on refs
\makeindex

\setindexpreamble{\label{index}}

\begin{document}
\label{start}
    % add something to the index...
    The foobar\index{foobar} is quite impressive!

    % some reference to the index...
    Please see the \nameref{index}.

    % print index...
    \printindex 
\end{document}

在标准类中,例如articlebook您可以使用etoolbox包来修补theindex环境以提供合适的锚点,如下所示:

\patchcmd{\theindex}{\thispagestyle{plain}}
  {\thispagestyle{plain}\phantomsection\label{index}}{}{}

\pageref您可以通过或 通过 来参考\hyperref[index]{text description},特别是\hyperref[index]{\indexname}

\documentclass{book}

\usepackage{makeidx}
\usepackage{etoolbox}
\usepackage{hyperref}
\makeindex

\patchcmd{\theindex}{\thispagestyle{plain}}
  {\thispagestyle{plain}\phantomsection\label{index}}{}{}

\begin{document}
\label{start}
    % add something to the index...
    The foobar\index{foobar} is quite impressive!

    % some reference to the index...
    Please see the \hyperref[index]{\indexname}.

    % print index...
    \printindex
\end{document}

答案2

你可以这样做imakeidx

\documentclass{scrbook}

\usepackage{imakeidx}
\usepackage{hyperref} % creates links on refs

\makeindex[title={Index\label{index}}]

\begin{document}
% add something to the index...
The foobar\index{foobar} is quite impressive!

% some reference to the index...
Please follow the \hyperref[index]{index}.

% print index...
\printindex

\end{document}

答案3

更“基本”的版本,使用由和fake植入的标签。\@currentlabel\label

\documentclass{scrbook}

\usepackage{makeidx}

\usepackage{hyperref} % creates links on refs

\makeatletter
\AtBeginDocument{%
  \newcommand{\@@fakelabelname}{}%
  \newcommand{\definefakelabel}[1]{%
    \gdef\@@fakelabelname{#1}% Make the label name globally available
  }
  \newcommand{\implantfakelabel}{%
    \expandafter\def\csname p@\@@fakelabelname\endcsname{}% Define the dummy 'label' content%
    \protected@edef\@currentlabel{\csname p@\@@fakelabelname\endcsname\indexname}% Fake `\refstepcounter`
    \phantomsection
    \label{\@@fakelabelname}% 
  }

  \let\latex@@theindex\theindex  % store the original index version
  \let\latex@@theindexend\endtheindex % store the original \endtheindex
  \renewenvironment{theindex}[1][index]{%
    \latex@@theindex% Call the old index start up
    \implantfakelabel% place the fake label here
  }{%
  \latex@@theindexend% End code
}
}
\makeatother

\makeindex



\begin{document}
    % add something to the index...

\chapter{Foo}
    The foobar\index{foobar} is quite impressive!

    % some reference to the index...
    Please follow the \ref{indexfoo}. % Refer to the label

\chapter{Foobar}

    % print index...
\definefakelabel{indexfoo}% how is the label named?
\printindex 
\end{document}

在此处输入图片描述

相关内容