3

I'm using the scrreprt class in twocolumn mode with hyperref:

\documentclass[a4paper]{scrreprt}
\usepackage[twocolumn]{geometry}
\usepackage{hyperref}


\begin{document}
\tableofcontents
\chapter{This is a chapter}

\chapter{Chapter TWOOO}

\end{document}

My problem is that the links in the table of contents do not point to the chapter heading, a common bug as in multiple questions, particularly Hyperref chapter link anchors in two column mode. The accepted answer there uses multicols in each chapter, which is functional but requires the twocolumn mode to be implemented via a multicol environment.

This is unacceptable to me as it seems to violate separation of form and content and can't be supplied to a .cls. Is there a way to have the hyperref links to chapters link to the heading? I would be happy with the chapter linking to the chapter heading's page if not the exact position of the chapter heading.

Hugh
  • 2,374
  • 17
  • 30

2 Answers2

3

This answer defines a new command \Chapter which links entries in the contents with chapter pages using navigator rather than hyperref.

Used in overly creative ways, this will certainly break things. Used in fairly standard ways, however, I think it may be helpful.

Caveat emptor...

By default, loading navigator has no effect on your document (as far as I can tell). However, the macro \Chapter demonstrates that it is possible, at least in theory, to use the package to set up links from the contents to the page on which the chapter begins in such a way that the chapter title is shown. Even though the anchor is placed after the chapter heading, it is configured so that the link shows the top part of the page i.e. content which is before the anchor in the source.

Syntax

\Chapter[short title]{title}
\Chapter*[short title]{title}
  • \Chapter[]{} feeds its arguments to KOMA's \chapter[]{} command and creates an anchor for the link from the contents.
  • \Chapter*[]{} feeds its arguments to KOMA's \addchap[]{} command and creates an anchor for the link from the contents. It uses a new counter to ensure unique names for the anchors created in this way.

There is no equivalent of KOMA's \addchap*{} because the star in \Chapter*[]{} is being used to determine whether to use \chapter[]{} or \addchap[]{} in the first place. If you wanted to mirror KOMA's commands more exactly, you could create another command (e.g. \AddChap*[]{}) instead. However, it seems from your comments that you are using \chapter*{} and so I've stuck with a version of that.

SCLFME (Somewhat Crude Likely Fragile Minimal Example):

