章をクリックすると目次に移動します

章をクリックすると目次に移動します

この質問を参考にしてください:Hyperref はテキスト内の章とページを参照しますおよびその重複の可能性目次に戻るクリック可能なリンク

これは基本的に 2 番目の質問の繰り返しになりますが、我慢してください。

最初の質問の回答を使用しましたが、少し編集しました。これで、すべての章の名前が目次にリンクされるようになりました (名前のみで、その上の「章番号」の部分はリンクされません。解決方法がわかれば、大きな問題ではありません。教えてください :) )

問題は、これによって目次の行自体も変更され、目次にリンクされることです。そのため、目次の章をクリックすると、またTOC へのリンク。

MWE:

\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もう一度 toc に明示的に記述し、章のタイトルに コマンドを設定します。これにより、正しいハイパーアンカーが保持され、ブックマーク パネルに\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}

関連情報