如何将超链接放在 endfloat 的标记中?

如何将超链接放在 endfloat 的标记中?

的文件端浮点包解释了如何自定义标记中的文本,而不是默认的“[表 3 关于此处。]”。但我想自定义标记以包含指向文档末尾图形的超链接。如何实现?

这里有一个子问题:在自定义标记时,\thepostfigure指的是图号。是否有类似的命令\thefigurereference来引用图,以便标记文本可以是这样的Figure \ref{\thefigurereference}} about here.

更新 2:在评论中,我也询问了反向链接,以便文档末尾的图形标题可以超链接回文本中嵌入的“[图 1 关于此处]”浮动。@John Kormylo 的可接受解决方案实现了这一点。

更新 1 [经过编辑以更清楚地引用@John Kormylo 的原始答案,而不是他的修订答案]:

@John Kormylo 的(原始)解决方案有效,但据我所知,它与 subcaption 包不兼容。因此,我修改后的问题是:有没有一种方法可以超链接到仍与标题兼容的图形?

这是使用其解决方案的 MWE,其中超链接不会转到末尾的图形。图 2 只是为了说明为什么我需要 subcaption - 即使没有它,只要使用 subpcaption 包,链接仍然会失败。

\documentclass{article}
\usepackage{endfloat}
\usepackage{mwe}
\usepackage{hyperref}
\usepackage{subcaption}

% John Kormylo's original solution, which works when the subcaption package isn't used:
\renewcommand{\floatplace}[1]{% #1 = float type (e.g. figure)
   \begin{center}
     \hyperlink{#1.\csname thepost#1\endcsname}%
       {[\csname #1name\endcsname~\csname thepost#1\endcsname\ about here.]}
   \end{center}}

\begin{document}

\begin{figure}
\includegraphics{example-image}
\caption{A figure without subfigures}
\end{figure}

\begin{figure}[htb]
\centering
\begin{subfigure}{.5\textwidth}
  \centering
    \includegraphics[width=.9\textwidth]{example-image}
  \caption{first part}
\end{subfigure}%
%
\begin{subfigure}{.5\textwidth}
  \centering
    \includegraphics[width=.9\textwidth]{example-image}
  \caption{(second part)}
\end{subfigure}%
\caption{A figure with subfigures}
\end{figure}

\lipsum[1-2]


\end{document}

生成的文件aux表明解决方案不起作用的原因是 subcaption 将计数器的名称从 等更改figure.1figure.2figure.caption.2figure.caption.3它包含表格列表的以下几行(其中包含指向图形的有效超链接):

\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces This is figure 1.\relax }}{3}{figure.caption.2}}
\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces This is figure 2.\relax }}{4}{figure.caption.3}}

一种选择是重写 John Kormylo 的(原始)\floatplace命令,从 2 开始编号,并将头部重命名为figure.captiontable.caption。但在不同的文档中,辅助文件中的编号figure.caption从 18 开始。(不过,我还没有弄清楚如何在 MWE 中重现这一点。)这意味着这种解决方法行不通。

答案1

创建名为 的\caption调用。不幸的是,只能访问(实际上)。所以我们需要创建一个从到 的映射,并通过辅助文件将其移动到文档的开头。为了将它和反向链接结合起来,我用 替换了。\refstepcounter{figure}\hypertarget\@currentHref\floatplace\thefigure\thepostfigure\thefigure\@currentHref\caption\figurecaption

为了与原版保持一致,\floatplace我将其设置为与浮点类型无关。不过,我只添加了\figurecaption和的代码\newfigure

\documentclass{article}
\usepackage{endfloat}
\usepackage{mwe}
\usepackage{subcaption}
\usepackage{hyperref}

