25

I am trying to create a reference to a float that doesn't use a caption. If I include \label{foo} within the float and reference it using \pageref{foo}, the correct page number is displayed in my pdf document but the hyperlink created by the hyperref package links to a different page (the first page of the section). If I include a caption before the label in the float, the hyperref link goes to the correct page.

Is there a way to get the hyperref link to work correctly without including a caption in the float? Or else is there a way to suppress the display of a caption so I can include one without it being shown?

Below is a minimal example. If I process it using pdflatex, I get three pages. The "figure" is shown on the second page, and the third page says, correctly, "See figure on page 2." But the hyperlink on the '2' says "Go to page 1", and if I click it it takes me to page 1.

If I put an empty \caption{} before the \label{foo}, then the hyperlink works correctly, but I don't want to show a caption for my float.

\documentclass[11pt]{memoir}

\usepackage{hyperref}

\begin{document}

some text
\clearpage


\begin{figure}
  a figure
  \label{foo}
\end{figure}

more text
\clearpage


See figure on page \pageref{foo}.

\end{document}
ecto
  • 333
  • 1
  • 3
  • 11
  • 2
    I assumes this works as expected with a caption in the float? – Mica Apr 30 '10 at 16:34
  • Correct -- if I include a caption the hyperlink goes to the right page. – ecto Apr 30 '10 at 16:42
  • 1
    Do you put \caption before \label in the float? LaTeX produces wrong figure numbers otherwise. Please post a minimal example so we can try. – sastanin Apr 30 '10 at 16:47

3 Answers3

22

The \label command references the last invocation of \refstepcounter. \caption recognises that it is in a figure environment and calls \refstepcounter{figure}. You can call \refstepcounter by yourself.

To avoid skipping a number in the series of figures, you may create an own, meaningless counter with \newcounter{dummy}. The result:

\documentclass{scrreprt}
\usepackage{hyperref}
\newcounter{dummy}
\begin{document}

\chapter{First}

\newpage
\begin{figure}
{\Huge FIGURE}
\refstepcounter{dummy}
\label{fig:figure}
\end{figure}

\chapter{Second}

Goto \pageref{fig:figure}

\end{document}

Creates an hyperlink to the end of the figure. (works on my machine :-) Note than \ref{fig:figure} is meaningless.

Meinersbur
  • 7,881
  • 1
  • 27
  • 29
21

Right before the label, use \phantomsection, like so:

\documentclass{memoir}
\usepackage{hyperref}
\begin{document}
some text
\clearpage
\begin{figure}
a figure
\phantomsection
\label{foo}
\end{figure}
more text
\clearpage
See figure on page \pageref{foo}.
\end{document}

:)

Mark VY
  • 1,489
  • 16
  • 31
  • The advantage of this over @Meinersbur's solution using \refstepcounter is that it retains the correct numbering when the label is referenced using \autoref, for example. (\autoref will still not show it as "Figure", it will use whatever text is assigned to \sectionautorefname .) – Daira Hopwood Jul 22 '19 at 17:50
0

Loading the caption package should suppress caption output of empty captions. The labels for floats are always determined by the caption command preceding the label command.

Svante
  • 50,694
  • 11
  • 78
  • 122
  • Why then does the page number display correctly for \pageref if I don't include a caption? Only the hyperlink is incorrect. I just tried loading the caption package but unfortunately it did not suppress the caption output. It still shows as "Figure 0.1:". – ecto Apr 30 '10 at 17:15