# How to put hyperlinks in endfloat's markers?

The documentation of the endfloat package explains how to customize the text in the markers from the default of "[Table 3 about here.]" But I would like to customize the markers to include a hyperlink to the figure at the end of the document. How can this be accomplished?

Here's a subquestion: In customizing the marker, \thepostfigure refers to the figure number. Is there a similar command like \thefigurereference that refers to the reference to the figure, so that the marker text can be something like Figure \ref{\thefigurereference}} about here.?

Update 2: In the comments, I asked about a backlink as well, so that the caption of the figure at the end of the document would hyperlink back to the "[Figure 1 about here]" float embedded in the text. @John Kormylo's accepted solution delivers this.

Update 1 [edited to be clearer it's referring to @John Kormylo's original answer, not his revised one]:

@John Kormylo's (original) solution works, but as I have learned it is incompatible with the subcaption package. So, my revised question is: Is there a way to hyperlink to figures that still works with caption?

Here is a MWE using his solution where the hyperlinks don't go to the figures at the end. Figure 2 is just there to demonstrate why I need subcaption - the linking still fails even without it, so long as the subpcaption package is used.

\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}
{[\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}


The aux file produced suggests that the reason the solution didn't work is that subcaption changes the name of the counters from figure.1, figure.2 etc. to figure.caption.2, figure.caption.3 etc. It has these lines for the list of tables (which has working hyperlinks to the figures):

\@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}}


One option would be to re-write John Kormylo's (original) \floatplace command starting numbering at 2 and re-naming the head to figure.caption or table.caption. But in in a different document, the numbering for figure.caption in the aux file starts at 18. (I haven't figured out how to reproduce this in a MWE, though.) This means that such a workaround won't do.

• Welcome to TeX.SX!! It is better to post a full minimal working example that starts with a \documentclass command, has a minimal preamble and then \begin{document}...\end{document}. Unless the problem is a compilation error, the code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much ore likely that they will! – Andrew Feb 7 '16 at 4:36
• For referencing, use \label{<figure>} in the figure and then \ref{<figure>} where you want the reference, where <figure> is whatever label you want. For hyperlinks it might be enough to include \usepackage{hyperref} -- if you'd included a MWE I'd test this:) – Andrew Feb 7 '16 at 4:38

The \caption calls \refstepcounter{figure} which creates a \hypertarget named \@currentHref. Unfortunately, \floatplace only has access to \thefigure (actually \thepostfigure). So we need to create a map from \thefigure to \@currentHref and move it up to the beginning of the document via the aux file. To combine this and the backlink, I replaced \caption with \figurecaption.

In keeping with the original \floatplace I made it float type independent. However, I only added code for \figurecaption and \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}%
\end{center}}

\newcommand{\figurecaption}[2][\empty]% #1=short caption (optional), #2=caption
\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}


This should work no matter what weird naming convention subcaption imposes.

• John, your solution works in your MWE, but when I tried to use it in my document it failed. The hyperlinks instead directed to the top of the document. Turns out the conflict was the subcaption package. Any ideas how to make the links work properly with subcaption? – Seth Feb 7 '16 at 21:55
• I can see from the aux file that the list of figures uses references like figure.caption.19` (for figure 1) which then proceed sequentially. I think the increment for these labels (18 in this case) depends on the number of subfigures there are - if there are no subfigures, the increment is 1. So if we could figure out the increment, I think we could modify your solution to work with subcaption. – Seth Feb 7 '16 at 23:04
• Subcaption should have no effect on the figure counter (it uses a different counter). Please provide an MWE which demonstrates the actual problem. – John Kormylo Feb 8 '16 at 20:49
• John, I've put up (after some delay) a MWE of the interaction between subcaption and your solution, plus my poking around in the aux file for what goes wrong. Care to take another crack at it? – Seth Feb 19 '16 at 1:18
• Okay, I fixed to work with subcaption. – John Kormylo Feb 19 '16 at 4:09