Graphics, Figures & Tablesissue with hyperref and subfig

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
emjotenel
Posts: 3
Joined: Fri May 07, 2010 2:16 pm

issue with hyperref and subfig

Post by emjotenel »

Hello,

I have a little problem with hyperref's linking of captions in subfigures. Say, let subfigures be 1.1a through 1.1d and organised in 2x2 as follows:
a b
c d

When clicking in text on link to the whole figure 1.1, it points to the top of the figure (GREAT). When clicking on either 1.1a or 1.1c links, they point to the top of the respective subfigures (GREAT). However, when clicking on either 1.1b or 1.1d links, they point to the top of the respective subfigure captions (NOT GREAT).

I'm using MiKTeX 2.8 and all the packages were updated last week.

Code: Select all

This is pdfTeX, Version 3.1415926-1.40.10 (MiKTeX 2.8)
During compilation of an output pdf, I also get this:

Code: Select all

Package caption Warning: \caption will not be redefined since it's already
(caption)                redefined by a document class or package which is
(caption)                unknown to the caption package.
See the caption package documentation for explanation.

LaTeX Warning: Command \selectfont   has changed.
               Check if current package is valid.
My tex file contains:

Code: Select all

\documentclass[11pt,a4paper,oneside,final]{book}

\usepackage{caption,everysel,keyval,ragged2e,subfig}
\captionsetup{margin=10pt,font=small,labelfont=bf}
\usepackage{fancyhdr}
\usepackage[pdftex]{hyperref}
\usepackage{graphicx}
\usepackage[all]{hypcap}
A typical 2x2 figure declaration looks like this:

Code: Select all

\begin{figure}[t!bp]
  \centering
  \subfloat[][Caption1.]
  {\label{file1.PDF}\includegraphics[width=7cm]{file1.PDF}}
  \subfloat[][Caption2.]
  {\label{file2.PDF}\includegraphics[width=7cm]{file2.PDF}}
  \\
  \subfloat[][Caption3.]
  {\label{file3.PDF}\includegraphics[width=7cm]{file3.PDF}}
  \subfloat[][Caption4.]
  {\label{file4.PDF}\includegraphics[width=7cm]{file4.PDF}}
  \\
  \newcommand*{\tempA}{Short caption}
  \newcommand*{\tempB}{Description.}
  \caption[\tempA]{\textbf{\tempA} -- \tempB}\label{fig:four-by-four}
\end{figure}
Any thoughts will be much appreciated!

Thanks in advance,
emjotenel

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

issue with hyperref and subfig

Post by sommerfee »

emjotenel wrote:During compilation of an output pdf, I also get this:

Code: Select all

Package caption Warning: \caption will not be redefined since it's already
(caption)                redefined by a document class or package which is
(caption)                unknown to the caption package.
See the caption package documentation for explanation.[/quote]

When you take a look at the log file you'll find the definition of the macro which the caption package don't know. Can you please post these lines here so I can get an idea what is going wrong?

[quote]My tex file contains:[/quote]

Are you able to build a full working example which I can compile at home so I can see what is going wrong? This would be very helpful.

[quote]
subfig
[/quote]

Please note that the hyperref support within the subfig package is broken. You could give my subcaption package a try instead. (The subcaption package is part of the caption package bundle.)

[quote]\usepackage[all]{hypcap}[/quote]

When using the caption package you don't need this, it could even be troublesome in some cases. Just drop this line and take a look at the caption package documentation, section about hyperref support.

[quote]
  \subfloat[][Caption1.]
  {\label{file1.PDF}\includegraphics[width=7cm]{file1.PDF}}[/quote]

Better put the \label into the caption and not into the (sub-)figure contents.

Axel
emjotenel
Posts: 3
Joined: Fri May 07, 2010 2:16 pm

issue with hyperref and subfig

Post by emjotenel »

Thank you for your reply and apologies for a late response.

I am submitting a proof-of-principle tex file alongside with a simplistic graphics file (it really is not that crucial, but I am attaching it for your convenience).

I have also included another example in the code---with three subfigures on top of each other. In this case, hyperref links to given subfigures point to the top of those subfigures, which seems to suggest that hyperref has problems with subfigures if they are aligned side-by-side. :idea:
\usepackage[all]{hypcap}
When using the caption package you don't need this, it could even be troublesome in some cases. Just drop this line and take a look at the caption package documentation, section about hyperref support.
So I commented out the hypcap line and adjusted the captionsetup to look like this:

