There are quite a bit of related questions:
- hyperref not working. It goes to wrong page. Use
\usepackage{tocbibind}
. - Link of “List of figures” leads to Table of Contents. Use
\usepackage{tocbibind}
. - Correct hyperlink to the index. Not sure...
- KOMA book: correct hyperlink to the index?. Use
index=totoc
. - Link to index in TOC points does not point to index, but to whatever is right before index. Use
\phantomsection
or\usepackage{tocbibind}
. - Links do not lead to right pages. Use
\phantomsection
and\addcontentsline
. Puthyperref
last. - Latex Table of Contents Links to Wrong Section. Use
\phantomsection
(optional\newpage
before) and\addcontentsline
, or\usepackage[nottoc]{tocbibind}
. - Hyperref does not work for List of Fig/Tables and previous textblocks (Abstract etc.). Use
\usepackage{tocbibind}
. - fncychap messes up bookmarks. Use
\phantomsection
and\addcontentsline
. - fncychap and hyperref messes up page references. Use
\usepackage{hyperref}
after\usepackage{fncychap}
. - wrong page numbering in the bibliography using fncychap. Use
\phantomsection
.
However, after trying out many of the suggested solutions (which are sometimes contradictory) for a couple of hours, and still failing, I thought I post this here now as another question (and hopefully I can add the solution to this case later).
In earlier variants of my code, even the link to glossaries and bibliography was broken, but that seems to work now. In this latest version, everything seems to work, except the link to figures and tables.
Note that the code comes from a template for a PhD thesis, and in principle I think I should not change too much in there.
Example code, test.tex
:
\documentclass[listof=totoc]{scrbook}
\usepackage{amsmath}
\usepackage[Bjarne]{fncychap}
\usepackage{hyperref}
% for original fncychap
\makeatletter
\DeclareOldFontCommand{\rm}{\normalfont\rmfamily}{\mathrm}
\makeatother
\title{Thesis}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\pagestyle{headings}
\chapter{Introduction}
...
\backmatter
\listoffigures
\listoftables
\end{document}
For reference, my Latex version:
$ pdflatex --version
pdfTeX 3.14159265-2.6-1.40.19 (TeX Live 2018)
kpathsea version 6.3.0
Copyright 2018 Han The Thanh (pdfTeX) et al.
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.34; using libpng 1.6.34
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 4.00
I already tried multiple things, but maybe in the wrong combination, or wrong import order, or wrong options, or so.
\phantomsection
somewhat works, but it then links to the end of the previous chapter, not the beginning of the new chapter.\usepackage{tocbibind}
(or with some of the options) does not seem to work. More specifically:\usepackage{tocbibind}
(without options) adds a "Contents" chapter (which I don't want) and breaks the bibliography chapter link in the TOC. The glossary link still works, and the list of tables/figures link still does not work.\usepackage[nottoc]{tocbibind}
is like without options but without the extra "Contents" chapter.\usepackage[nottoc,notlof,notlot]{tocbibind}
is just the same as\usepackage[nottoc]{tocbibind}
.notlof
ornotlot
does not seem to have an effect.
index=totoc
in the documentclass options (via) does not seem to have an effect.\usepackage{nomencl}
(with variants of the options) does not seem to have an effect.
Probably relevant are also the hyperref
options. I think I need to have hypertexnames=false
in there, as this was in the original template like this. Also, changing this seems to break other things.
Also, in some places, it is stated that hyperref
should come first. In other places, it is stated that it should come last. In yet other places, it is stated that glossaries
should come after hyperref
.
I noticed, the \usepackage[Bjarne]{fncychap}
seems to cause this. If I remove this, it works. However, I need to use it.
(Also, I noticed that the template comes with the fncychap
v1.34 package. However, if I use the normal one (from Tex Live 2018), I get the same problem. In addition, maybe some Class scrbook Error: undefined old font command \rm
error, but that is easy to work around, and not related here.)
Edit: After randomly trying around, after the answer from Ulrike Fischer, without really understanding what I'm doing, I added this (right after the last \usepackage
):
\makeatletter
\renewcommand\@schapter[1]{%
\begingroup
\let\@mkboth\@gobbletwo
\Hy@GlobalStepCount\Hy@linkcounter
\xdef\@currentHref{\Hy@chapapp*.\the\Hy@linkcounter}%
\Hy@raisedlink{%
\hyper@anchorstart{\@currentHref}\hyper@anchorend
}%
\endgroup
\H@old@schapter{#1}%
}
\makeatother
(I got this code from my /usr/local/texlive/2018/texmf-dist/tex/latex/koma-script/hyperref.hak
file.)
And this seems to fix the problem... Maybe someone can explain why it works? Or if it really solves the problem, or maybe introduces other problems?
Further edit: In my fncychap.sty
, I found this:
%%% Begin: To avoid problem with scrbook.cls (fncychap version 1.32)
%%OUT:
%\def\@schapter#1{\if@twocolumn
% \@topnewpage[\@makeschapterhead{#1}]%
% \else
% \@makeschapterhead{#1}%
% \@afterheading
% \fi}
%%IN:
\def\@schapter#1{%
\if@twocolumn%
\@makeschapterhead{#1}%
\else%
\@makeschapterhead{#1}%
\@afterheading%
\fi}
%%% End: To avoid problem with scrbook.cls (fncychap version 1.32)
If I just remove that, it also seems to fix my problem?
I wonder though, what is it what fncychap
tried to fix here? I.e. what was meant by "avoid problem with scrbook.cls"?
As a side node, daleif argues in the comments that you better should avoid fncychap
and scrbook
if you can. fncychap
was last updated in 2007.
fncychap
, it should compile now. Note that I want to have the glossaries in it, and also a table in it, because this combination is important. I don't want to fix the list of tables link but then introduce other problems in other parts.\usepackage[Bjarne]{fncychap}
the hypertargets seems to work correctly when it is not loaded. When loaded things go wrong. Plus it has not been updated in 13 years.glossaries
beforehyperref
(onlycleverref
needs to go afterhyperref
)