Graphics, Figures & Tableslinks from TOC to subfigure don't work in book class

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
lauraf
Posts: 29
Joined: Tue Jan 13, 2009 12:44 pm

links from TOC to subfigure don't work in book class

Post by lauraf »

Hi all,

I am using the subfig package. I have noticed that the links from the table of contents to subfigures always take me to page 1, instead of taking me to the right page in the document.

Here is an example:

Code: Select all

\documentclass{book}

\usepackage[pdftex]{hyperref}

\usepackage{subfig}

\makeindex

\begin{document}

\tableofcontents
\setcounter{lofdepth}{2}
\listoffigures

\chapter{This is Chapter 1}

This is a reference to figure \ref{fig:test1}.

\newpage

\begin{figure}[!ht]
\centering
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        	\end{picture}
    	\caption{Testfigures}
	\label{fig:test1}
\end{figure}

\chapter{This is Chapter 2}

This is a reference to figure \ref{fig:test2} with subfigures \ref{fig:testsub1} and \ref{fig:testsub2}.

\newpage

\begin{figure}[!ht]
    \centering
    \unitlength .5cm
    \subfloat[SubFig1]
    {\label{fig:testsub1}
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        \end{picture}
    }
    \qquad
    \subfloat[SubFig2]
    {\label{fig:testsub2}
        \begin{picture}(10,10)
            \put(0,10){\line(1,-1){10}}
        \end{picture}
    }
    \caption{Testfigures}
    \label{fig:test2}
\end{figure}


\end{document}

I have noticed:
1. that the links from a reference within the text to the subfigure work fine
2. that this issue only occurs with the book/report classes

Does anybody know how to fix this or a workaround?

Many thanks!
Laura
Last edited by lauraf on Wed Jun 03, 2009 10:21 pm, edited 1 time in total.

Recommended reading 2024:

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

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

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

links from TOC to subfigure don't work in book class

Post by sommerfee »

lauraf wrote:Does anybody know how to fix this or a workaround?
All the (well known) problems of the combination subfig & hyperref, and the fact that the subfig package isn't maintained anymore, made me developing the subcaption package:

Code: Select all

\documentclass{book}

\usepackage{hyperref}

\usepackage{caption}
\usepackage[list=on]{subcaption}

\makeindex

\begin{document}

\tableofcontents
\listoffigures

\chapter{This is Chapter 1}

This is a reference to figure \ref{fig:test1}.

\newpage

\begin{figure}[!ht]
    \centering
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        \end{picture}
        \caption{Testfigures}
    \label{fig:test1}
\end{figure}

\chapter{This is Chapter 2}

This is a reference to figure \ref{fig:test2} with subfigures
\ref{fig:testsub1} and \ref{fig:testsub2}.

\newpage

\begin{figure}[!ht]
    \centering
    \unitlength .5cm
    \subcaptionbox{SubFig1\label{fig:testsub1}}
    {%
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        \end{picture}
    }
    \qquad
    \subcaptionbox{SubFig2\label{fig:testsub2}}
    {%
        \begin{picture}(10,10)
            \put(0,10){\line(1,-1){10}}
        \end{picture}
    }
    \caption{Testfigures}
    \label{fig:test2}
\end{figure}

\end{document}
(The subcaption package is part of the caption package bundle)

HTH,
Axel
lauraf
Posts: 29
Joined: Tue Jan 13, 2009 12:44 pm

Re: links from TOC to subfigure don't work in book class

Post by lauraf »

Thanks Axel,

Yes I was aware of the subcaption package. I have inserted the code you provided above in the relevant places in my document, but on the second run, I get the following:

! LaTeX Error: No counter 'KVtest' defined.

Any ideas what may be causing this?

Sorry it's a huge document (thesis) that calls many other files, so I wouldn't know how to add an example of it!

Alternatively, is there a way to tell hyperref to not include hyperlinks for the subfigures in the TOC?

Many many thanks
Laura
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

links from TOC to subfigure don't work in book class

Post by sommerfee »

lauraf wrote:! LaTeX Error: No counter 'KVtest' defined.
Any ideas what may be causing this?
Do you declare a new float type called "test" somewhere in your document preamble, e.g. with \newfloat? If yes you have to use \DeclareCaptionSubType{test} additionally before you can use sub-captions within this environment. (See subcaption document for details) It's the equivalent to the command \newsubfloat of the subfig package.

If this information doesn't help: Could you please give me the log file from the second run? Hopefully It will give me an idea what is going wrong.
Alternatively, is there a way to tell hyperref to not include hyperlinks for the subfigures in the TOC?
Sorry, I'm not aware of a way of doing this. The only way I know is to completely disable the TOC entries of the subfigures.

