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

8
  • Try \addcontentsline{toc}{chapter}{Introduction}
    – cgnieder
    Aug 4, 2014 at 12:51
  • \addtocontents is mostly a source of errors ;-)
    – user31729
    Aug 4, 2014 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 4, 2014 at 12:56
  • 1
    I want to have a ref in my toc, without a page number.
    – Piotr
    Aug 4, 2014 at 13:18
  • 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. Jul 5, 2017 at 11:43

2 Answers 2

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

2
  • How can I add an hyperlink for this custom entry ?
    – Piotr
    Aug 4, 2014 at 14:10
  • {\@currentHref} or {chapter.\thechapter} Aug 4, 2014 at 19:33
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}} 
1
  • 1
    doesn't work for me, gives the same error: Argument of \contentsline has an extra }. Apr 16, 2016 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .