重置计数器后 hyperref 生成错误指向的 PDF 书签
LaTeX 202 1 min
本文发布于 1134 天前,其中的信息可能已经物是人非

描述

为了在不同 part 下使用不同章节标题格式,重置 chapter 计数器

\documentclass{book}

\begin{document}

\part{1}

\chapter{1}

\part{2}

\setcounter{chapter}{0}
\renewcommand{\thechapter}{\Roman{chapter}}

\chapter{2}

\end{document}

但第二个 chapter 的书签会指向第一个 chapter。

类似可见 Hyperref did not work properly when I reset the section-counter during the document - TeX - LaTeX Stack Exchange

原因

这是因为 \setcounter{chapter}{0} 后,hyperref 生成书签时会认为第二个 chapter 的位置指向其计数器(此时为 1)的位置。

方法

  1. hyperref 宏包中开启选项 hypertexnames=false

    \usepackage{hyperref}
    \hypersetup{hypertexnames=false}
  2. 在导言区使用 \@addtoreset 命令重置计数器,而不是手动重置每个部件的计数器

    \makeatletter
    \@addtoreset{chapter}{part}
    \makeatother

    参考 Hyperref did not work properly when I reset the section-counter during the document - TeX - LaTeX Stack Exchange

  3. 其他方法可见 hyperref vs. setcounter - LaTeX Forumhyperref does not work when section counter is reset - TeX - LaTeX Stack Exchange

重置计数器后 hyperref 生成错误指向的 PDF 书签
https://blog.liks.space/archives/hyperref-wrong-pdf-bookmark/
作者
Liks
发布于
许可