Document Classeshyperref problem

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
niverson
Posts: 4
Joined: Thu Jul 22, 2010 3:07 am

hyperref problem

Post by niverson »

I created and have been maintaining the document class below for years:

http://www-math.bgsu.edu/~inathan/bgsu_latex_class.html

Apparently the person in charge of making sure the manuscripts look okay started deleting all the beautiful pdfbookmarks because she wanted the chapter bookmarks to link to the top of the page instead of the chapter heading.

I have searched far and wide and continue to dive into hyperref's source for clues. If you think you can help please do.

One idea I had was to link to the built-in unique page anchors (pageanchor=true option). Unfortunately, i can't seem to find a command to create a bookmark to an existing anchor.
Last edited by niverson on Mon Jul 26, 2010 5:44 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

niverson
Posts: 4
Joined: Thu Jul 22, 2010 3:07 am

hyperref problem

Post by niverson »

I have found a partial solution with the bookmark package part of oberdiek's collection:

http://www.ctan.org/tex-archive/macros/ ... /oberdiek/

Now for sections that do not appear in the toc I can add a pdf bookmark using the bookmark command as follows:

Code: Select all

\newenvironment{abstract}{\clearpage
\bookmark[page=\value{page},view={FitH 800},level=0]{ABSTRACT}
\label{abstract}
\thispagestyle{myheadings}
\begin{center}\textbf{ABSTRACT}\\ \end{center}
  \begin{flushleft}{\@advisor , Advisor\\} \end{flushleft}

}{\clearpage
}
However I would still like to change the behavoir of the hyperref automatically generated bookmarks. Here's a minimal working example.

Code: Select all

\documentclass{book}
\usepackage{hyperref}

\begin{document}
\chapter{Hi}
\section{less}
\section{filling}
\section{tastes}
\section{great}
\chapter{Luke I am your father}
\section{star wars}
\appendix
\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{APPENDIX}
\chapter*{APPENDIX}
\end{document}
Note that the pdf bookmarks for chapters are at the headings (not the top of the page as requested). I do still want sections and lower level bookmarks to be linked to the section of text.
niverson
Posts: 4
Joined: Thu Jul 22, 2010 3:07 am

hyperref problem

Post by niverson »

Many thanks to Heiko Oberdiek for kindly answering my emails on this matter. Here's the solution I managed. Since the dissertation standards of my university have very limited front matter I don't need to worry about chapters and sections before the \mainmatter but the page numbers will be offset. So I redefined the \mainmatter command to find this fixed offset. I also disabled hyperref's automatic bookmark renedering by

Code: Select all

\hypersetup{bookmarkstype=none}
Below is my mucking around with the internals of chapter / section to make pdf bookmarks appear in the desired way. Note that I'm not changing very much of the original code. In this post I've added comments to show you exactly my contribution to the code.

Code: Select all

% Need to define offset to get links right

\renewcommand\mainmatter{%
% My Contribution here
  \newcounter{offset}\setcounter{offset}{\value{page}}
  \newcounter{trupage}
% End of my contribution
    \cleardoublepage
  \@mainmattertrue
  \pagenumbering{arabic}
  }

% This is to ensure that Chapter appears before the chapter number in
% the toc and fixes pdfbookmarks to point higher on the page 
\renewcommand{\chaptername}{CHAPTER}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
            \addcontentsline{toc}{chapter}{\chaptername\space\protect\numberline{\thechapter:}#1}%
% My contribution here
            \setcounter{trupage}{\value{page}+\value{offset}}
            \bookmark[page=\value{trupage},view={FitH 800},level=0]{\chaptername\space\thechapter: #1}
% End of my contribution
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
% Adds pdfbookmarks for sections
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
% My contribution here
  \pdfbookmark[#2]{{\csname the#1\endcsname}: #7}{#7}
% End of my contribution
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
I also made a tool for any people who'd like to add pdfbookmarked toc entries outside of the section / chapter commands.

Code: Select all

% Below adds pdf bookmarks and table of contents entries (manually)    
\newcommand{\addtocbookmark}[2]%
{
\ifthenelse{\equal{#1}{part}}
	{       \addcontentsline{toc}{#1}{#2}%
        	\setcounter{trupage}{\value{page}+\value{offset}}
        	\bookmark[page=\value{trupage},view={FitH 800},level=0]{#2}
        	}
{
\ifthenelse{\equal{#1}{chapter}}
	{       \addcontentsline{toc}{#1}{#2}%
        	\setcounter{trupage}{\value{page}+\value{offset}}
        	\bookmark[page=\value{trupage},view={FitH 800},level=0]{#2}
        	}
{
\ifthenelse{\equal{#1}{section}}
	{       \addcontentsline{toc}{#1}{#2}%
            \pdfbookmark[1]{#2}{#2}}
{
\ifthenelse{\equal{#1}{subsection}}
    {       \addcontentsline{toc}{#1}{#2}%
            \pdfbookmark[2]{#2}{#2}}
{
\ifthenelse{\equal{#1}{subsubsection}}
	{       \addcontentsline{toc}{#1}{#2}%
            \pdfbookmark[3]{#2}{#2}}
{
\ifthenelse{\equal{#1}{paragraph}}
	{       \addcontentsline{toc}{#1}{#2}%
            \pdfbookmark[4]{#2}{#2}}
{
\ifthenelse{\equal{#1}{subparagraph}}
	{       \addcontentsline{toc}{#1}{#2}%
            \pdfbookmark[5]{#2}{#2}}
{
}}}}}}}}
Heiko Oberdiek suggested a different method to solve the page offset problem. His method will be far more robust than mine as it will allow chapters / sections in the beginning of the document. Here's what he suggested below, I haven't tried implementing it yet.

Code: Select all

\usepackage{zref-abspage}
\newcounter{pglab}

\makeatletter

\begingroup
 \edef\pglab{\pglab\the\value{pglab}}%
 \stepcounter{pglab}%
 \zref@labelbyprops{\pglab}{abspage}%
 \zref@refused{\pglab}%
 \bookmark[page=\zref@extractdefault{\pglab}{abspage}{1}, ...]
 ...
\endgroup

\makeatother
Tracey_R
Posts: 1
Joined: Thu May 02, 2013 6:22 pm

hyperref problem

Post by Tracey_R »

This is can be solved much more simply with the "pdfview" option:

Code: Select all

\usepackage[pdfview=Fit]{hyperref}
This will open all pdfbookmarks (including tocLoft entries) at the top of the page, instead of directly at the beginning of content.

Best,
Tracey
Post Reply