# Hypertarget pointing at \thefigure

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 \contentsline
\makeatletter
\long\def\efcline#1{\csname l@#1\endcsname}
\makeatother

% Extended figure caption
\newcommand{\extcaption}[1]{% Write to .efc file
\unexpanded{\unexpanded{#1}}%
}%
}
\makeatletter
\newcommand{\l@extfig}[2]{\noindent #1 \vspace{\parskip} \par}% How each .efc entry is handled
\makeatother

\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 Banana
\end{itemize}
}
\end{figure}

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

• \@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
• I suspect you don't NEED a caption with hycap, you just need \stepcounter{figure}. – John Kormylo Nov 23 '15 at 15:39

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

% Redefinition of \addcontentsline
% Redefinition of \contentsline
\makeatletter
\long\def\efcline#1{\csname l@#1\endcsname}
% Extended figure caption
\newcommand{\extcaption}[1]{% Write to .efc file
\newcommand{\extcaption}[1]{% Write to .efc file
\bfseries Figure \thefigure, page~\thepage:} %
\unexpanded{\unexpanded{#1}}%
}}}}
\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