为图表列表、表格列表以及图表和表格标题添加 PDF 书签

为图表列表、表格列表以及图表和表格标题添加 PDF 书签

我想通过两种方式为我用 LaTeX 创建的 .pdf 文件添加书签:在和中\listoffigures以及\listoftables在图表和表格本身中。

我开始尝试申请图片列表下的书签,但我无法找到正确的解决方案。部分问题在于,对于常规表格、长表格和其他类型的表格,该解决方案似乎不起作用。我希望找到一种适用于所有图形和表格类型的解决方案。

因此,我尝试使用\DeclareCaptionFormatcaption 包中的宏来制作书签。但是,我似乎无法让它获取正确的标题部分。在理想的解决方案中,如果存在短标题,它将抓取短标题,如果不存在短标题,它将抓取正常标题。

这是我目前的尝试:

\documentclass{article}

\title{Sections and Chapters}
\author{Bill Denney}
\date{today}

\usepackage{float}
\usepackage{longtable}
\usepackage{etoolbox}
\usepackage{bookmark}

%%% Generate bookmarks for the table of contents, list of figures, and list of tables
\makeatletter
\pretocmd\tableofcontents{%
\pdfbookmark[0]{\contentsname}{toc}%
}
\makeatother

\makeatletter
\pretocmd\listoffigures{%
\pdfbookmark[0]{\listfigurename}{lof}%
}
\makeatother

\makeatletter
\pretocmd\listoftables{%
\pdfbookmark[0]{\listtablename}{lot}%
}
\makeatother

%%% Generate bookmarks for all figures
\usepackage{caption}
\newcommand{\bookmarkcaption}[2]{
  \addtocontents{#1}{
    \bookmark[
      rellevel=1,
      keeplevel,
      dest=\@currentHref,
    ]{#2}
  }
  \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
  ]{#2}
  #2\par
}

\DeclareCaptionFormat{bookmarkfig}{\bookmarkcaption{lof}{#1#2#3}}
\DeclareCaptionFormat{bookmarktab}{\bookmarkcaption{lot}{#1#2#3}}
\captionsetup[figure]{format=bookmarkfig}
\captionsetup[table]{format=bookmarktab}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\bookmarksetup{startatroot}

\begin{figure}
  \caption{F1}
\end{figure}

\begin{figure}
  \caption{F2}
\end{figure}

\begin{table}
  \begin{tabular}{c}
    1
  \end{tabular}
  \caption{T1}
\end{table}

\begin{longtable}{c}
  \caption{T2}
  2 \\
  2
\end{longtable}

\begin{table}
  \begin{tabular}{c}
    3
  \end{tabular}
  \caption{T3}
\end{table}

\end{document}

答案1

我会利用 \contentsline 拥有所有需要的信息这一事实。

\documentclass{article}

\title{Sections and Chapters}
\author{Bill Denney}
\date{today}

\usepackage{float}
\usepackage{longtable}
\usepackage{etoolbox}

\usepackage{bookmark}

%%% Generate bookmarks for the table of contents, list of figures, and list of tables
\makeatletter
\pretocmd\tableofcontents{%
\pdfbookmark[0]{\contentsname}{toc}%
}
\makeatother

\makeatletter
\pretocmd\listoffigures{%
\pdfbookmark[0]{\listfigurename}{lof}%
}
\makeatother

\makeatletter
\pretocmd\listoftables{%
\pdfbookmark[0]{\listtablename}{lot}%
}
\makeatother

\usepackage{caption}

\begin{document}

\tableofcontents

\NewCommandCopy\oricontentsline\contentsline
\makeatletter
\RenewDocumentCommand\contentsline{mmmm}
{%
  \oricontentsline{#1}{#2}{#3}{#4}%
  {\let\numberline\@gobble
    \bookmark[
      rellevel=1,
      keeplevel,
      dest=#4,
    ]{#2}}%
}
\listoffigures
\listoftables

\bookmarksetup{startatroot}

\begin{figure}
  \caption{F1}
\end{figure}

\begin{figure}
  \caption{F2}
\end{figure}

\begin{table}
  \begin{tabular}{c}
    1
  \end{tabular}
  \caption{T1}
\end{table}

\begin{longtable}{c}
  \caption{T2}
  2 \\
  2
\end{longtable}

\begin{table}
  \begin{tabular}{c}
    3
  \end{tabular}
  \caption{T3}
\end{table}

\end{document}

在此处输入图片描述

要在分段命令下添加图形和表格,您可以暂时更改书签类型。这里需要一个小补丁。该示例还更改了 toc-level 命令,以便浮动位于其分段命令下(如果您愿意,可以使用绝对值)。bookmarksnumbered您可以使用 获得编号。

\documentclass{article}

\usepackage{bookmark}
\usepackage{etoolbox}

\makeatletter
% \patchcmd\Hy@writebookmark appears to no longer be necessary as of
% 2023-12-18 possibly due to recent updates in either the hyperref or
% bookmark package
%\patchcmd\Hy@writebookmark{\def\BKM@type}{\edef\BKM@type}{}{\fail}

\def\toclevel@figure{\inteval{\BKM@currentlevel+1}}
\def\toclevel@table{\inteval{\BKM@currentlevel+1}}

\AddToHook{env/figure/begin}{%
  \hypersetup{bookmarkstype=lof,bookmarksnumbered}%
  \def\Hy@numberline#1{\figurename{} #1 }%
  \bookmarksetup{keeplevel}}
  
\AddToHook{env/table/begin}{%
  \hypersetup{bookmarkstype=lot,bookmarksnumbered}%
  \def\Hy@numberline#1{\tablename{} #1 }%
  \bookmarksetup{keeplevel}%
  }
  
\makeatother 
\begin{document}

\section{abc}

\begin{figure}
\caption{Figure}
\end{figure}

\begin{table}
\caption{table}
\end{table}

\subsection{blub}

\begin{figure}
\caption{Figure}
\end{figure}

\begin{table}
\caption{table}
\end{table}

\end{document}

在此处输入图片描述

相关内容