9

I'm doing my report for my last internship and I've been trying to add hyperref to my LaTeX pdf. I would like to have a link in my toc for my introduction.

Without hyperref, it works fine :

\chapter*{Introduction}
\addtocontents{toc}{\contentsline{chapter}{\numberline{}Introduction}{}}

But, when I use hyperref, I have this :

! Argument of \contentsline has an extra }.
<inserted text>
\par
l.3 ...line{chapter}{\numberline{}Introduction}{}}
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.
Runaway argument?
! Paragraph ended before \contentsline was complete.
<to be read again>
\par
l.3 ...line{chapter}{\numberline{}Introduction}{}}
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
)

What went wrong here ?

Thanks

cgnieder
  • 66,645
Piotr
  • 93
  • Try \addcontentsline{toc}{chapter}{Introduction} – cgnieder Aug 04 '14 at 12:51
  • \addtocontents is mostly a source of errors ;-) –  Aug 04 '14 at 12:53
  • Ignore my second comment if you really don't want a page number in the toc. But then why add an entry at all? – cgnieder Aug 04 '14 at 12:56
  • hyperref redefines \contentsline to use 4 args instead of 3. I think the usual entry is @currentHref (don't forget \makeatletter). – John Kormylo Aug 04 '14 at 13:07
  • @Piotr: You don't want to remove the page numbers? –  Aug 04 '14 at 13:09
  • 1
    I want to have a ref in my toc, without a page number. – Piotr Aug 04 '14 at 13:18
  • @Piotr I don't understand why. What use is an entry in the table of contents when a reader cannot see the page number they must scroll to when they want to read the corresponding section? – cgnieder Aug 04 '14 at 13:51
  • 1
    @clemens I think the use is that the reader can go to ToC and easily find the chapter by name, then click it to get there, but it does not clutter up the page numbers of the actual document. For example you might want to add a thanks chapter, but since it is not content, you might not want to number its page and subsequently not add it to ToC with page number in the entry. – Zelphir Kaltstahl Jul 05 '17 at 11:43

2 Answers2

12

hyperref redefined \contentsline to take four arguments instead of the usual three. Additionally, since you're working with ToC-related stuff (which are written to file), you need to be careful about expansion. Use it in the following way:

\documentclass{report}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\chapter*{Introduction}
\addtocontents{toc}{\protect\contentsline{chapter}{\protect\numberline{}Introduction}{}{}}
\end{document}

The fourth argument supplies the hyperref link (or anchor). Since it is empty, the ToC-entry won't be hyperlinked.


Adding a hyperlink depends on the type of link. In your case, you can use the following:

\documentclass{report}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\clearpage\phantomsection
\chapter*{Introduction}
\addtocontents{toc}{\protect\contentsline{chapter}{\protect\numberline{}Introduction}{}{chapter*.\thepage}}
\end{document}

Each \chapter* can be accessed using chapter*.<current page>, which is passed to the last option of \contentsline. Since the \phantomsection/mark is made on the current page, chapter*.\thepage results in an appropriate expansion of the current \chapter*.

Werner
  • 603,163
0

I've found an other solution using the package tocloft :

\chapter*{Introduction}
\phantomsection
\addtocontents{toc}{\cftpagenumbersoff{chapter}} 
\addcontentsline{toc}{chapter}{Introduction}
\addtocontents{toc}{\cftpagenumberson{chapter}} 
Piotr
  • 93