## LaTeX forum ⇒ General ⇒ table of contents and hyperref Topic is solved

LaTeX specific issues not fitting into one of the other forums of this category.
maxhikorski
Posts: 7
Joined: Fri Jan 04, 2019 6:41 pm

I have added the \hyperref package to my document. Everything okay except that in Contents (as generated by \tableofcontents), everything has been given a hyperlink that links to the wrong section; it links to the section BEFORE the one required. So 'Welcome' links to Contents, Chapter 1 links to 'Welcome' etc ... can anyone fix this? Many thanks, Jonny

Stefan Kottwitz
Posts: 9242
Joined: Mon Mar 10, 2008 9:44 pm
Hi Jonny,

welcome to the forum!

Since it's hard to guess what went wrong without seeing the code - can you post your code please? A reduced (compilable) example would be good, no need for actual text content.

Stefan

maxhikorski
Posts: 7
Joined: Fri Jan 04, 2019 6:41 pm
Okay, Stefan, this compiles for me all right and gives the problem I am talking about. Clicking in a link on the Contents page connects to the wrong chapter.

1. \documentclass[12pt]{article}
2. \usepackage[ansinew]{inputenc}
3. \usepackage{array}
4. \usepackage{color}
5. \usepackage{amsmath,amsthm}
6.
7. \usepackage{amsxtra}
8. \usepackage{amstext}
9. \usepackage{amssymb}
10. \usepackage{latexsym}
11. \usepackage{graphicx}
12. \usepackage{enumerate}
13. \usepackage{hyperref}
14.
15. \begin{document}
16.
17. \begin{center}
19. \end{center}
20.
21. \tableofcontents
22.
23. \newpage
24.
26. \section*{Welcome}
27.
28. Text...
29.
31.
32. \newpage
33.
35. \section*{Chapter 1}
36.
37. Text...
38.
40.
41. \newpage
42.
44. \section*{Chapter 2}
45.
46. Text...
47.
49.
50. \newpage
51.
52. \end{document}
Last edited by Stefan Kottwitz on Sat Jan 05, 2019 5:36 pm, edited 1 time in total.
Reason: code marked

Stefan Kottwitz
Posts: 9242
Joined: Mon Mar 10, 2008 9:44 pm
Hi Jonny,

now that I see the code, it's clear. The starred \section* commands don't produce an anchor for hyperref, since they are not intended to be shown in the table of contents, with a link. You added a contents entry manually. Then you also need to add a manual link. That can be done by \phantomsection. In this order:

1. \section*{Chapter 1}
2. \phantomsection
4.
5. ...
6.
7. \section*{Chapter 2}
8. \phantomsection

Stefan