[Tex/LaTex] Certain hyperlinks (memoir+hyperref) placed too low

hyperreflinkspositioning

I tend to use the memoir class together with the hyperref package, in order to have my table of contents, the lists of tables and figures, and the bibliography be hyperlinked. While all links to chapters, sections, and the like link to the correct positions in the document, the links to the table of contents, the lists of tables and figures, and the bibliography appear too low, approximately where the first item in these respective listings would be placed, but not just to the above-left of these lists' headings. Here is a minimal working example:

\documentclass{memoir}
  % using the memoir class here is necessary
  % for all the desired hyperlinks to be created
\usepackage{hyperref}

\begin{document}

\tableofcontents

\section{A section}
This is the only section.

\listoftables
\listoffigures

\begin{thebibliography}{9}
\end{thebibliography}

\end{document}

(It might be necessary to enlarge the display in your pdf-reader to notice this effect.)

Is it possible to correct the hyperlink targets so that they point directly to the respective headings (here, "Contents", "List of Tables", "List of Figures", and "Bibliography")? (Note: I am aware of the hypcap package but not sure whether/how it can be used to address this.)

Best Answer

The following patch uses etoolbox. Add it to your document preamble after loading hyperref, and it will correct the hyper-links:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
% Patch 'Table of Contents'
\patchcmd{\mem@tableofcontents}% <cmd>
  {\phantomsection}% <search>
  {\relax}% <replace>
  {}{\@latex@error{Could not patch \string\mem@tableofcontents}}% <success><failure>
% Patch 'List of Tables'
\patchcmd{\mem@listoftables}% <cmd>
  {\phantomsection}% <search>
  {\relax}% <replace>
  {}{\@latex@error{Could not patch \string\mem@listoftables}}% <success><failure>
% Patch 'List of Figures'
\patchcmd{\mem@listoffigures}% <cmd>
  {\phantomsection}% <search>
  {\relax}% <replace>
  {}{\@latex@error{Could not patch \string\mem@listoffigures}}% <success><failure>
% Patch 'Bibliography'
\patchcmd{\@memb@bchap}% <cmd>
  {\phantomsection}% <search>
  {\relax}% <replace>
  {}{\@latex@error{Could not patch \string\@memb@bchap}}% <success><failure>
\renewcommand{\printtoctitle}[1]{%
  \phantomsection\printchaptertitle{#1}}
\let\printlottitle\printtoctitle
\let\printloftitle\printtoctitle
\makeatother

The reason behind the faulty linking stems from the placement of \phantomsection inside the patched macros. It is placed after the respective headings have been created, causing the "low hyper target."

The thebibliography environment sets a \chapter*, which I've left as is in terms of its hyper-linking. As such, it inserts \phantomsection before setting the chapter space and header. However, as this is the default for all chapters, it seemed suitable.