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: 9345
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.

\documentclass[12pt]{article}\usepackage[ansinew]{inputenc}\usepackage{array}\usepackage{color}\usepackage{amsmath,amsthm} \usepackage{amsxtra}\usepackage{amstext}\usepackage{amssymb}\usepackage{latexsym}\usepackage{graphicx}\usepackage{enumerate}\usepackage{hyperref} \begin{document} \begin{center}\hypertarget{loretta}{\LARGE{Home Page}}\end{center} \tableofcontents \newpage \addcontentsline{toc}{section}{Welcome}\section*{Welcome} Text... \hyperlink{loretta}{Home Page} \newpage \addcontentsline{toc}{section}{Chapter 1}\section*{Chapter 1} Text... \hyperlink{loretta}{Home Page} \newpage \addcontentsline{toc}{section}{Chapter 2}\section*{Chapter 2} Text... \hyperlink{loretta}{Home Page} \newpage \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: 9345
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:

\section*{Chapter 1}\phantomsection\addcontentsline{toc}{section}{Chapter 1} ... \section*{Chapter 2}\phantomsection\addcontentsline{toc}{section}{Chapter 2}

Stefan