如何在 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表明該解決方案不起作用的原因是子標題將計數器的名稱從 等更改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。但在另一個文件中,aux 文件中的編號figure.caption從 18 開始。

答案1

建立命名.不幸的是,只能訪問(實際上)。因此,我們需要建立一個從到 的映射,並透過 aux 檔案將其移至文件的開頭。為了將其與反向連結結合起來,我替換為.\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}

相關內容