Code: Select all

\captionsetup{margin=10pt,font=small,labelfont=bf,hypcap=true}
and then links to main figures pointed to their captions. However, with hypcap-package enabled, those links pointed to the top of respective environments (which is what I need). I realise that the new caption package is supposed to take over that functionality, but at least in this instance that does not seem to be the case.
\subfloat[][Caption1.]
{\label{file.PDF}\includegraphics[width=7cm]{file.PDF}}
Better put the \label into the caption and not into the (sub-)figure contents.
I tried putting label right after the subfloat caption declaration:

Code: Select all

\subfloat[][Caption1.]\label{file.PDF}
{\includegraphics[width=7cm]{file.PDF}}
but then I got compilation errors:

Code: Select all

Chapter 1.
! Argument of \sf@sub@label has an extra }.
<inserted text>
                \par
l.30   \subfloat[][Subcaption1.]\label
                                      {fig:sub_top}
?
Please correct me if I did something stupid. :roll:
When you take a look at the log file you'll find the definition of the macro which the caption package don't know. Can you please post these lines here so I can get an idea what is going wrong?
I have attached the whole log file.


Thanks again,
emjotenel
Attachments
main.log
Log file
(14.01 KiB) Downloaded 539 times
file.pdf
A simplistic graphics file
(1 KiB) Downloaded 442 times
main.tex
The main tex file
(1.68 KiB) Downloaded 499 times
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Re: issue with hyperref and subfig

Post by sommerfee »

- If you include the hypcap package, all hyperref anchors regarding figures and tables will be done by the hypcap package (and not by the caption package). Since the hypcap package is not adapted to the subfig package (and vice versa) you'll usually not get correct hyperlinks to sub-figures when including the hypcap package. (But some seems to be correct because of the wrong \label placement.)

- The reason why the caption package option "hypcap=true" (which is the default setting) does not work for you is because the caption package has detected an incompatibility (therefore you get a warning) which makes this (and other) feature(s) non-functional. As I found out from your log file the version of the caption package you use is too old for working fine with the hyperref package version you use. So updating the caption package to the recent version 3.1m will clear this.

- After updating the caption package and removing the usage of the hypcap package nearly all links to sub-figures should be fine except the ones for 1.2b and 1.2d, so I can confirm that there are problems with side-by-side sub-figures when using the subfig package. (As already told the hyperref support of the subfig package is broken. The caption packages fixes some of the hyperref issues for the subfig package (by patching some subfig code), but is not able to fix this problem. For fixing this I would need to patch much more subfig code inside my caption package which would not be the right way of doing it - the subfig package needs to be fixed instead.) For that (any many other) reason(s) I wrote my own subcaption package as a replacement.

- After correcting the label positions inside the sub-figures (they should be inside the caption and not inside the figure) the subcaption package will give you proper hyperlinks to all the sub-figures. You even have the choice if you would like to point all sub-figure hyperlinks to the main figure (default), or if all hyperlinks should jump to the sub-figures instead (subcaption package option "hypcap=true"). When using the latter you can also adjust the spacing between the hyperlink target and the sub-figure by using the subcaption package option "hypcapspace=...". (See caption and subcaption package documentation for details.)

Attached to this posting you'll find a modified version of main.tex and main.pdf.

If you have any questions please don't hesitate to ask.

Axel (author & maintainer of the caption package bundle)
Attachments
main.pdf
Compiled with the subcaption package option "hypcap=true"
(54.96 KiB) Downloaded 729 times
main.tex
(1.75 KiB) Downloaded 723 times
emjotenel
Posts: 3
Joined: Fri May 07, 2010 2:16 pm

Re: issue with hyperref and subfig

Post by emjotenel »

Thank you very much!

That is exactly what I had in mind. :D


Regards,
emjotenel
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

issue with hyperref and subfig

Post by sommerfee »

Here comes an updated example file which uses documented features of the subcaption package only, so it still compiles on up-to-date TeX systems.

Please note that it (still) needs the file file.pdf offered by emjotenel in http://www.latex-community.org/forum/vi ... 769#p33769
Post Reply