LaTeX forum ⇒ Generaltable 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

table of contents and hyperref

Postby maxhikorski » Fri Jan 04, 2019 7:40 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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9345
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Fri Jan 04, 2019 8:02 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
Site admin

maxhikorski
Posts: 7
Joined: Fri Jan 04, 2019 6:41 pm

Postby maxhikorski » Sat Jan 05, 2019 5:28 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}
  18. \hypertarget{loretta}{\LARGE{Home Page}}
  19. \end{center}
  20.  
  21. \tableofcontents
  22.  
  23. \newpage
  24.  
  25. \addcontentsline{toc}{section}{Welcome}
  26. \section*{Welcome}
  27.  
  28. Text...
  29.  
  30. \hyperlink{loretta}{Home Page}
  31.  
  32. \newpage
  33.  
  34. \addcontentsline{toc}{section}{Chapter 1}
  35. \section*{Chapter 1}
  36.  
  37. Text...
  38.  
  39. \hyperlink{loretta}{Home Page}
  40.  
  41. \newpage
  42.  
  43. \addcontentsline{toc}{section}{Chapter 2}
  44. \section*{Chapter 2}
  45.  
  46. Text...
  47.  
  48. \hyperlink{loretta}{Home Page}
  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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9345
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat Jan 05, 2019 5:45 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
  3. \addcontentsline{toc}{section}{Chapter 1}
  4.  
  5. ...
  6.  
  7. \section*{Chapter 2}
  8. \phantomsection
  9. \addcontentsline{toc}{section}{Chapter 2}


Stefan
Site admin

maxhikorski
Posts: 7
Joined: Fri Jan 04, 2019 6:41 pm

Postby maxhikorski » Sun Jan 06, 2019 9:56 pm

Many thanks, Stefan, this does the trick. I really appreciate your help with this. Jonny


Return to “General”

Who is online

Users browsing this forum: No registered users and 12 guests