Axel
lauraf
Posts: 29
Joined: Tue Jan 13, 2009 12:44 pm

links from TOC to subfigure don't work in book class

Post by lauraf »

Axel, thanks very much. I have started from scratch and it now seems to work, I am not sure what was causing that error.

I now have one minor issue to which I could not find the answer in the manuals.

I have added this to the preamble:

Code: Select all

\usepackage[labelfont={rm,bf}, textfont=rm, font=small, aboveskip=0pt]{caption}
\usepackage[labelfont={rm,bf}, textfont=rm, font=small, list=true, listformat=subparens, belowskip=10pt]{subcaption}
\captionsetup[table]{position=top} 
\captionsetup[subtable]{position=top} 
Is it possible to set \subref to return the value in parenthesis?
E.g. \subref{Fig2a} to return
(a)
instead of
a
?

Many many thanks for all your help!
Laura
lauraf
Posts: 29
Joined: Tue Jan 13, 2009 12:44 pm

links from TOC to subfigure don't work in book class

Post by lauraf »

I have noticed one more issue :cry:

After the \appendix command, the subfigure counter doesn't seem to reset. For example:

Code: Select all

\documentclass{book}

\usepackage[pdftex]{hyperref}

\usepackage{caption}
\usepackage[list=on]{subcaption}

\makeindex

\begin{document}

\tableofcontents
%\setcounter{lofdepth}{2}
\listoffigures

\chapter{This is Chapter 1}

This is a reference to figure \ref{fig:test1}.

\newpage

\begin{figure}[!ht]
\centering
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        	\end{picture}
    	\caption{Testfigures}
	\label{fig:test1}
\end{figure}

\chapter{This is Chapter 2}

This is a reference to figure \ref{fig:test2} with subfigures \ref{fig:testsub1} and \ref{fig:testsub2}.

\newpage

\begin{figure}[!ht]
    \centering
    \unitlength .5cm
    \subcaptionbox{SubFig1\label{fig:testsub1}}
    {%
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        \end{picture}
    }
    \qquad
    \subcaptionbox{SubFig2\label{fig:testsub2}}
    {%
        \begin{picture}(10,10)
            \put(0,10){\line(1,-1){10}}
        \end{picture}
    }
    \caption{Testfigures}
    \label{fig:test2}
\end{figure}

\appendix
\chapter{This is the Appendix}

This is a reference to figure \ref{fig:testA} with subfigures \ref{fig:testsubA1} and \ref{fig:testsubA2}.

\newpage

\begin{figure}[!ht]
    \centering
    \unitlength .5cm
    \subcaptionbox{SubFigA1\label{fig:testsubA1}}
    {%
        \begin{picture}(10,10)
            \put(0,0){\line(1,1){10}}
        \end{picture}
    }
    \qquad
    \subcaptionbox{SubFigA2\label{fig:testsubA2}}
    {%
        \begin{picture}(10,10)
            \put(0,10){\line(1,-1){10}}
        \end{picture}
    }
    \caption{Testfigures}
    \label{fig:testA}
\end{figure}

\end{document}
I have figure I can reset it before each appendix with:

Code: Select all

\setcounter{subfigure}{0}
Is there another, obvious way to do this that I have missed?
Sorry for all the questions...
Thank you!
Laura
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

links from TOC to subfigure don't work in book class

Post by sommerfee »

lauraf wrote:Is it possible to set \subref to return the value in parenthesis?
E.g. \subref{Fig2a} to return
(a)
instead of
a
?
Yes. If you want the parens be part of the reference (and not be some kind of label format), redefine \thesubfigure so it contains the parens, and keep the visual stuff (labelformat and listformat) clean from them. For example you could change

Code: Select all

\usepackage[labelfont={rm,bf}, textfont=rm, font=small, list=true, listformat=subparens, belowskip=10pt]{subcaption}
to

Code: Select all

