14

I dont understand why my hyperref is not going to the page I wanted when I click it on my table of content?

\documentclass[12pt, a4paper, oneside]{report}
\usepackage[T1]{fontenc}
\usepackage{mathptmx} %times font
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{hyperref}
\usepackage{setspace}
\usepackage[english]{babel} %roman & arabic label page
\usepackage[top=50mm , bottom=50mm, left=45mm, right=45mm]{geometry}

\addto\captionsenglish{% Replace "english" with the language you use
\renewcommand{\contentsname}%
{Table of Contents}% change "Contents" (default) to "Table of Contents"
}

\hypersetup{
colorlinks=true, %set true if you want colored links
linktoc=all,     %set to all if you want both sections and subsections linked
linkcolor=black,  %choose some color if you want links to stand out
}

\pagenumbering{Roman}

\begin{document}
\input{CoverPage}
\thispagestyle{empty}
\pagebreak

\input{TitlePage}
\thispagestyle{empty}
\pagebreak

\input{CopyrightPage}
\addcontentsline{toc}{chapter}{Copyright}
\setcounter{page}{1}
\pagebreak

\input{DeclarationPage}
\addcontentsline{toc}{chapter}{Declaration}
\pagebreak

\input{AcknowledgementPage}
\addcontentsline{toc}{chapter}{Acknowledgement}
\pagebreak

\tableofcontents
\addcontentsline{toc}{chapter}{Table of Contents}

\pagebreak

\addcontentsline{toc}{chapter}{List of Figures}
\listoffigures
\pagebreak

% % (1) =========================================
\pagenumbering{arabic}
\addcontentsline{toc}{chapter}{1. Introduction}
\input{Introduction}
\setcounter{chapter}{1}
\pagebreak
  • 1
    Using this way, you have to use a \phantomsection before the \addcontentsline..., but there might be more issues to solve. \addcontentsline should be done before you use \input and I would not use \pagebreak there. Replace it with \cleardoublepage –  Aug 06 '15 at 07:27
  • @ChristianHupfer i just tried.. it's not working.. aikss – Michimcchicken Aug 06 '15 at 07:29
  • Yes, as I added: There is more work to do, probably. Your document is only a fragment, it's hard to help you. Do the files with \input have \chapter inside? –  Aug 06 '15 at 07:29
  • how about this \tableofcontents \phantomsection \addcontentsline{toc}{chapter}{Table of Contents} do i put \tableofcontents after the \addcontentsline.. as well?? – Michimcchicken Aug 06 '15 at 07:31
  • Yes, try that, but there's even a better way. I'll try a solution. –  Aug 06 '15 at 07:32
  • the files inside the \input has sections and subsections.. – Michimcchicken Aug 06 '15 at 07:32
  • it's working!! thanksss but the rest of the page is working except the table of contents page.. it link to the 1st page.. – Michimcchicken Aug 06 '15 at 07:34
  • As I said, I'll try a solution... just be patient ;-) –  Aug 06 '15 at 07:34
  • opps haha sorry... – Michimcchicken Aug 06 '15 at 07:35
  • @ChristianHupfer ehh i just changed pagebreak to \cleardoublepage.. everything works!! thanks!! but can i ask why use cleardoubepage instead of page breaks? o.o – Michimcchicken Aug 06 '15 at 07:42
  • \hyperref needs clear page anchors which is 'guaranteed' if the page is cleared of all content and then a new page is shipout, in conjunction with a \phantomsection for \addcontentsline. –  Aug 06 '15 at 07:52

3 Answers3

21

There are multiple issues with the O.P. code...

  • Use \cleardoublepage\phantomsection\addcontentsline{...} if some content must be explicitly added to a ToC
  • For the Table of Contents and List of Figures \usepackage{tocbibind} is the cleaner solution
  • The Introduction should be real chapter and not something with \section and subsection. This screws up the bookmarking etc. and numbering.
  • The \input is too much for my taste -- It makes editing (and debugging) very difficult.

Some notes on \phantomsection etc.

If content is to be added to a .toc etc. file, \addcontentsline{toc}{...}{...} is most times the right choice. However, if this happens without a \cleardoublepage (or \clearpage) in conjunction with \phantomsection, the link will go to a previous page.

The \phantomsection can be omitted however, if a command like \chapter* etc. is used. An anchor related to chapter* is inserted by hyperref then. For more information, see section

3.2 Options for destination names

in the hyperref manual please.


\documentclass[12pt, a4paper, oneside]{report}
\usepackage[T1]{fontenc}
\usepackage{mathptmx} %times font
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{setspace}
\usepackage[english]{babel} %roman & arabic label page
\usepackage[top=50mm , bottom=50mm, left=45mm, right=45mm]{geometry}

\addto\captionsenglish{% Replace "english" with the language you use
\renewcommand{\contentsname}%
{Table of Contents}% change "Contents" (default) to "Table of Contents"
}

\title{Foo}
\author{Foo}

\usepackage{blindtext}
\usepackage{tocbibind}

\usepackage{hyperref}

\hypersetup{
colorlinks=true, %set true if you want colored links
linktoc=all,     %set to all if you want both sections and subsections linked
linkcolor=black,  %choose some color if you want links to stand out
}

\pagenumbering{Roman}

\begin{document}
\input{CoverPage}
\thispagestyle{empty}
\cleardoublepage
\input{TitlePage}
\thispagestyle{empty}
\cleardoublepage

\phantomsection
\addcontentsline{toc}{chapter}{Copyright}
\input{CopyrightPage}
\setcounter{page}{1}
\cleardoublepage

\phantomsection
\addcontentsline{toc}{chapter}{Declaration}
\input{DeclarationPage}
\cleardoublepage

\phantomsection
\addcontentsline{toc}{chapter}{Acknowledgement}
\input{AcknowledgementPage}
\cleardoublepage

\tableofcontents
\addcontentsline{toc}{chapter}{Table of Contents}
\cleardoublepage

\listoffigures
\addcontentsline{toc}{chapter}{List of Figures}
\cleardoublepage

% % (1) =========================================
\pagenumbering{arabic}
\phantomsection
\addcontentsline{toc}{chapter}{1. Introduction}
\input{Introduction}
\setcounter{chapter}{1}  % Why???



\end{document}

For the individual FooPage.tex files I used basically this code

\chapter*{Foo}  % To make the page header outstanding, just for this solution
\blindtext[2]

Replace Foo with the relevant name (Exception: TitlePage.tex is \maketitle only)

The individual \phantomsection commands aren't necessary if \chapter* etc is used, but I kept them, since it's unclear what's inside the real \input{...} files.

enter image description here

  • @touhami: I would say that depends on the files to be input, doesn't it? If I omit it, say before \input{CopyrightPage} and remove \chapter*{Copyright} in that file, it clearly goes wrong. The \addcontentsline will lead to wrong link to the cover page –  Aug 06 '15 at 08:26
  • you are right here. – touhami Aug 06 '15 at 08:29
  • @touhami: Yes, in this case ;-) But thanks for the editing suggest -- the O.P. edited in between and I think the error came in then... –  Aug 06 '15 at 08:30