General ⇒ table of contents and hyperref
-
- Posts: 7
- Joined: Fri Jan 04, 2019 6:41 pm
table of contents and hyperref
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
NEW: TikZ book now 40% off at Amazon.com for a short time.

- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
table of contents and hyperref
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
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
LaTeX.org admin
-
- Posts: 7
- Joined: Fri Jan 04, 2019 6:41 pm
table of contents and hyperref
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.
Code: Select all
\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.
- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
table of contents and hyperref
Hi Jonny,
now that I see the code, it's clear. The starred
Stefan
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:Code: Select all
\section*{Chapter 1}
\phantomsection
\addcontentsline{toc}{section}{Chapter 1}
...
\section*{Chapter 2}
\phantomsection
\addcontentsline{toc}{section}{Chapter 2}
LaTeX.org admin
-
- Posts: 7
- Joined: Fri Jan 04, 2019 6:41 pm
table of contents and hyperref
Many thanks, Stefan, this does the trick. I really appreciate your help with this. Jonny