\usepackage[labelfont={rm,bf}, textfont=rm, font=small, list=true, labelformat=simple, belowskip=10pt]{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
Regarding the problem with the appendix: Unfortunately the current version of the subcaption package has a bug (which will be fixed in the very next version) which causes that the sub-figure counter will not be reset if the previous chapter contains only one single figure. If your document is affected you can add the following workaround after \usepackage[...]{subcaption}:

Code: Select all

% Workaround for bug in caption package v3.1 regading the
% numbering of subfigures in document classes with chapters
% which contain only one single figure
\makeatletter
\@addtoreset{subfigure}{chapter}
\makeatother
HTH,
Axel
lauraf
Posts: 29
Joined: Tue Jan 13, 2009 12:44 pm

links from TOC to subfigure don't work in book class

Post by lauraf »

Thank you Axel! Just so you know, the reset does not seem to work. I have added:

Code: Select all

\usepackage[labelfont={rm,bf}, textfont=rm, font=small]{caption}
\usepackage[labelfont={rm,bf}, textfont=rm, font=small, list=true, listformat=subparens]{subcaption}
\captionsetup[table]{position=top, skip=10pt} 
\captionsetup[subtable]{position=top, skip=10pt} 
\makeatletter
\@addtoreset{subfigure}{chapter}
\makeatother
and got the error shown in the TeXShop console attached. I have also attached the log file in case this is useful. Anyway, the \setcounter{subfigure}{0} fix seems to do the trick, so I am happy to go with that for now.

I have one final query, I promise... :oops:

For example:

Code: Select all

\documentclass{book}

\usepackage[pdftex]{hyperref}

\usepackage[labelfont={rm,bf}, textfont=rm, font=small]{caption}
\usepackage[labelfont={rm,bf}, textfont=rm, font=small, list=true, listformat=subparens]{subcaption}
\captionsetup[table]{position=top, skip=10pt} 
\captionsetup[subtable]{position=top, skip=10pt} 

\begin{document}

\begin{table}%
\centering
\caption{Table}%
\label{tab:table}%
\subcaptionbox{Sub1\label{tab:sub1}}
{%
\begin{tabular}{ll}
\hline
A&B\\
\hline
\end{tabular}
}%
\\%
\subcaptionbox{Sub2\label{tab:sub2}}
{%
\begin{tabular}{ll}
\hline
A&B\\
\hline
\end{tabular}
}%
\end{table}

\end{document}
Essentially, I need spacing above the subcaptions to separate Sub2 from the first subtable above it. But I also want spacing below Table (to separate the caption from the content in tables with no subtables). This however causes more space than I need in between Table and Sub1.

I have tried all possible combinations of skip, aboveskip, belowskip etc but nothing seems to work. The closest I can get is to have no space below Table, but then, as I said, there is no space between the caption and the content in tables with no subtables.

I promise I have tried all I could think of!
Many many thanks!
Laura
Attachments
thesis.log
(24 KiB) Downloaded 259 times
console.tiff
console.tiff (101.2 KiB) Viewed 6166 times
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

links from TOC to subfigure don't work in book class

Post by sommerfee »

lauraf wrote:and got the error shown in the TeXShop console attached.
Unfortunately the log file is not complete but cut at exactly 24k. It seems that this forum does not allow larger files as attachment ? Could you please send me the complete log file via e-mail? (You'll find my e-mail address in the caption and subcaption documentation.)
I have one final query, I promise... :oops:
No, please continue to ask your questions as long as you have any! Well, this is what this forum is for, there is no (hopefully) no question limit here. ;)
I have tried all possible combinations of skip, aboveskip, belowskip etc but nothing seems to work.
In my point of view it's best to forget about aboveskip and belowskip. especially the latter is rarely of any use. Just set the skip between caption and content with the "skip=" option and make all the other skips for yourself.

For example:

Code: Select all

\documentclass{book}

\usepackage{hyperref}

\usepackage[labelfont={rm,bf}, textfont=rm, font=small]{caption}
\usepackage[labelfont={rm,bf}, textfont=rm, font=small, list=true, listformat=subparens]{subcaption}
\captionsetup[table]{position=top, skip=10pt}
\captionsetup[subtable]{position=top, skip=10pt}

\begin{document}

\begin{table}%
\centering
\caption{Table}%
\label{tab:table}%
\subcaptionbox{Sub1\label{tab:sub1}}
{%
\begin{tabular}{ll}
\hline
A&B\\
\hline
\end{tabular}
}%
\par
\bigskip % or \medskip or \vspace{...} or ...
\subcaptionbox{Sub2\label{tab:sub2}}
{%
\begin{tabular}{ll}
\hline
A&B\\
\hline
\end{tabular}
}%
\end{table}

\end{document}
HTH,
Axel
lauraf
Posts: 29
Joined: Tue Jan 13, 2009 12:44 pm

Re: links from TOC to subfigure don't work in book class

Post by lauraf »

Thanks Axel, that works beautifully (for the spacing).

Re. the log file, I have trashed the aux files, ru-run everything with the offending code, and again it is truncated as in the file I attached in the previous post. So 24k is the size of the log file. I have also re-run everything from within Terminal (rather than TeXShop) but I get the same.

Let me know if there is any other file that I can send that may be useful!

Again, many thanks for all your help!
Laura
Post Reply