Table of Contents – Hyperref in TOC Without Page Number

hyperrefpage-numberingtable of contents

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

Best Answer

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

Related Question