LaTeX forum ⇒ Graphics, Figures & Tablesissue with hyperref and subfig Topic is solved

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

issue with hyperref and subfig

Postby emjotenel » Fri May 07, 2010 2:36 pm

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.
  1. This is pdfTeX, Version 3.1415926-1.40.10 (MiKTeX 2.8)


During compilation of an output pdf, I also get this:
  1. Package caption Warning: \caption will not be redefined since it's already
  2. (caption) redefined by a document class or package which is
  3. (caption) unknown to the caption package.
  4. See the caption package documentation for explanation.
  5.  
  6. LaTeX Warning: Command \selectfont has changed.
  7. Check if current package is valid.


My tex file contains:
  1. \documentclass[11pt,a4paper,oneside,final]{book}
  2.  
  3. \usepackage{caption,everysel,keyval,ragged2e,subfig}
  4. \captionsetup{margin=10pt,font=small,labelfont=bf}
  5. \usepackage{fancyhdr}
  6. \usepackage[pdftex]{hyperref}
  7. \usepackage{graphicx}
  8. \usepackage[all]{hypcap}


A typical 2x2 figure declaration looks like this:
  1. \begin{figure}[t!bp]
  2. \centering
  3. \subfloat[][Caption1.]
  4. {\label{file1.PDF}\includegraphics[width=7cm]{file1.PDF}}
  5. \subfloat[][Caption2.]
  6. {\label{file2.PDF}\includegraphics[width=7cm]{file2.PDF}}
  7. \\
  8. \subfloat[][Caption3.]
  9. {\label{file3.PDF}\includegraphics[width=7cm]{file3.PDF}}
  10. \subfloat[][Caption4.]
  11. {\label{file4.PDF}\includegraphics[width=7cm]{file4.PDF}}
  12. \\
  13. \newcommand*{\tempA}{Short caption}
  14. \newcommand*{\tempB}{Description.}
  15. \caption[\tempA]{\textbf{\tempA} -- \tempB}\label{fig:four-by-four}
  16. \end{figure}


Any thoughts will be much appreciated!

Thanks in advance,
emjotenel

Tags:

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

Postby sommerfee » Fri May 07, 2010 7:57 pm

emjotenel wrote:During compilation of an output pdf, I also get this:
[code]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.


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?

My tex file contains:


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.

subfig


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.)

\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.

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


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

Postby emjotenel » Wed May 12, 2010 1:15 pm

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:
  1. \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:
  1. \subfloat[][Caption1.]\label{file.PDF}
  2. {\includegraphics[width=7cm]{file.PDF}}

but then I got compilation errors:
  1. Chapter 1.
  2. ! Argument of \sf@sub@label has an extra }.
  3. <inserted text>
  4. \par
  5. l.30 \subfloat[][Subcaption1.]\label
  6. {fig:sub_top}
  7. ?

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 267 times
file.pdf
A simplistic graphics file
(1 KiB) Downloaded 262 times
main.tex
The main tex file
(1.68 KiB) Downloaded 243 times

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

Postby sommerfee » Thu May 13, 2010 10:13 am

- 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 506 times
main.tex
(1.75 KiB) Downloaded 419 times

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

Postby emjotenel » Thu May 13, 2010 5:01 pm

Thank you very much!

That is exactly what I had in mind. :D


Regards,
emjotenel

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

Postby sommerfee » Thu Jan 17, 2013 10:09 am

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests