3

I asked a question about how to generate bookmarks for tables and figures a few month ago and received an excellent answer (solved question).

The proposed code works like a charm for normal tables. However, I'm struggling to find a good solution for longtables. In the code below, I get one bookmark for each page on which the longtable is presented, and all bookmarks point to the same page. An ideal solution would be to only have one bookmark for each long table and the bookmark should of course point to the long table's first caption.

Moreover, the name of the bookmark for the longtable is the name of the section. I assume that longtable does not change currentlabelname, but I'm not sure if it is possible to manually change currentlabelname in the longtable environment.

I have been trying to solve this for a sick amount of time, but I have not manage to find a good solution. Any advice are much appreciated.

\documentclass{article}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{longtable}

\captionsetup{singlelinecheck=off}
\makeatletter
\DeclareCaptionTextFormat{Tablebookmark}{%
  \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
  ]{Table \thetable: \@currentlabelname}%
  #1%
}
\captionsetup[table]{textformat=Tablebookmark}
\DeclareCaptionTextFormat{Figurebookmark}{%
  \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
  ]{Figure \thefigure: \@currentlabelname}%
  #1%
}
\captionsetup[figure]{textformat=Figurebookmark}
\makeatother


\begin{document}
\null\newpage

\section{Normal table} 
\begin{table}[ht]
 \caption[Short text]{This is a table with a very long caption
 and it is nice to use the short caption in the bookmark}
 \begin{tabular}{cc}
 Header & Header \\
 1&2\\
 \end{tabular}
\end{table}

\newpage

\section{This becomes the title bookmark}    

\begin{longtable}{ll}
\caption[My long table]{A long table} 
\tabularnewline
\endfirsthead\caption[My long table continued]{\emph{(continued)}}
\tabularnewline
\endhead
\hline
\endfoot
longtable header & longtable header\\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
\end{longtable}

\end{document}
  • 1
    normally you want \caption[] on the head other than the first, to suppress writing to the toc. – David Carlisle Oct 6 '14 at 20:26
1

Heiko will probably post something using the proper interfaces but you can force the value of \currentlabelname and test that value and only conditionally make a bookmark:

enter image description here

\documentclass{article}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{longtable}

\captionsetup{singlelinecheck=off}
\makeatletter
\DeclareCaptionTextFormat{Tablebookmark}{%
\ifx\@currentlabelname\@empty
\else
  \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
  ]{Table \thetable: \@currentlabelname}%
\fi
  #1%
}
\captionsetup[table]{textformat=Tablebookmark}
\DeclareCaptionTextFormat{Figurebookmark}{%
  \bookmark[
    rellevel=1,
    keeplevel,
    dest=\@currentHref,
  ]{Figure \thefigure: \@currentlabelname}%
  #1%
}
\captionsetup[figure]{textformat=Figurebookmark}
\makeatother


\begin{document}
\null\newpage

\section{Normal table} 
\begin{table}[ht]
 \caption[Short text]{This is a table with a very long caption
 and it is nice to use the short caption in the bookmark}
 \begin{tabular}{cc}
 Header & Header \\
 1&2\\
 \end{tabular}
\end{table}

\newpage

\section{This becomes the title bookmark}    

\begin{longtable}{ll}
\noalign{\makeatletter\gdef\@currentlabelname{my long table}}
\caption[My long table]{A long table} 
\tabularnewline
\noalign{\makeatletter\gdef\@currentlabelname{}}
\endfirsthead\caption[]{\emph{(continued)}}
\tabularnewline
\endhead
\hline
\endfoot
longtable header & longtable header\\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
\end{longtable}

\end{document}

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.