0

I'm using hyperref package so that \tableofcontents has clickable links.

Is there a way to force one hyperlink in \tableofcontents to refer to a given page instead of the automatically selected page?

Say you have a chapter beginning on page 2 of your PDF output but you want table of contents to create a corresponding clickable link that displays page 3 instead.

(I am looking for a general answer here, hence the absence of code/header)

  • 2
    Yes, this is possible, but it's confusing, isn't it? – user31729 Aug 3 '15 at 17:56
  • Louis, I fear, I will provide some work and you will delete the question after some minutes, as you have done with the other ones. ;-) – user31729 Aug 3 '15 at 18:00
  • 1
    Although your question is general, you should give a rule what to do actually. What is the rule for another chapter? Is it the second page of the chapter too then? – user31729 Aug 3 '15 at 18:01
  • 3
    The hyperlinks in the TOC are there to go to the start of the respective part of the text. I don't see why you'd want them to point elsewhere (except for making fun of your readers...). If you want some Table of Whatever pointing at other parts of the text, there mostly are ways to point at e.g. figures, tables in the respective table. – vonbrand Aug 3 '15 at 18:07
  • 1
    Fixing wrong anchors is usually much easier than fixing wrong link targets. A \phantomsection at the right place or using \refstepcounter instead \stepcounter might already solve the issue. – Heiko Oberdiek Aug 3 '15 at 19:03
2

Perhaps I've misunderstood the question anyway completely:

There should be a hyperlink looking like a chapter entry but not to the start page of the chapter, pointing to somewhere else.

I've done a \nottocchapter which behaves like an usual numbered chapter, but kicked out \addcontentsline there.

Then I defined \lookslikeachapterentrybutpointstosomewhereelse (that name is a pain for users misusing the appropiate thing ;-)) which adds the relevant chapter entry later on, with the page number where it appears.

Do I recommend this all? No ;-)

\documentclass{book}

\usepackage{blindtext}
\usepackage{xparse}


\makeatletter

\let\latex@chapter\chapter

\def\currentchaptername{}

\NewDocumentCommand{\notocchapter}{om}{%
  \IfValueTF{#1}{%
    \def\currentchaptername{#1}
  }{%
    \def\currentchaptername{#2}
  }%
  \begingroup
  \renewcommand{\addcontentsline}[3]{}% Do nothing for this chapter
  \IfValueTF{#1}{%
    \latex@chapter[#1]{#2}
  }{%
    \latex@chapter{#2}
  }%
  \endgroup
}

\NewDocumentCommand{\lookslikeachapterentrybutpointstosomewhereelse}{o}{%
  \phantomsection
  \IfValueTF{#1}{%
    \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
  }{%
    \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}\currentchaptername}
  }%
}

\makeatother

\usepackage[linktocpage]{hyperref}


\begin{document}

\tableofcontents

\notocchapter{Some chapter}

\blindtext[20] 

\lookslikeachapterentrybutpointstosomewhereelse

\chapter{Some other chapter}

\notocchapter{Another chapter}

\blindtext[40] 

\lookslikeachapterentrybutpointstosomewhereelse[And now for something completely different]


\end{document}
  • I've omitted a screenshot – user31729 Aug 3 '15 at 19:16
  • @Mico: ;-) You know me -- About one quarter of my answers contain that line :-P – user31729 Aug 3 '15 at 19:22
  • I thought it's particularly appropriate in this context. :-) – Mico Aug 3 '15 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.