4

I'm using subfloats rather extensively, and want to use hyperref to refer to the subfloats.

It works, sort-of. In the below example, when clicking the hyperref to the first figure, the pdf goes nicely to the top-left of the figure. However, when clicking the reference to the second figure, the pdf goes to near the bottom of the second figure, such that most of the figure is out of view.

screenshot

Hyperref goes to the light green crosses, which is fine for 1.1a), but I want it to go to the blue cross for `1.1b)

Below is my MWE, which I build by running pdflatex twice using texlive 2012.

\documentclass[a4paper,11pt]{report}

\usepackage{chngcntr}
\counterwithin{figure}{chapter}
\usepackage{graphicx}
\usepackage{caption,subfig}
\usepackage{hyperref}

\begin{document}
\chapter{First chapter}

This will be a reference to the first part of Figure~\ref{fig:firstfigure}

This will be a reference to the second part of Figure~\ref{fig:secondfigure}

\begin{figure}[ht]
\centering
    \subfloat[Some subfloat caption for the first figure goes here.]{\label{fig:firstfigure}\includegraphics[width=0.4\textwidth]{./placeholder}}\hspace{0.15\textwidth}              
    \subfloat[And some other caption for the second subfigure goes here.]{\label{fig:secondfigure}\includegraphics[width=0.4\textwidth]{./placeholder}}
\caption{\protect\subref{fig:firstfigure} is the first figure. \protect\subref{fig:secondfigure} is the second figure.}\label{fig:somefigures}
\end{figure}

\end{document}

Any insight in how to fix the hyperref behaviour for the second subfigure is much appreciated!

  • 1
    Hi and welcome to TeX.SX! – bersanri Jun 19 '13 at 12:16
  • The hyperref support of the subfig package is buggy, this is a known problem. See also: tex.stackexchange.com/questions/13625/… – user2574 Jun 19 '13 at 20:03
  • Thanks! I was already messing around in the direction of subcaption, and I think I got it working properly. I'll post the MWE of the solution I found in a moment. – IceRabbit Jun 19 '13 at 20:11
  • No worries, thanks for your help! I see you've ditched the \protect before the \subref commands. Is there any particular reason for that? – IceRabbit Jun 19 '13 at 20:18
  • @IceRabbit See my comment to your answer which you just have deleted. BTW: I have deleted my answer after seeing that you have just answered, too. :-D I would suggest that you undelete your answer!? At least we should keep one of our answers :-D – user2574 Jun 19 '13 at 20:21
2

I managed to find the solution by ditching the subfig package in favour of the subcaption package, as also suggested by Axel Sommerfeldt.

The MWE of my solution is

\documentclass[a4paper,11pt]{report}

\usepackage{chngcntr}
\counterwithin{figure}{chapter}
\usepackage{graphicx}
\usepackage[hypcap=true]{caption}
\usepackage[hypcap=true]{subcaption}
\usepackage{hyperref}

\begin{document}
\chapter{First chapter}

This will be a reference to the first part of Figure~\ref{fig:firstfigure}

This will be a reference to the second part of Figure~\ref{fig:secondfigure}

\begin{figure}[h]
\centering
    \subcaptionbox{Some subfloat caption for the first figure goes here.\label{fig:firstfigure}}{\includegraphics[width=0.4\linewidth]{./placeholder}}\hspace{0.15\textwidth}
    \subcaptionbox{And some other caption for the second subfigure goes here.\label{fig:secondfigure}}{\includegraphics[width=0.4\linewidth]{./placeholder}}
\caption{\protect\subref{fig:firstfigure} is the first figure. \protect\subref{fig:secondfigure} is the second figure.}
\label{fig:somefigures}
\end{figure}

\end{document}
|improve this answer|||||
  • BTW: The \protect isn't needed in front of \subref when using the subcaption package, it should work fine without, too. (But this extra \protect does not make any harm, it's just superfluous.) – user2574 Jun 19 '13 at 20:19

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.