\documentclass[a4paper,twocolumn]{scrreprt}
\usepackage{navigator,xparse,kantlipsum}
\newcounter{unnumberedchapter}
\setcounter{unnumberedchapter}{0}
\NewDocumentCommand \Chapter { s o m }{%
  \IfBooleanTF{#1}{%
    \stepcounter{unnumberedchapter}%
    \IfValueTF{#2}{%
      \addchap[\protect\jumplink{nonoch\theunnumberedchapter}{#2}]{#3}%
      \outline[fit=fitv]{1}[nonoch\theunnumberedchapter]{#3}%
    }{%
      \addchap[\protect\jumplink{nonoch\theunnumberedchapter}{#3}]{#3}%
      \outline[fit=fitv]{1}[nonoch\theunnumberedchapter]{#3}%
    }%
  }{%
    \IfValueTF{#2}{%
      \chapter[\protect\jumplink{ch\thechapter}{#2}]{#3}%
      \outline[fit=fitv]{1}[ch\thechapter]{#3}%
    }{%
      \chapter[\protect\jumplink{ch\thechapter}{#3}]{#3}%
      \outline[fit=fitv]{1}[ch\thechapter]{#3}%
    }%
  }%
}
\pagestyle{headings}

\begin{document}
  \tableofcontents

  \Chapter*{Introduction}
  \kant[1-4]

  \Chapter*[Another]{Another Introduction}
  \kant[5-7]

  \Chapter[Shorter]{This is a chapter}
  \kant[8-12]

  \Chapter{Chapter TWOOO}
  \kant[13-17]

  \Chapter*{Epilogue}
  \kant[18-20]

\end{document}

Note that if you want a chapter which does not appear in the contents, you can, of course, simply use \chapter*{} in the usual way.

cfr
  • 198,882
  • (Ignore previous comment.) The only problem I foresee is if I want a starred chapter to appear in the table of contents. Right now I'm using \chapter*{Intro}\addcontentsline{toc}{chapter}{Intro}. Would I have to manually edit the table of contents for this to happen? I tried \addcontentsline{toc}{Chapter}{Intro} (capital C) but that produced an error. – Hugh Mar 04 '15 at 03:53
  • @Hugh That is doable but it will have to be later. It is already too late (early) here and my brain can't figure out anything. I'll try to polish it a bit when it (brain) has had some sleep ;). – cfr Mar 04 '15 at 03:58
  • @Hugh Please see edited answer. This is not very sophisticated but it is a bit more so than my earlier version! – cfr Mar 04 '15 at 12:35
  • @Hugh Is this working for you? Or have you found a different solution? – cfr Mar 08 '15 at 14:13
  • This is working, but I think the alternative solution works better (and answers the question I originally asked). What made you choose navigator over hyperref? – Hugh Mar 09 '15 at 11:26
  • Accepted to give 2nd place credit. :) – Hugh Mar 09 '15 at 11:28
  • @Hugh You don't need to give 2nd place credit! I really just tried navigator because I knew it handles the links differently from hyperref and I thought it might happen to work better. I tried it and it did, so I posted the solution. But I figured that switching to navigator might well create other complications in part because it does not do as much out-of-the-box for you automatically in terms of creating links, whereas hyperref does a lot either by default or with a few package options. So I was curious whether it worked in a larger document. (I've never used navigator before.) – cfr Mar 09 '15 at 12:50
1

The following solution inserts a new hyper target (an internal document link) as part of the chapter format setting.

The first step is to insert the hyper target:

% Add extra hyper target for chapter: chapter..\thechapter
\renewcommand*{\chapterformat}{%
  \mbox{\raisebox{25pt}[0pt][0pt]{\hypertarget{chapter..\thechapter}{}}% Add 
    \chapappifchapterprefix{\nobreakspace}\thechapter\autodot\enskip}%
}

Above we insert a target named chapter..\thechapter, raised by 25pt (an appropriate height, without affecting any vertical positioning). This target is synonymous with the traditional target chapter.\thechapter.

The second step is to adjust the hyper link to point to this new hyper target only when you're calling \chapter. For this we patch \addcontentsline:

\usepackage{etoolbox}
\makeatletter
% Update \addcontentsline to jump to new hyper target _only_ if \chapter is used
\patchcmd{\addcontentsline}% <cmd>
  {\Hy@writebookmark}% <search>
  {\ifnum\pdfstrcmp{chapter}{#2}=0 % Chapter mark
     \edef\@currentHref{chapter..\thechapter}%
   \fi
   \Hy@writebookmark}% <replace>
  {}{}% <success><failure>
\makeatother

The above patch inserts a conditional to check whether you've used \chapter or not (via a \pdfstrcmp string comparison - requires e-TeX). If you are using \chapter, then \@currentHref is updated to point to the new hyper target chapter..\thechapter. This is done just before the bookmark and ToC-related entry is placed.

Here is a complete usage and minimal example:

\documentclass[twocolumn]{scrreprt}

\usepackage{hyperref,lipsum,etoolbox}

% Add extra hyper target for chapter: chapter..\thechapter
\renewcommand*{\chapterformat}{%
  \mbox{\raisebox{25pt}[0pt][0pt]{\hypertarget{chapter..\thechapter}{}}% Add 
    \chapappifchapterprefix{\nobreakspace}\thechapter\autodot\enskip}%
}
\makeatletter
% Update \addcontentsline to jump to new hyper target _only_ if \chapter is used
\patchcmd{\addcontentsline}% <cmd>
  {\Hy@writebookmark}% <search>
  {\ifnum\pdfstrcmp{chapter}{#2}=0 % Chapter mark
     \edef\@currentHref{chapter..\thechapter}%
   \fi
   \Hy@writebookmark}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}

\tableofcontents

\chapter{First chapter}
\lipsum[1-10]

\chapter{Second chapter}
\lipsum[11-20]

\end{document}
Werner
  • 603,163
  • I get the error Undefined control sequence \scr@chapter@sectionafterskip+35pt\relax with the line break just before +35pt\relax – Hugh Feb 28 '15 at 07:09
  • 2
  • Thanks. This fixes the problem but raises another one: the first line of each chapter is indented. This goes away if I comment out the line with \@afterindentfalse but then the links break again. Any ideas? – Hugh Mar 01 '15 at 03:12
  • If it helps, the indent looks to be about an en space and it does not occur with a \chapter*. – Hugh Mar 01 '15 at 03:21
  • 1
    \patchcmd{\chapter}{\@afterindentfalse}{\@afterindentfalse\leavevmode}{}{} pretty much breaks all the latex after-heading code if there is a blank line after the chapter command. – David Carlisle Mar 01 '15 at 17:10
  • @Hugh: See my updated answer which should eliminate the problems. Hopefully... – Werner Mar 04 '15 at 06:47
  • @Werner what should I patch to solve the similar problem with \vref \cref and friends? – Hugh May 20 '16 at 10:16