Add PDF bookmarks to list of figures, list of tables, and figure and table captions

bookmarkscaptionshyperrefpdftex

I want to bookmark my LaTeX-created .pdf file in two ways: in the \listoffigures and \listoftables and at the figures and tables themselves.

I started trying to apply Bookmarks under List of Figures, but I couldn't quite get to the right solution. Part of the issue is that the solution doesn't seem to work the same way for regular tables vs longtables vs other types of tables. I am hoping for a solution that works for all figure and table types.

So, I tried to use the \DeclareCaptionFormat macro from the caption package to make the bookmarks there. But, I can't seem to make it work to get the correct part of the caption. In the ideal solution, it would grab the short caption, if it exists, and the normal caption if the short caption does not exist.

Here is my current attempt:

\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}

Best Answer

I would make use of the fact that \contentsline has all the needed info.

\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}

enter image description here

To add the figure and table also below the sectioning commands you can temporarly change the bookmarkstype. A small patch is needed here. The example also changes the toc-level command so that the floats fall under their sectioning command (use an absolute value if you prefer that). With bookmarksnumbered you can get a numbering .

\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}

enter image description here

Related Question