# Hyperref chapter in twocolumn mode (without using multicols)

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.

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}{%
\outline[fit=fitv]{1}[nonoch\theunnumberedchapter]{#3}%
}{%
\outline[fit=fitv]{1}[nonoch\theunnumberedchapter]{#3}%
}%
}{%
\IfValueTF{#2}{%
\outline[fit=fitv]{1}[ch\thechapter]{#3}%
}{%
\outline[fit=fitv]{1}[ch\thechapter]{#3}%
}%
}%
}

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

• (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 4 '15 at 3: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 4 '15 at 3:58
• @Hugh Please see edited answer. This is not very sophisticated but it is a bit more so than my earlier version! – cfr Mar 4 '15 at 12:35
• @Hugh Is this working for you? Or have you found a different solution? – cfr Mar 8 '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 9 '15 at 11:26

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}{%
\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
{\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}{%
\chapappifchapterprefix{\nobreakspace}\thechapter\autodot\enskip}%
}
\makeatletter
{\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}

• 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 7:09
• @Hugh: Update your distribution and try again. – Werner Feb 28 '15 at 7:11
• 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 1 '15 at 3:12
• If it helps, the indent looks to be about an en space and it does not occur with a \chapter*. – Hugh Mar 1 '15 at 3:21
• \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 1 '15 at 17:10