如何設定書籤錨點,使 \section- 和 \section*-bookmark 的行為相同

如何設定書籤錨點,使 \section- 和 \section*-bookmark 的行為相同

如果我製作一個\section書籤\section*,錨點會指向不同位置的兩個部分。新增了一個小的垂直空間\section*(請參閱.png-files)。如何確保錨點放置在同一位置?

我需要這個解決方案,因為我想做一個附錄清單,其中條目只需要顯示在LoA和 中ToC,但我仍然需要文本中的節號 和LoA

\documentclass{book}

\usepackage{bookmark}
\hypersetup{bookmarksnumbered=true}

\begin{document}
\chapter{Chapter one}

References here:

ref: \ref{chap2} (chapter)

ref: \ref{chap2sec1} (section)

ref: \ref{chap2sec2} (star-section)

\chapter{Chapter two}\label{chap2}

...(contents of chapter 2)...

\section{Sec one}\label{chap2sec1}

...(contents of chapter 2 - section 1)...

\refstepcounter{section}
\section*{\thesection{}{\quad}Sec two}\label{chap2sec2}
\addcontentsline{toc}{section}{\protect\numberline{\thesection}Sec two}

...(contents of chapter 2 - section 2)...

\end{document}

普通節書籤 (\section) 星形部分書籤 (\section*)

編輯(1):我添加了一個紅色框來顯示 的錯誤書籤錨點\section*。部分之間的間距是正確的,只是錨點設定錯誤。

答案1

您可以調整工作方式\section\section*以相同的方式執行操作\section,只需進行一些細微的修改:

\documentclass{book}

\usepackage{bookmark}
\hypersetup{bookmarksnumbered=true}

\usepackage{xparse}
\makeatletter
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname\space}% Just for this example
\let\oldsection\section
\let\oldaddcontentsline\addcontentsline
\RenewDocumentCommand{\section}{s o m}{%
  \IfBooleanTF{#1}
    {{% \section*
      \renewcommand{\refstepcounter}[1]{\phantomsection}% Gobble counter stepping
      \renewcommand{\@seccntformat}[1]{}% Gobble section number formatting
      \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{toc}{section}{#3}}% Next entry _will_ be a section
      \oldsection{#3}% \section*[.]{..}
    }}{% \section
      \IfNoValueTF{#2}
        {\oldsection{#3}}% \section{..}
        {\oldsection[#2]{#3}}% \section[.]{..}
    }%
}
\makeatother

\begin{document}
\chapter{Chapter one}

References here:

ref: \ref{chap2} (chapter)

ref: \ref{chap2sec1} (section)

ref: \ref{chap2sec2} (star-section)

\chapter{Chapter two}\label{chap2}

...(contents of chapter 2)...

\section{Sec one}\label{chap2sec1}

...(contents of chapter 2 - section 1)...

\stepcounter{section}%
\section*{\thesection{} Sec two}\label{chap2sec2}

...(contents of chapter 2 - section 2)...

\end{document}

xparse用於輕鬆捕獲通常由\section(星號版本、可選和強制參數)管理的參數。

\section*被呼叫時,我們更新三個典型的事情\section*

  1. 反步進\refstepcounter被設為空操作;

  2. 節計數器格式化巨集\@seccntformat被轉換為無操作;

  3. 與內容相關的書寫被更新為特定於輸入,從而避免可能的\numberline列印。

由於\section現在\section*使用相同的過程來列印標題(即\@sect內部),因此輸出超連結會跳到相同的垂直位置。

\refstepcounter您的範例有點做作,因為它包含作為第 2.2 節一部分的手冊。但是,我沒有看到使用\section上面定義的更新版本的超連結跳轉有任何問題。

相關內容