如何让 hyperref 链接到索引

如何让 hyperref 链接到索引

我正在尝试获取指向索引开始的实际页面的超链接。它位于参考书目后面几页,但当您单击目录中的“索引”时,您将被带到参考书目的第一页。选项 hyperindex 使索引条目可链接,这非常好,但标题“索引”由提供,\printindex我无法弄清楚如何修改它。

我获取 Biblio 链接的方式是

\chapter*{Bibliography}
\addcontentsline{toc}{chapter}{Bibliography}

示例文件:

\documentclass{book}
 \usepackage{index,multicol}
 \usepackage[nosort,nocompress]{cite}
 \usepackage[hyperindex]{hyperref}
\makeindex
\begin{document}
\tableofcontents
\chapter*{Preface}
 \addcontentsline{toc}{chapter}{Preface}
This is a very brief preface.
 \chapter{This is the first chapter}
 \section{Here is the first section}
 Here we have Section 1 \index{Section 1}
Here is a bibliographical reference \cite{Kn1,Kn2}.
 \section{Here is the second section}
 Here is Section 2 \index{section 2}
\begin{thebibliography}{Kn2}
 \addcontentsline{toc}{chapter}{Bibliography}
 \bibitem{Kn1}D.E. Knuth, AOCP 1
 \bibitem{Kn2}D.E. Knuth, AOCP 2
\end{thebibliography}
\printindex
\end{document}

请注意,在真正的书中,\addtocontentsline在后面添加\printindex会将指针放在错误的位置。

答案1

如果相关命令没有在内部执行此操作,则需要添加\phantomsectionas 锚点。对于您的情况,我建议使用 imakeidx 和 tocbibind,以简化代码:

\documentclass{book}
\usepackage{tocbibind}
\usepackage{imakeidx}
\usepackage[nosort,nocompress]{cite}
\usepackage[hyperindex]{hyperref}

\makeindex[intoc]

\begin{document}
\tableofcontents
\cleardoublepage\phantomsection
\chapter*{Preface}
 \addcontentsline{toc}{chapter}{Preface}
This is a very brief preface.
 \chapter{This is the first chapter}
 \section{Here is the first section}
 Here we have Section 1 \index{Section 1}
Here is a bibliographical reference \cite{Kn1,Kn2}.
 \section{Here is the second section}
 Here is Section 2 \index{section 2}
\begin{thebibliography}{Kn2}
 \bibitem{Kn1}D.E. Knuth, AOCP 1
 \bibitem{Kn2}D.E. Knuth, AOCP 2
\end{thebibliography}

\printindex
\end{document}

相关内容