\makeatletter
\renewcommand{\floatplace}[1]{% #1 = float type (e.g. figure)
   \begin{center}
     \def\floatnumber{\csname thepost#1\endcsname}
     \def\floatname{\csname #1name\endcsname}
     \hypertarget{figureback\floatnumber}{}%
     \@ifundefined{#1anchor\floatnumber}%
       {[\floatname~\floatnumber\ about here.]}%
       {\hyperlink{\csname #1anchor\floatnumber\endcsname}%
         {[\floatname~\floatnumber\ about here.]}}
   \end{center}}

\newcommand{\figurecaption}[2][\empty]% #1=short caption (optional), #2=caption
 {\ifx\empty#1\relax \caption[#2]{\hyperlink{figureback\thefigure}{#2}}%
  \else \caption[#1]{\hyperlink{figureback\thefigure}{#2}}%
  \fi
  \immediate\write\@auxout{\string\newfigure{\thefigure}{\@currentHref}}}
\makeatother

\newcommand{\newfigure}[2]% #1 = \thefigure, #2 = \@currentHref
  {\expandafter\gdef\csname figureanchor#1\endcsname{#2}}

\begin{document}

\begin{figure}
\includegraphics{example-image}
\figurecaption{A figure without subfigures}
\end{figure}

\begin{figure}[htb]
\centering
\begin{subfigure}{.5\textwidth}
  \centering
    \includegraphics[width=.9\textwidth]{example-image}
  \caption{first part}
\end{subfigure}%
%
\begin{subfigure}{.5\textwidth}
  \centering
    \includegraphics[width=.9\textwidth]{example-image}
  \caption{(second part)}
\end{subfigure}%
\figurecaption{A figure with subfigures}
\end{figure}

\lipsum[1-2]

\end{document}

无论子标题施加什么奇怪的命名约定,这都应该有效。

答案2

我需要对表格和图形执行相同的操作,同时检查词汇表包的错误。(我认为它可能在某处定义了自己的“tablecaption”函数。)
如下所示:

\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{xcolor} %Farben
\usepackage{hyperref} %Links
\usepackage[nolists]{endfloat} % figures mit platzhalter und dann im Anhang
\renewcommand{\efloatseparator}{\mbox{}}
\usepackage{glossaries}
\usepackage{ifthen}

\usepackage{caption}
\usepackage{subcaption}
\usepackage{mwe}

\makeatletter
\renewcommand{\floatplace}[1]{% #1 = float type (e.g. figure)
    \begin{center}
        \def\floatnumber{\csname thepost#1\endcsname}
        \def\floatname{\csname #1name\endcsname}

        \ifthenelse{\equal{#1}{figure}}% define backlink based on figure or table
        {\def\backlink{figureback\floatnumber}}%
        {\def\backlink{tableback\floatnumber}}%

        \colorbox{yellow}{\floatname}
        \colorbox{red}{\backlink}

        \colorbox{orange}{\floatnumber}
        \colorbox{green}{\csname #1anchor\floatnumber\endcsname}

        \hypertarget{\backlink}{}
        \@ifundefined{#1anchor\floatnumber}%
        {[ \floatname~\floatnumber\ about here. ]}%
        {[ \hyperlink{\csname #1anchor\floatnumber\endcsname}%
            {\floatname~\floatnumber} about here. ]}
    \end{center}
}

%for figure
\newcommand{\figurecaption}[2][\empty]% #1=short caption (optional), #2=caption
{%
    \ifx\empty#1\relax %
        \caption[#2]{\hyperlink{figureback\thefigure}{#2}}%
    \else %
        \caption[#1]{\hyperlink{figureback\thefigure}{#2}}%
    \fi
    \immediate\write\@auxout{\string\newfigure{\thefigure}{\@currentHref}}%
}
\makeatother

\newcommand{\newfigure}[2]% #1 = \thefigure, #2 = \@currentHref
{\expandafter\gdef\csname figureanchor#1\endcsname{#2}}

\makeatletter
%for table
\newcommand{\tabcaption}[2][\empty]% #1=short caption (optional), #2=caption (tabcaption instead of tablecaption for glossaries)
{%
    \ifx\empty#1\relax %
        \caption[#2]{\hyperlink{tableback\thetable}{#2}}%
    \else %
        \caption[#1]{\hyperlink{tableback\thetable}{#2}}%
    \fi
    \immediate\write\@auxout{\string\newtable{\thetable}{\@currentHref}}%
}
\makeatother

\newcommand{\newtable}[2]% #1 = \thetable, #2 = \@currentHref
{\expandafter\gdef\csname tableanchor#1\endcsname{#2}}

\begin{document}
    \begin{table}
        \tabcaption{tab}
        \begin{tabular}{lcr}
            1 & 2 & 3 \\
        \end{tabular}
    \end{table}

    \begin{figure}
        \includegraphics{example-image}
        \figurecaption{A figure without subfigures}
    \end{figure}

    \begin{figure}[htb]
        \centering
        \begin{subfigure}{.5\textwidth}
            \centering
            \includegraphics[width=.9\textwidth]{example-image}
            \caption{first part}
        \end{subfigure}%
        %
        \begin{subfigure}{.5\textwidth}
            \centering
            \includegraphics[width=.9\textwidth]{example-image}
            \caption{(second part)}
        \end{subfigure}%
        \figurecaption{A figure with subfigures}
    \end{figure}

    \lipsum[1-2]
\end{document}

相关内容