1

Hopefully the last question I would ask about the extended figure caption macro. In the last version Here, hyperref do not affect \addefcline or \efcline, leaving me the task of having to define an hypertarget on the figure every time I use the \extcaption function.

In short, how could I make an hypertarget that point at \thefigure to make the process automatic ? Also, is there some sort of ifhyperref package (to check whether hyperref is loaded) ?

Here is the 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}
3
  • 1
    \@ifpackageloaded{hyperref}{ true }{ false } should do. As for \hypertarget, you might look at the hycap package. – John Kormylo Nov 23 '15 at 2:34
  • \AtBeginDocument\\@ifpackageloaded{hyperref}{}{} does the trick indeed. However, hypcap requires you to either set the anchor manually, or automatically but making sure all figures have captions ... isn't there a way to link aimply to \figure ? I asked a similar question previously to link at a \thesection tex.stackexchange.com/questions/270141/… – HcN Nov 23 '15 at 12:02
  • 1
    I suspect you don't NEED a caption with hycap, you just need \stepcounter{figure}. – John Kormylo Nov 23 '15 at 15:39
1

Found a solution using the hyperref's \@currentHref variable.This gives the final code:

% 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

To recap, an extended Figure caption is added inside the figure float like

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

, generating an .efc file containing all the content of \extcaption{}, which are printed by

\printextcaptions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.