
參考這個問題:Hyperref 參考文本中的章節和頁面及其可能的重複可點擊連結返回目錄
這基本上又是第二個問題,但請耐心等待。
我使用了第一個問題的答案,但經過了精心編輯。我現在將每個章節名稱連結回目錄(只是名稱,而不是上面的“第#章”部分。這不是一個大問題,如果您知道如何解決它,請讓我知道:))
問題是:這也會改變目錄行本身以連結到目錄。因此,如果我現在點擊目錄中的某一章,也目錄連結。
微量元素:
\documentclass[11pt]{report}
\usepackage{hyperref}
\usepackage{lipsum}
\usepackage{xparse}
\AtBeginDocument{
\let\oldchapter\chapter
\RenewDocumentCommand{\chapter}{s o m}{%
\clearpage
\IfBooleanTF{#1}
{\oldchapter*{\hyperref[toc]{#3}}}% \chapter*[..]{...}
{\IfValueTF{#2}
{\oldchapter[#2]{\hyperref[toc]{#3}}}% \chapter[..]{...}
{\oldchapter{\hyperref[toc]{#3}}}% \chapter{...}
\label{chapter-\thechapter}% \label this chapter
}%
}
}
\begin{document}
\chapter*{Preface}
\lipsum[1-1]
\tableofcontents\label{toc}
\chapter{Introduction}
\lipsum[2-2]
\end{document}
我所做的是採用支援未編號章節的解決方案,並\hyperref[toc]{}
為其添加一個。
答案1
最簡單的解決方案是使用\oldchapter[#3]{\hyperref[toc]{#3}}}%
,即明確地將#3
再次寫入目錄並\hyperref
在章節標題中設定命令。這將保留正確的超錨點並防止[toc]
書籤面板中出現醜陋的前綴!
\documentclass[11pt]{report}
\usepackage{hyperref}
\usepackage{lipsum}
\usepackage{xparse}
\AtBeginDocument{
\let\oldchapter\chapter
\RenewDocumentCommand{\chapter}{s o m}{%
\clearpage
\IfBooleanTF{#1}
{\oldchapter*{\hyperref[toc]{#3}}}% \chapter*[..]{...}
{\IfValueTF{#2}
{\oldchapter[#2]{\hyperref[toc]{#3}}}% \chapter[..]{...}
{\oldchapter[#3]{\hyperref[toc]{#3}}}% \chapter{...}
\label{chapter-\thechapter}% \label this chapter
}%
}
}
\begin{document}
\chapter*{Preface}
\lipsum[1-1]
\tableofcontents\label{toc}
\chapter{Introduction}
\lipsum[2-2]
\chapter{Results}
\lipsum[2-4]
\end{document}