\contentsline
我使用的是在 中定义的第四个参数版本hyperref
。有没有办法根据其标签自动计算包\contentsline
中最后一个参数的正确超链接?hyperref
现在我正在使用这样的东西:
\addtocontents{myList}{\string\contentsline{subsection}{Title of the entry}{\pageref*{labelForTheEntry}}{othertocsubsection.4.0.14}}
其中“othertocsubsection.4.0.14”是我手动设置的,有点脆弱。
我尝试过类似的事情:
\addtocontents{myList}{\string\contentsline{subsection}{Title of the entry}{\pageref*{labelForTheEntry}}{\ref{labelForTheEntry}}}
但它不起作用。
我试图在此引用一个将来的页面,稍后会将其包含进来。上面的第一个选项有效,但问题是必须手动设置目标链接。
下面我给出了一个简单的例子:
\documentclass[a4paper,twoside]{book}
\usepackage[final]{pdfpages}
\usepackage[bookmarks=true, bookmarksnumbered=true, bookmarksopen=true]{hyperref}
\usepackage{tocloft}
\begin{document}
\pagenumbering{Roman}
\newcommand{\indexNumberOne}{}
\newlistof{myIndexOne}{myIndOne}{\indexNumberOne}
\setcounter{myIndOnedepth}{4}
\newlistentry{myIndexOnechapter}{myIndOne}{0}
\newlistentry[myIndexOnechapter]{myIndexOnesection}{myIndOne}{1}
\newlistentry[myIndexOnesection]{myIndexOnesubsection}{myIndOne}{2}
\newlistentry[myIndexOnesubsection]{myIndexOnesubsubsection}{myIndOne}{3}
\pagenumbering{arabic}
\setcounter{page}{1}
\pdfbookmark[1]{Main TOC (documents appear in sequential order)}{MainTOC}
\tableofcontents
\cleardoublepage
\pdfbookmark[1]{Additional index (documents DO NOT appear in sequential order)}{AdditionalIndex}
\listofmyIndexOne
\cleardoublepage
\addtocontents{myIndOne}{\centerline{\underline{Additional Index}}}
\addtocontents{myIndOne}{\string\contentsline{subsection}{Document 2}{\pageref*{docTwo}}{section*.2}}
\cleardoublepage
\addtocontents{myIndOne}{\string\contentsline{subsection}{Document 1}{\pageref*{docOne}}{section*.1}}
\cleardoublepage
\phantomsection
\label{docOne}
\addcontentsline{toc}{section}{Document 1}
\includepdf[pages=-,fitpaper=true]{doc1.pdf}
\cleardoublepage
\phantomsection
\label{docTwo}
\addcontentsline{toc}{section}{Document 2}
\includepdf[pages=-,fitpaper=true]{doc2.pdf}
\cleardoublepage
\end{document}
我想要的是一个通用的解决方案,以这样一种方式替换“section*.2”和“section*.1”之类的内容,即自动从标签计算这些内容(如果移动文档的顺序,当前链接最终会指向错误的位置)。有什么想法吗?
答案1
可以使用 来提取标签的超锚点\getrefbykeydefault{labelname}{anchor}{some default value}
,其中anchor
是键,标签名称是docOne
等等,就像在本文档中一样。
我提供了一个名为的小包装宏,\adddoctotoc
它将内容写入myIndOne
,需要一个条目标题和标签名称,然后自动提取锚点。
请注意它也\includepdf
有一个功能!toc
提到的dummydocX.pdf
文件是LaTeX
使用这个虚拟代码创建的:
\documentclass{article}
\usepackage{blindtext}
\pagestyle{empty}
\begin{document}
\section{Beginning \jobname}
\blindtext[50]
\end{document}
这是工作代码,可能需要运行三次编译才能获得正确的引用和锚点!
\documentclass[a4paper,twoside]{book}
\usepackage[final]{pdfpages}
\usepackage[bookmarks=true, bookmarksnumbered=true, bookmarksopen=true]{hyperref}
\usepackage{tocloft}
\begin{document}
\pagenumbering{Roman}
\newcommand{\indexNumberOne}{}
\newlistof{myIndexOne}{myIndOne}{\indexNumberOne}
\setcounter{myIndOnedepth}{4}
\newlistentry{myIndexOnechapter}{myIndOne}{0}
\newlistentry[myIndexOnechapter]{myIndexOnesection}{myIndOne}{1}
\newlistentry[myIndexOnesection]{myIndexOnesubsection}{myIndOne}{2}
\newlistentry[myIndexOnesubsection]{myIndexOnesubsubsection}{myIndOne}{3}
\newcommand{\adddoctotoc}[3][myIndOne]{%
\addtocontents{#1}{\string\contentsline{subsection}{#2}{\getpagerefnumber{#3}}{\getrefbykeydefault{#3}{anchor}{page.1}}}%
}
\pagenumbering{arabic}
\pdfbookmark[1]{Main TOC (documents appear in sequential order)}{MainTOC}
\tableofcontents
\cleardoublepage
\pdfbookmark[1]{Additional index (documents DO NOT appear in sequential order)}{AdditionalIndex}
\listofmyIndexOne
\cleardoublepage
\addtocontents{myIndOne}{\centerline{\underline{Additional Index}}}
\adddoctotoc{Document 2}{docTwo}
\adddoctotoc{Document 1}{docOne}
%%\addtocontents{myIndOne}{\string\contentsline{subsection}{Document 1}{\pageref*{docOne}}{section*.1}}
\cleardoublepage
\phantomsection
\label{docOne}
\addcontentsline{toc}{section}{Document 1}
\includepdf[pages=-,fitpaper=true]{dummydoc1.pdf}
\cleardoublepage
\phantomsection
\label{docTwo}
\addcontentsline{toc}{section}{Document 2}
\includepdf[pages=-,fitpaper=true]{dummydoc2.pdf}
\cleardoublepage
\end{document}