1

I want to automatically generate bookmarks under List of Figures. The code below adds bookmark for each Figure (which is good), but I fail to also automatically generate the same bookmarks under List of Figures (without manually entering the code commented out in the code below). Any help or advice would be much appreciated!

\documentclass{article}
\usepackage{caption}
\usepackage[bookmarksnumbered]{hyperref}
\hypersetup{colorlinks,
        linkcolor=blue,
        linktoc=page}
\usepackage{bookmark}
\usepackage{etoolbox}
\usepackage{tocloft} 

%%% Generate bookmarks for all figures
\makeatletter
\pretocmd\endfigure{%
\bookmark[
rellevel=1,
keeplevel,
dest=\@currentHref,
 ]{Figure \thefigure: \@currentlabelname}%
}{}{\errmessage{Patching \noexpand\endfigure failed}}
\makeatother


% %%% Format list of tables and list of figures
\newlength{\mylen}
\renewcommand{\cfttabpresnum}{\tablename\space}
\renewcommand{\cfttabaftersnum}{}
\settowidth{\mylen}{\cfttabpresnum\cfttabaftersnum}
\addtolength{\cfttabnumwidth}{2cm}
\renewcommand{\cftfigpresnum}{\figurename\space}
\renewcommand{\cftfigaftersnum}{}
\settowidth{\mylen}{\cftfigpresnum\cftfigaftersnum}
\addtolength{\cftfignumwidth}{2cm}


\begin{document}
\phantomsection
\addcontentsline{toc}{section}{List of Figures}
\listoffigures
%\bookmark[
%    dest=Figure1,
%    rellevel=1,
%    keeplevel]{Figure 1: Figure caption text}
%\bookmark[
%    dest=Figure2,
%    rellevel=1,
%    keeplevel]{Figure 2: Figure caption text 2}
\newpage

\section{The document}

\begin{figure}[ht]
\centering
\rule{6cm}{3cm}
\caption{Figure caption text}
%\hypertarget{Figure1}{}
\end{figure}

\newpage

\begin{figure}[ht]
\centering
\rule{6cm}{3cm}
\caption{Figure caption text 2}
%\hypertarget{Figure2}{}
\end{figure}

\end{document}

Current: Bookmarks in current document

Goal: Ideal placement of bookmarks

  • The problem is, that you want to add bookmarks from all over the document to something (LOF) right at the start. – user31729 Nov 10 '14 at 14:38
2

See a related question here: Custom bookmark list of figures and tables

The easiest way is to write the same bookmark entry to the .lof file such that a bookmark is generated when \listoffigures is issued (by reading in the .lof file).

This can be achieved by using \addtocontents{lof}{\protect{\bookmark{}...} (see code below.

\documentclass{article}
\usepackage{caption}
\usepackage{etoolbox}
\usepackage{tocloft} 

\usepackage[bookmarksnumbered,bookmarksopen=true]{hyperref}
\hypersetup{colorlinks,
        linkcolor=blue,
        linktoc=page}
\usepackage{bookmark}

%%% Generate bookmarks for all figures
\makeatletter

\pretocmd\endfigure{%
\addtocontents{lof}{\protect{%
    \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
    ]{Figure \thefigure: \@currentlabelname}}}%
\bookmark[
rellevel=1,
keeplevel,
dest=\@currentHref,
]{Figure \thefigure: \@currentlabelname}%
}{}{\errmessage{Patching \noexpand\endfigure failed}}
\makeatother



% %%% Format list of tables and list of figures
\newlength{\mylen}
\renewcommand{\cfttabpresnum}{\tablename\space}
\renewcommand{\cfttabaftersnum}{}
\settowidth{\mylen}{\cfttabpresnum\cfttabaftersnum}
\addtolength{\cfttabnumwidth}{2cm}
\renewcommand{\cftfigpresnum}{\figurename\space}
\renewcommand{\cftfigaftersnum}{}
\settowidth{\mylen}{\cftfigpresnum\cftfigaftersnum}
\addtolength{\cftfignumwidth}{2cm}


\begin{document}
\phantomsection
\addcontentsline{toc}{section}{List of Figures}
\listoffigures
\newpage

\section{The document}

\begin{figure}[ht]
\centering
\rule{6cm}{3cm}
\caption{Figure caption text}
\end{figure}

\newpage

\section{Different section}

\begin{figure}[ht]
\centering
\rule{6cm}{3cm}
\caption{Figure caption text 2}
\end{figure}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.