每个长表一个书签

每个长表一个书签

几个月前,我问了一个关于如何为表格和图形生成书签的问题,并得到了一个很好的答案(已解决的问题)。

建议的代码对于普通表格来说非常有效。但是,我很难找到一个适合长表格的好的解决方案。在下面的代码中,我为显示长表格的每个页面获取一个书签,并且所有书签都指向同一页面。理想的解决方案是每个长表格只有一个书签,并且书签当然应该指向长表格的第一个标题。

此外,longtable 的书签名称是部分的名称。我假设 longtable 不会更改 currentlabelname,但我不确定是否可以在 longtable 环境中手动更改 currentlabelname。

我已经尝试解决这个问题很长时间了,但一直没能找到一个好的解决方案。任何建议都非常感谢。

\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

Heiko 可能会使用适当的界面发布一些内容,但您可以强制该值\currentlabelname并测试该值,并且仅有条件地制作书签:

在此处输入图片描述

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

相关内容