指向 \thefigure 的超目标

指向 \thefigure 的超目标

希望这是我问的最后一个问题,关于扩展图形标题宏。在上一个版本中这里hyperref不影响\addefcline\efcline,每次使用该函数时,我都必须在图形上定义一个超目标\extcaption

简而言之,我如何才能创建一个指向的超目标,\thefigure以使该过程自动化?此外,是否有某种ifhyperref包(用于检查 hyperref 是否已加载)?

这是 MWE

\documentclass[a4paper,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{caption}

% Redefinition of \addcontentsline
\long\def\addefcline#1#2#3{%
  \addtocontents{#1}{\efcline{#2}{#3}{\thepage}}}

% Redefinition of \contentsline
\makeatletter
\long\def\efcline#1{\csname l@#1\endcsname}
\makeatother

% Extended figure caption
\newcommand{\extcaption}[1]{% Write to .efc file
  \addefcline{efc}{extfig}{{%
    \bfseries \protect\hyperlink{fig:\thefigure}{Figure \thefigure, page~\thepage:}} %
    \unexpanded{\unexpanded{#1}}%
  }%
}
\makeatletter
\newcommand{\printextcaptions}{\@starttoc{efc}}% Read .efc file
\newcommand{\l@extfig}[2]{\noindent #1 \vspace{\parskip} \par}% How each .efc entry is handled
\makeatother

\usepackage[linktoc=all]{hyperref}

\begin{document}

\chapter{A chapter}

\section{A section}
\lipsum[1]

\begin{figure}
\includegraphics{example-image-a.pdf}
\caption{Picture of a big A}\hypertarget{fig:\thefigure}{}
\extcaption{\lipsum[1-2]}
\end{figure}

\begin{figure}
\includegraphics{example-image-b.pdf}
\caption{Picture of a big B}\hypertarget{fig:\thefigure}{}
\extcaption{The reasons why B is awful
\begin{itemize}
\item "B" like Bad
\item "B" like Banana
\end{itemize}
}
\end{figure}

\section{Extended figure captions}
\printextcaptions
\end{document}

答案1

hyperref使用的变量找到了解决方案。这\@currentHref给出了最终的代码:

% Redefinition of \addcontentsline
\long\def\addefcline#1#2#3{%
  \addtocontents{#1}{\efcline{#2}{#3}{\thepage}}}
% Redefinition of \contentsline
\makeatletter
\long\def\efcline#1{\csname l@#1\endcsname}
% Extended figure caption
\AtBeginDocument{\@ifpackageloaded{hyperref}{ % If hyperref loaded
\newcommand{\extcaption}[1]{% Write to .efc file
  \addefcline{efc}{extfig}{{%
    \bfseries \protect\hyperlink{\@currentHref}{Figure \thefigure, page~\thepage:}} %
    \unexpanded{\unexpanded{#1}}}}}{% If hyperref not loaded
\newcommand{\extcaption}[1]{% Write to .efc file
  \addefcline{efc}{extfig}{{%
    \bfseries Figure \thefigure, page~\thepage:} %
    \unexpanded{\unexpanded{#1}}%
  }}}}
% Read .efc file
\newcommand{\printextcaptions}{\@starttoc{efc}}
\newcommand{\l@extfig}[2]{\noindent #1 \vspace{\parskip} \par}% How each .efc entry is handled
\makeatother

回顾一下,在浮动图形中添加了一个扩展的图形标题,如下所示

\begin{figure}
\includegraphics{example-image-a.pdf}
\caption{Picture of a big A}
\extcaption{\lipsum[1-2]}
\end{figure}

,生成一个.efc包含所有内容的文件\extcaption{},由 打印

\printextcaptions

相关内容