2

There are quite a bit of related questions:

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 or notlot 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.

Albert
  • 2,707
  • 1
    Please reduce the example to only those pieces that are actually needed. – daleif Jul 07 '20 at 13:42
  • @daleif I reduced it further. Also I added a fix for some Tex Live versions and 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. – Albert Jul 07 '20 at 14:00
  • 1
    The general idea is that you can trim things down to the minimal example that exhibits the problem (no bibliography, no glossary). Then you can figure out why your TOC isn't linking to particular tables or figures. Then you can put the fix into your actual project, and see if there is some new complication. But there usually isn't. (And if fncychap is contributing to the problem, that's only one extra line in your minimal example.) – Teepeemm Jul 07 '20 at 14:16
  • You should probably drop \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. – daleif Jul 07 '20 at 14:27
  • Additionally load glossaries before hyperref (only cleverref needs to go after hyperref) – daleif Jul 07 '20 at 14:28
  • Ok, I cleaned it up, but I'm a bit afraid that the solution now might be invalid with the other things I removed (glossaries, bibliography, tables, figures, links, ...). – Albert Jul 07 '20 at 14:30
  • @daleif I must use that package fncychap, because this is the official PhD template, and I must not change it. However, I think I am allowed to patch any problems in that package. – Albert Jul 07 '20 at 14:31
  • Then you probably have to talk to the people who says you have to use such an archaic template using components that has not been updated in years. – daleif Jul 07 '20 at 15:22
  • @daleif But it should be possible to fix fncychap, right? I mean, I could also rephrase my question to "how to fix fncychap". Or just copy the relevant fncychap over directly to this question, and then not have that package anymore. – Albert Jul 07 '20 at 15:30
  • It cannot be your responsibility to patch a broken PhD template, it should be reported to your organisation that there are issues to be addressed with the template. Why are they mixing koma with fncychap in the first place. This does not seem to be a trustworthy template to begin with – daleif Jul 07 '20 at 16:01
  • I'd like to see the requirement that you must use fncychap and scrbook. At my department we have no such rules, only requirements on the contents – daleif Jul 07 '20 at 16:03
  • @daleif Instead of discussing with my university and/or supervisor about whether I can change it, and explaining them about the problems, maybe it is easier to just fix the problem? Also, even if I could avoid fncychap or scrbook, I still would be interested how to fix the problem, or understand the problem. Actually I already posted one possible solution. Maybe that's it? – Albert Jul 07 '20 at 16:24
  • Then think about the next student. Why should they have to go through the same thing – daleif Jul 07 '20 at 16:26
  • @daleif I still am interested in the basic problem itself, no matter the university politics. And maybe other people also get to the situation that they have to use fncychap and scrbook. I don't think it is helpful now to say that these better just should not be used. (Unless you maybe have a very simple way to replace them by sth else which will produce exactly the same output.) Or I mean, fair point, I will keep that in mind, and this is probably also good to know for future readers. However, I still would like to know how to solve it. Or it is not valid to ask this question? – Albert Jul 07 '20 at 17:04

2 Answers2

1

scrbook contains patches that it executes after hyperref has been loaded. In this patch it reverts the definition of the chapter commands and so overwrites the patches from hyperref. scrbook obviously doesn't expect that the chapter commands have be changed by some other package.

You can try this:

\documentclass[listof=totoc,
enabledeprecatedfontcommands]{scrbook}
%disable scrbook patch:
\makeatletter
\renewcommand\scr@chapter@after@hyperref@patch{}
\makeatother
\usepackage[Bjarne]{fncychap}

\usepackage[% hypertexnames=false,% bookmarks=true,% %%% generate bookmarks ... bookmarksnumbered=true,% %%% ... with numbers ]{hyperref}

\title{Thesis}

\begin{document} \frontmatter \tableofcontents

\mainmatter

\chapter{Introduction} \chapter{Conclusion}

\backmatter

\listoffigures

\listoftables \end{document}

Ulrike Fischer
  • 327,261
0

Download the latest fncychap package here, and copy the fncychap.sty into the directory of your Latex code.

Open the file. You will find some code like 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)

Remove this code, or comment it out.

This seems to solve the problem.

scrbook has its own definition of \@schapter and this breaks it.

Albert
  • 2,707