Friday, July 6, 2012

hyperref, ToCs and incorrect page numbers and bookmarks

I was having trouble with page numbers in my table of contents and hyperref.Basically the page numbers would occasionally be the page before, or the section before, and the link in the ToC or in the document layout would take you to the wrong part of the document.

The culprit was probably most likely the lines I had in there that pushed sections and subsections onto right-hand (odd) pages.
\newcommand*\stdsection{}
\let\stdsection\section
\renewcommand*\section{%
\clearpage\ifodd\value{page}\else\mbox{}\clearpage\fi
\stdsection}
If I add in a \phantomsection prior to the final \stdsection, problem (mostly!) solved:
\newcommand*\stdsection{}
\let\stdsection\section
\renewcommand*\section{%
\clearpage\ifodd\value{page}\else\mbox{}\clearpage\fi
\phantomsection\stdsection}
I still can't get it to put my Glossary entry on the right page...

But finally, success ! Putting a \cleardoublepage between my \listoftables and my glossary \printglossaries for some reason fixed it... a \null\newpage or a \newpage, or \clearpage didn't work (despite putting it on the same page as a \cleardoublepage).

To add a bookmark to the location of the ToC but without actually adding a reference in the ToC,
\cleardoublepage \hypertarget{tocpage}{} \tableofcontents    \bookmark[dest=tocpage,level=1]{Table of Contents}
The cleardoublepage stopped the hypertarget from referring to the previous section, but actually does nothing in my document. the level of the bookmark defines the level in the bookmarks - I wanted it to be the same level as the sections.

EDIT: this also works if you have an unnumbered section that you have added manually to your ToC, ala:
\subsection*{title}
\addcontentsline{toc}{subsection}{title}
this corrects the problem with hyperref not hyperlinking to the right page:
\phantomsection \subsection*{title}
\addcontentsline{toc}{subsection}{title}

No comments:

Post a Comment