正确的自定义 PDF 书签与更改的页码

正确的自定义 PDF 书签与更改的页码

我有以下代码,其中我使用自定义标题来:

  1. 创建目录条目
  2. 创建自定义书签

我希望第一个 PDF 书签指向TOC页面。

由于加载或包\addcontentsline{toc}{chapter}{#1}时会自动创建 PDF 书签,因此我过去常常隐藏使用它创建的 PDF 书签。多亏了它,我可以使用包创建我需要的所有书签。hyperrefbookmark\hypersetup{bookmarksdepth=-1}bookmark

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\hypersetup{bookmarksdepth=-1}
\usepackage{bookmark}
\def \myheading#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\bookmark[page=\thepage,level=-1]{My own bookmark for #1}
}
\begin{document}
\bookmark[page=\thepage,level=-1]{My own TOC bookmark}
\tableofcontents
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

上面的代码生成的文档中的目录和 PDF 书签都指向正确的位置。

然而,当我尝试更改页码时,事情开始变糟了。

当我使用时:

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\hypersetup{bookmarksdepth=-1}
\usepackage{bookmark}
\def \myheading#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\bookmark[page=\thepage,level=-1]{My own bookmark for #1}
}
\begin{document}
\frontmatter
% \bookmark[page=\thepage,level=-1]{My own TOC bookmark} % This is commented out, because adding it will cause compilation error.
\tableofcontents
\mainmatter
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

TOC 工作正常。但是bookmarks软件包生成的 PDF 书签对各个标题的页码感到困惑,结果指向了错误的位置:“二”书签指向第 3 页而不是第 5 页,“三”书签指向第 7 页而不是第 9 页,等等。

并且使用目录书签会导致编译错误(所以我不得不将其注释掉)。

我可以通过使用/\setcounter{page}{1}来处理这个特定的编译错误问题:\frontmatter\mainmatter

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\hypersetup{bookmarksdepth=-1}
\usepackage{bookmark}
\def \myheading#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\bookmark[page=\thepage,level=-1]{My own bookmark for #1}
}
\begin{document}
\bookmark[page=\thepage,level=-1]{My own TOC bookmark}
\tableofcontents
\myheading{One}
\setcounter{page}{1}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

但它会使目录书签具有与“一”书签相同的页码(1),并且所有其他 PDF 书签都指向错误的页面。

我如何创建正确的自定义书签指向正确的页面(如第一个代码示例所示),同时将“一”之前的所有数字改为罗马数字(目录位于第“i”页)和“One” 位于第 1 页

(当然,目录也应该指向正确的页面,但这似乎不是问题。)

答案1

  • \pdfbookmark可用于目录。

  • \texorpdfstring可用于将不同的文本放入目录和书签中。则pdfbookmarksdepth=-1不需要。

例子:

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{bookmark}
\def \myheading#1{%
  \chapter*{#1}
  \addcontentsline{toc}{chapter}{\texorpdfstring{#1}{My own bookmark for #1}}%
}
\begin{document}
\frontmatter
\pdfbookmark[0]{My own TOC bookmark}{toc}
\tableofcontents
\mainmatter
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

结果

答案2

dest=这是一个使用代替page=标签用法的提议

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage[depth=-1]{bookmark}

\newcounter{fakecntr}

\newcommand{\myheading}[1]{%
\refstepcounter{fakecntr}
\phantomsection
\chapter*{\hypertarget{chap::\number\value{fakecntr}}{#1}}
\addcontentsline{toc}{chapter}{#1}
\bookmark[dest=chap::\number\value{fakecntr},level=-1]{My own bookmark for #1}
}
\begin{document}
\frontmatter
\bookmark[dest=toc::bookmark,level=-1]{My own TOC bookmark}
\hypertarget{toc::bookmark}{\tableofcontents}
\mainmatter
\cleardoublepage
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

相关内容