[Tex/LaTex] Table of Contents links to wrong pages + Wrong header name

bookmarkshyperrefpdftextable of contents

At the risk of being categorised as duplicates to this and that, which I found hard to follow because of the clutter specific to the original poster and because I am rather new to the language, I would ask a similar question.

I am using a template downloaded from my university website to write a thesis. Here is the (abridged) malfunctioning code:

\documentclass[12pt]{book}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Additional packages  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[bookmarks]{hyperref}
\usepackage{a4wide}
\usepackage{fancyhdr}
\usepackage{graphicx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Definition of the Header %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\pagestyle{fancyplain}
\renewcommand{\chaptermark}[1]%
{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]%
{\markright{\thesection\ #1}}
\lhead[\fancyplain{}{\bfseries\thepage}]%
{\fancyplain{}{\bfseries\rightmark}}
\rhead[\fancyplain{}{\bfseries\leftmark}]%
{\fancyplain{}{\bfseries\thepage}}
\cfoot{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Beginning of Dokument  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}
    \frontmatter

    \tableofcontents
    \markboth{Contents}{Contents}

    \listoffigures
    \markboth{List of Figures}{List of Figures}

    \cleardoublepage

    \markboth{Abstract}{Abstract}
    \addcontentsline{toc}{chapter}{\protect Abstract}
    \chapter*{Abstract}
    Here is a maximum one-page summary of Dissertation.

    \cleardoublepage

    \addcontentsline{toc}{chapter}{\protect Acknowledgement}
    \chapter*{Acknowledgement}
    Thank you.


    \mainmatter\setcounter{page}{1}
    \chapter{First Chapter}
    Shakespeare: What is in a name? That which you call a chick, by any other name would smell as sweet.

\end{document}

The problem you see is two-fold. One, the table of contents does not seem to link properly to Abstract and Acknowledgement. Secondly, observe (in the compiled PDF) the header of the blank page (viii) after the Acknowledgement. It still says Abstract.

I tried inserting a \phantomsection in various sections but that does not seem to remedy the problem. Kindly help.

Thank you very much.

Best Answer

You have to use \phantomsection before \addcontentsline:

\documentclass[12pt]{book}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Additional packages  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{a4wide}% really old package, I would suggest to use geometry
\setlength{\headheight}{14.5pt}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage[bookmarks]{hyperref}% <- moved to load as last package
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Definition of the Header %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\pagestyle{fancyplain}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhead[LE,RO]{\fancyplain{}{\bfseries\thepage}}%<- changed
\fancyhead[RE]{\fancyplain{}{\bfseries\nouppercase{\leftmark}}}% <- changed
\fancyhead[RO]{\fancyplain{}{\bfseries\nouppercase{\rightmark}}}% <- changed
\fancyfoot{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Beginning of Dokument  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\frontmatter
\tableofcontents
\listoffigures

\cleardoublepage
\phantomsection% <- added
\addcontentsline{toc}{chapter}{Abstract}
\markboth{Abstract}{Abstract}
\chapter*{Abstract}
Here is a maximum one-page summary of Dissertation.

\cleardoublepage
\phantomsection% <- added
\addcontentsline{toc}{chapter}{Acknowledgement}
\markboth{Acknowledgement}{Acknowledgement}%<- added
\chapter*{Acknowledgement}
Thank you.

\mainmatter% resets the page number automatically
\chapter{First Chapter}
\end{document}