2

I need a custom order bookmark for a document report: currently, I'm using a macro from Bookmarks under List of Figures

I need a custom bookmark like this:

  • List of contents
    • chapter 1 .......
    • chapter 2 ......
    • chapter 3 .....
  • List of figures
    • Figure 1.1.......
    • Figure 2.4..........
  • List of tables
    • Table 2.5 ........
    • Table 2.7 ......
4

The linked post (which has an answer by me too) is not a solution to the problem, but it can be used as a starter.

The main problem is that a \bookmark command will enter the relevant information at the current level of the bookmark tree, this means the figure bookmarks will appear right between a chapter or section bookmark, but are not ordered or form a tree of themselves.

This can be cured by postponing the bookmark operation to some other place and faking some list of figure bookmark etc, which is doing the right bookmark entries at the end if \listoffiguresbookmarks is called at the end. (The same is true for \listoftablesbookmarks.)

\documentclass{article}
\usepackage{caption}
\usepackage{etoolbox}
\usepackage[notlof,notlot]{tocbibind}

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

\usepackage{bookmark}

\makeatletter
\newcommand{\listoffiguresbookmarks}{%
  \pdfbookmark[0]{\listfigurename}{listoffiguresbookmark}
  \bookmarksetup{level=1}
  \@starttoc{lofb}
}
\newcommand{\listoftablesbookmarks}{%
  \pdfbookmark[0]{\listtablename}{listoftablesbookmark}
  \bookmarksetup{level=1}
  \@starttoc{lotb}
}

\makeatother

%%% Generate bookmarks for all figures and tables
\makeatletter

\pretocmd\endfigure{%
\addtocontents{lofb}{%
  \protect{%
    \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
    ]{Figure \thefigure: \@currentlabelname}}}%
}{}{\errmessage{Patching \noexpand\endfigure failed}}
\pretocmd\endtable{%
\addtocontents{lotb}{%
  \protect{%
    \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
    ]{Table \thetable: \@currentlabelname}}}%
}{}{\errmessage{Patching \noexpand\endtable failed}}

\makeatother



\AtEndDocument{%
  \listoffiguresbookmarks
  \listoftablesbookmarks
}

\begin{document}
\tableofcontents
\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}

\clearpage

\begin{table}[ht]
\centering
\caption{Table caption No. 1}
\end{table}


\end{document}

enter image description here

| improve this answer | |
  • Please note that an improved version should check whether there's a caption at all – user31729 Feb 19 '17 at 11:53
  • Instead of hacking the environment, you should redefine the command \contentsline while reading the lof file. – Marco Daniel Feb 19 '17 at 12:37
  • @MarcoDaniel: I am pretty sure, if I would have used a redefinition of \contentsline, the next complainer would have been there (And we have a lot of complainers here ;-) There are a lot of possible solutions. – user31729 Feb 19 '17 at 15:49
  • Indeed :-) Next time I will redefine \caption :-) – Marco Daniel Feb 19 '17 at 16:07
  • @MarcoDaniel: Not redefine, but patching. Actually, that's what I had in mind first, but I took the answer to the linked post and changed it slightly. – user31729 Feb 19 '17 at 16:12
2

Here another approach. You can modify it for your needs. The examples uses two captions inside a single figure environment.

\documentclass{article}
\usepackage{xcolor}
\usepackage{caption,subcaption}
\usepackage[nottoc]{tocbibind}
\usepackage[]{hyperref}
\hypersetup{colorlinks,linkcolor=blue,linktoc=page}
\usepackage{bookmark}
\bookmarksetup{numbered, open, color=blue}
\makeatletter
\renewcommand{\listoffigures}{%
 \begingroup
    \let\SAVEDcontentsline\contentsline
    \def\contentsline##1##2##3##4{%
       \SAVEDcontentsline{##1}{##2}{##3}{##4}%
       \begingroup
         \def\numberline####1####2{####1:~####2}
         \bookmark[rellevel=1,keeplevel,dest=##4,]{\figurename~##2}%
       \endgroup  
     }%  
    \tocfile{\listfigurename}{lof}%
  \endgroup  
}
\makeatother


\begin{document}
\tableofcontents
\listoffigures
\clearpage
\section{The document}
\subsection{the fist subsection}
\begin{figure}[ht]
\centering
\begin{minipage}{0.49\linewidth}
\rule{6cm}{3cm}
\caption{Figure caption text 1}
\end{minipage}\hfill%
\begin{minipage}{0.49\linewidth}
\rule{6cm}{3cm}
\caption[Short form figrue 2]{long long long long long long long long long Figure caption text 2}
\end{minipage}
\end{figure}

\clearpage

\begin{figure}[ht]
\centering
      \subcaptionbox{A cat\label{cat}}
        {\rule{3cm}{3cm}}
      \subcaptionbox{An elephant\label{elephant}}
        {\rule{3cm}{3cm}}
      \caption{Two animals}\label{animals}
\end{figure}

\section{The second section}
\subsection{the second subsection}


\end{document}

enter image description here

| improve this answer | |

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.