为表格和图形添加书签

为表格和图形添加书签

我正在尝试使用以下代码为文档中的所有表格和图片生成书签。但是,使用此代码,表格 1 书签指向图 1,而图 2 书签指向表格 2,我无法找到图片和表格编号混淆的原因。

此外,使用此代码,整个标题都包含在书签中。我希望书签中只有简短的标题,但不知道该如何获取简短的标题。如能提供任何帮助,我将不胜感激。

谢谢!

% needs two runs
 \documentclass{article}
 \usepackage{caption}
 \usepackage{hyperref}

 \DeclareCaptionTextFormat{Tablebookmark}{\belowpdfbookmark{Table \thetable: #1}{\thetable}#1}
  \captionsetup[table]{textformat=Tablebookmark}
 \DeclareCaptionTextFormat{Figurebookmark}{\belowpdfbookmark{Figure \thefigure: #1}{\thefigure}#1}
  \captionsetup[figure]{textformat=Figurebookmark}

\begin{document}
\null\newpage

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

\newpage

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

\newpage

\begin{table}[ht]
 \begin{tabular}{cc}
 Header & Header \\
 1&2\\
 \end{tabular}
 \caption{Another caption text 2}
\end{table}

\newpage

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

\newpage %added two blank pages to make it easier to see bookmark targets
blank 
\newpage
blank

\end{document}

答案1

书签应使用目标名称(由\captionmore 或 less 设置)。包hyperref用于\@currentHref存储当前目标名称。在您的情况下,您可以使用数字为图形和表格设置相同的目标名称,而无需进一步指定。还设置了一个新的锚点,而不是最佳位置,这里设置的\belowpdfbookmark锚点更好。\@currentHrefhyperref/caption

该包bookmark提供了更简单的界面来访问目的地:

\bookmark[
  rellevel=1,
  keeplevel,
  dest=\@currentHref,
]{Table \thetable: #1}%

下一个问题:\caption它经常设置两次参数,因为它要检查字幕是否适合一行。可以使用 关闭此功能singlelinecheck=false。但这会影响单行字幕的格式。

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

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

\begin{document}
\null\newpage

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

\newpage

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

\newpage

\begin{table}[ht]
 \begin{tabular}{cc}
 Header & Header \\
 1&2\\
 \end{tabular}
 \caption{Another caption text 2}
\end{table}

\newpage

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

\newpage %added two blank pages to make it easier to see bookmark targets
blank 
\newpage
blank

\end{document}

\end{figure}如果这些浮动环境始终包含一个书签,则\end{table}可以将其用作设置书签的另一个位置\caption。由于包nameref将标题文本存储在宏中\@currentlabelname,因此可以在书签中使用它:

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

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

\begin{document}
\null\newpage

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

\newpage

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

\newpage

\begin{table}[ht]
 \begin{tabular}{cc}
 Header & Header \\
 1&2\\
 \end{tabular}
 \caption{Another caption text 2}
\end{table}

\newpage

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

\newpage %added two blank pages to make it easier to see bookmark targets
blank 
\newpage
blank

\end{document}

相关内容