% \iffalse
%% File: hyperref.dtx Copyright (C) 1995, 1996, 1997
%%  Sebastian Rahtz 
%
%  This package is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%
% Direct use of code from this package in another package
% which is either sold for profit, or not made freely available,
% is explicitly forbidden by the authors.
%
%<package>\NeedsTeXFormat{LaTeX2e}%
%<package>\ProvidesPackage{hyperref}%
%<package>                [1997/08/21 5.3 Hypertext links for LaTeX]
%<*driver>
\documentclass{ltxdoc}%
\usepackage{array,times}%
\usepackage[hyperindex]{hyperref}%
\EnableCrossrefs
 \CodelineIndex
\begin{document}%
 \GetFileInfo{hyperref.sty}%
 \title{Hypertext marks in \LaTeX\thanks{This file
        has version number \fileversion, last
        revised \filedate.}}%
 \author{Sebastian Rahtz\\
Email: \texttt{s.rahtz@elsevier.co.uk}}%
 \date{\filedate}%
 \maketitle
 \tableofcontents
 \DocInput{hyperref.dtx}%
\end{document}%
%</driver>
% \fi
% \CheckSum{2607}%
%
% \MakeShortVerb{|}%
% \section{Introduction}%
% This package derives from and builds on the work of the
% Hyper\TeX\ project;
% see |http://xxx.lanl.gov/hypertex/|. It aims to extend the functionality
% of all the \LaTeX\ cross-referencing commands (including the
% table of contents) to produce |\special| commands;
% it provides general hypertext links, including those
% to external documents.
%
% The |\special| commands are defined in a configuration file.
% At present there are backends as follows:
% \begin{description}%
% \item[hypertex] dvi processors conforming to the Hyper\TeX\ guidelines
% (i.e. |xdvik| and |dvipsk|);
% \item[pdfmark] a generalized interface to Acrobat pdfmark commands,
%   (aliased to dvips for backward compatibility)   
% \item[dvips] basically pdfmark, but fixed for dvips
% \item[dvipsone] basically pdfmark, but fixed for dvipsone
% \item[pdf2ps] a special case of output suitable for processing by 
%             Ghostscript 
% \item[pdftex], for  Han The Thanh's \TeX{} variant
% \item[dvipdf] Sergey Lesenko's special dvi to PDF driver
% \item[dviwindo] Y\&Y's Windows previewer
% \end{description}%
%
% \section{Usage}%
% Use this package with any more or less normal \LaTeX\ document by specifying
% |\usepackage{hyperref}|. Make sure it comes \emph{last} of your loaded
% packages, to give it a fighting chance of not being over-written.
% Hopefully you will find that all cross-references work correctly 
% as hypertext.
%
% The low-level user macros which are available are as follows:
%
% \def\Mandatory#1{\{\emph{#1}\}}%
% \def\Optional[#1]{[#1]}%
% \begin{tabular}{>{\tt\char'134}ll}%
% hyperlink   &  \Mandatory{linkname} \Mandatory{text}\\
% hypertarget &\Mandatory{anchorname} \Mandatory{text}\\
% href        &       \Mandatory{URL} \Mandatory{anchor}\\
% hyperimage  & \Mandatory{image URL}\\
% hyperdef   &\Mandatory{URL} \Mandatory{category} 
% \Mandatory{name} \Mandatory{text}\\
% hyperref &\Mandatory{URL} \Mandatory{category} 
%  \Mandatory{name} \Mandatory{anchor}\\
% \hline
% hyperdef &\Optional[label] \Mandatory{URL} 
%  \Mandatory{category} \Mandatory{name} \Mandatory{text}\\
% hyperref &\Optional[label] \Mandatory{anchor}\\
% \hline
% htmladdimg &= {\tt\char'134hyperimage}\\
% htmladdnormallink &= {\tt\char'134href}\\
% \end{tabular}%
%
% The \verb|\hyperlink| command inserts \# in front of each link, making
% it relative to the current document; \verb|\href| expects a genuine URL.
%
% \section{Original, history and acknowledgements}%
%
% The original authors of |hyperbasics.tex| and |hypertex.sty|,
% from which this package descends, are:
% \begin{quote}%
%   Tanmoy Bhattacharya\\
%     \texttt{tanmoy@qcd.lanl.gov}\\
%   Los Alamos National Laboratory\\
%   New Mexico, 87544-0285\\
%   USA\\
% \emph{and} \\
%   Thorsten Ohl  \\ {\tt Thorsten.Ohl@Physik.TH-Darmstadt.de}\\
%   Technische Hochschule Darmstadt \\
%   Schlo\ss gartenstr. 9 \\
%   D-64289 Darmstadt \\
%   Germany \\
% \end{quote}%
% Eventually I rewrote most of the stuff, because
% I didn't understand a lot of the original, and was only interested
% in getting it to work with \LaTeXe; it still has problems, but the
% majority of \LaTeX\ documents go through, with good PDF
%  functionality.
%
% Tanmoy Bhattacharya found a great many of the bugs, and (even better)
% often provided fixes, which has made the package more robust.
% The days spent on Rev\TeX\ are entirely due to him!
% The investigations of Bill Moss (\texttt{bmoss@math.clemson.edu})  into
% the later versions including `nativepdf' scared up a host of bugs,
% and his testing is appreciated. Patrick Daly kindly updated
% his natbib package to allow easy integration with hyperref.
%
% I am grateful to Michael Mehlich, whose |hyper| package
% (developed in parallel with |hyperref|) showed me solutions
% for  some problems. Hopefully the two packages will combine 
% one of these days!
%
% Thanks to Arthur Smith, Mark Doyle, Paul Ginsparg, David Carlisle,
% T V Raman and Leslie Lamport for comments, requests, thoughts 
% and code to get the package into a useable state. 
%
% Especial extra thanks to David Carlisle for the ps2pdf and dviwindo
% support. Sergey Lesenko provided the changes needed for dvipdf,
% and Han The Thanh  supplied all the information needed for pdftex.
% \StopEventually{}%
%
% \section{The macros}%
%
%    \begin{macrocode}%
%<*package>
%    \end{macrocode}%
% \subsection{Package options}%
% It \emph{does} need the December 95 release of \LaTeX, because it uses
% |\protected@write|, and it defines commands in options; and the page
% setup internal code changed at that point. It'll probably break
% with the later releases!
%    \begin{macrocode}%
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\newif\ifHyper@Backref
\newif\ifHyper@driverloaded
\newif\ifHyper@psize
\newif\ifHyper@colorlinks
\newif\ifHyper@Figures
\newif\ifHyper@Nesting
\newif\ifHyper@Index
\newif\ifHyper@Plainpages
\newif\if@ActiveAnchor
\newif\ifHyper@raiselinks
\newif\ifHyper@breaklinks
\newif\ifHyper@pageanchor
\newif\ifHyper@debug
\Hyper@pageanchortrue
\Hyper@raiselinksfalse
\Hyper@breaklinksfalse
\newdimen\@linkdim
\Hyper@psizefalse
\Hyper@Indextrue
\Hyper@Figuresfalse
\Hyper@Nestingfalse
\Hyper@Backreffalse
\Hyper@driverloadedfalse
\def\pdf@bbox{pdf@llx pdf@lly pdf@urx pdf@ury}%
\RequirePackage{keyval}%
\RequirePackage{nameref}%
%    \end{macrocode}%
% General helper macro for package warnings and info
%    \begin{macrocode}
\def\hyper@warn#1{\PackageWarningNoLine{hyperref}{#1}}
\def\hyper@info#1{\PackageInfo{hyperref}{#1}}%
%    \end{macrocode}%
% The `draft' option makes the low-level macros no-ops.
%    \begin{macrocode}%
\DeclareOption{draft}{\AtBeginDocument{%
    \gdef\hyper@anchor#1#2{#2}%
    \gdef\hyper@@link[#1]#2#3#4{#4}%
    \def\hyper@anchorstart#1{}%
    \def\literalps@out#1{}%
    \def\hyper@anchorend{}%
    \def\hyper@linkstart#1{}%
    \def\hyper@linkend{}%
    \def\hyperbaseurl#1{}%
    \def\@writetorep#1#2#3{}%
    \def\pdfbookmark#1#2{}%
    \hyper@warn{ draft mode on}%
   }%
}%
\DeclareOption{nolinks}{\AtBeginDocument{%
    \gdef\hyper@anchor#1#2{#2}%
    \gdef\hyper@@link[#1]#2#3#4{#4}%
    \def\hyper@anchorstart#1{}%
    \def\hyper@anchorend{}%
    \def\hyper@linkstart#1{}%
    \def\hyper@linkend{}}%
}%
\DeclareOption{a4paper}{\def\special@paper{210mm,297mm}}%
\DeclareOption{a5paper}{\def\special@paper{148mm,210mm}}%
\DeclareOption{b5paper}{\def\special@paper{176mm,250mm}}%
\DeclareOption{letterpaper}{\def\special@paper{8.5in,11in}}%
\DeclareOption{legalpaper}{\def\special@paper{8.5in,14in}}%
\DeclareOption{executivepaper}{\def\special@paper{7.25in,10.5in}}%
%    \end{macrocode}%
% Colouring links at the \LaTeX\ level is useful for debugging, perhaps.
%    \begin{macrocode}%
\DeclareOption{colorlinks}{%
   \AtEndOfPackage{\RequirePackage{color}}%
   \def\colorlink#1{\color{#1}}%
   \Hyper@colorlinkstrue
}%
\DeclareOption{nocolorlinks}{%
   \def\colorlink#1{}%
   \Hyper@colorlinksfalse
}%
%    \end{macrocode}%
%The drivers for different backends
%    \begin{macrocode}%
\DeclareOption{pdftex}{%
   \input{pdftex.cfg}%
   \Hyper@breaklinkstrue
   \Hyper@driverloadedtrue
}%
\DeclareOption{dvipdf}{%
   \input{dvipdf.cfg}%
   \Hyper@driverloadedtrue
}%
\DeclareOption{nativepdf}{%
   \input{pdfmark.cfg}%
   \input{dvips.cfg}%
   \Hyper@driverloadedtrue
}%
\DeclareOption{pdfmark}{%
   \input{pdfmark.cfg}%
   \input{dvips.cfg}%
   \Hyper@driverloadedtrue
   }%
\DeclareOption{dvips}{%
   \input{pdfmark.cfg}%
   \input{dvips.cfg}%
   \Hyper@driverloadedtrue
   }%
\DeclareOption{hypertex}{%
   \input{hypertex.cfg}%
   \Hyper@driverloadedtrue
   }%
\DeclareOption{dviwindo}{%
    \input{dviwindo.cfg}%
    \ExecuteOptions{colorlinks}%
    \PassOptionsToPackage{dviwindo}{color}%
    \Hyper@driverloadedtrue}%
\DeclareOption{dvipsone}{%
   \input{pdfmark.cfg}%
   \input{dvipsone.cfg}%
   \Hyper@driverloadedtrue
}%
\DeclareOption{latex2html}{%
   \AtBeginDocument{\@@latextohtmlX}%
   }%
%    \end{macrocode}%
% Magic numbers fix for broken ps2pdf in ghostscript suite (David Carlisle)
%    \begin{macrocode}%
\DeclareOption{ps2pdf}{%
   \input{dvips.cfg}%
   \def\pdf@bbox{%
      pdf@llx .12 mul 72 add
      pdf@lly -.12 mul 770 add
      pdf@urx .12 mul 72 add
      pdf@ury -.12 mul 770 add}%
   \Hyper@driverloadedtrue
}%
\DeclareOption{nobookmarks}{%
 \AtEndOfPackage{\global\let\ReadBookmarks\relax\let\WriteBookmarks\relax}%
}%
\DeclareOption{nodebug}{%
   \Hyper@debugfalse
}%
\DeclareOption{debug}{%
   \Hyper@debugtrue
}%
%    \end{macrocode}%
% If we are going to PDF via HyperTeX |\special| commands, 
% the dvips (-Z option)  processor does not know
% the \emph{height} of a link, as it works solely on the
% position of the closing |\special|. If we use this option,
% the |\special| is raised up by the right amount, to fool
% the dvi processor.
%    \begin{macrocode}%
\DeclareOption{raiselinks}{%
  \Hyper@raiselinkstrue
}%
\DeclareOption{noraiselinks}{%
  \Hyper@raiselinksfalse
}%
\DeclareOption{breaklinks}{%
  \Hyper@breaklinkstrue
}%
\DeclareOption{nobreaklinks}{%
  \Hyper@breaklinksfalse
}%
\DeclareOption{nopageanchor}{%
  \Hyper@pageanchorfalse
}%
\DeclareOption{pageanchor}{%
  \Hyper@pageanchortrue
}%
%    \end{macrocode}%
% Set up back-referencing to be hyper links, by page or section number,
%    \begin{macrocode}%
\DeclareOption{backref}{%
  \PassOptionsToPackage{hyperref}{backref}%
  \Hyper@Backreftrue
}%
\DeclareOption{pagebackref}{%
  \PassOptionsToPackage{hyperpageref}{backref}%
  \Hyper@Backreftrue
}%
%    \end{macrocode}%
% Make index entries be links back to the relevant pages. By default
% this is turned on, but may be stopped.
%    \begin{macrocode}%
\DeclareOption{hyperindex}{%
  \Hyper@Indextrue
}%
\DeclareOption{nohyperindex}{%
  \Hyper@Indexfalse
}%
%    \end{macrocode}%
% Are the page links done as plain arabic numbers, or do
% they follow the formatting of the package? The latter loses
% if you put in typesetting like |\textbf| or the like.
%    \begin{macrocode}%
\DeclareOption{plainpages}{%
   \Hyper@Plainpagestrue
}%
\DeclareOption{noplainpages}{%
   \Hyper@Plainpagesfalse
}%
%    \end{macrocode}%
% Make included figures (assuming they use the standard graphics
% package) be hypertext links. Off by default. Needs more work.
%    \begin{macrocode}%
\DeclareOption{hyperfigures}{\Hyper@Figurestrue}%
\DeclareOption{nohyperfigures}{\Hyper@Figuresfalse}%
%    \end{macrocode}%
% Currently, |dvihps| doesn't allow anchors nested within targets,
% so this option tries to stop that happening. Other processors
% may be able to cope.
%    \begin{macrocode}%
\DeclareOption{nonesting}{%
  \Hyper@Nestingfalse
}%
\DeclareOption{nesting}{%
  \Hyper@Nestingtrue
}
\def\@nil{}
\def\special@paper{210mm,297mm}%
\ExecuteOptions{noraiselinks,nobreaklinks,nocolorlinks,noplainpages,%
  nonesting,hyperindex,nohyperfigures,pageanchor}%
\InputIfFileExists{hyperref.cfg}{}{}%
\ProcessOptions
\ifHyper@driverloaded\else\input{hypertex.cfg}\fi
\ifHyper@Backref
 \RequirePackage{backref}%
\else
 \def\Hyper@backout#1{}%
\fi
\@ActiveAnchorfalse
%    \end{macrocode}%
% \subsection{Basic hypertext macros}%
% Links have a filename (possibly a URL),
% an internal name, and some marked text.
% If the first parameter is empty, its an internal link,
% otherwise we need to open another file or a URL.
% Anchors have a name, and marked text.
%    \begin{macrocode}
\def\hyper@link{\@ifnextchar[{\hyper@@link}{\hyper@@link[Normal]}}%
%    \end{macrocode}%
% We have to be careful with the marked text, as if we break
% off part of something to put a |\special| around it, all hell breaks
% loose. Therefore, we check the category code of the first token,
% and only proceed if its safe. Tanmoy sorted this out.
%
% A curious case arises if the original parameter
% was in braces. That means that |#2| comes here a multiple
% letters, and the |noexpand| just looks at the first one,
% putting the rest in the output. Yuck.
%    \begin{macrocode}%
\long\def\hyper@anchor#1#2{\hyper@@anchor#1\relax#2\relax}%
\long\def\hyper@@anchor#1\relax#2#3\relax{%
\ifx\\#1\\#2\hyper@warn{empty link? #1: #2#3}%
\else
 \def\anchor@spot{#2#3}%
 \def\put@me@back{}%
 \ifx\relax#2\relax
 \else
    \ifHyper@Nesting
    \else
       \ifcat a\noexpand#2\relax
       \else
         \ifcat 0\noexpand#2 \relax
         \else
%\typeout{Anchor start is not alphanumeric on input line\the\inputlineno}%
           \def\anchor@spot{}%
           \def\put@me@back{#2#3}%
         \fi
      \fi
    \fi
 \fi
 \if@ActiveAnchor
      \anchor@spot
 \else
      \hyper@@@anchor{#1}%
 \fi
 \expandafter\put@me@back
\fi
}%
%    \end{macrocode}%
% We may need to keep the beginning and end of the anchor
% separate, when a whole section is the link. Set this up.
% Anchors of this type are \emph{not} coloured!!!
%    \begin{macrocode}%
%    \end{macrocode}%
% \subsection{User hypertext macros}%
% |\hyperlink| has two arguments, the name of a hypertext object
% defined somewhere by |\hypertarget|, and the anchor text. |\href|
% also has two arguments, but the first is in this case a full URL.
% The difference is that |\hyperlink| puts in the |#|  sign for you,
% for use in internal cross-referencing.
% You have to escape the |#|, |~| and |%| characters in the URL.
%
% |\hyperimage| points to a graphic.
%
% A simple link is created with |\hypertarget|, with two
% parameters of an anchor name, and anchor text.
%    \begin{macrocode}
\def\hyperlink#1#2{%
  \hyper@link{}{\hyper@hash#1}{#2}%
}
\def\href#1#2{%
  \bgroup
  \let\%\@percentchar
  \let\#\hyper@hash
  \let\~\hyper@tilde
  \hyper@link{#1}{}{#2}%
  \egroup
}%
\let\old@url\url
\def\url#1{\href{#1}{\old@url{#1}}}
\def\hypertarget#1#2{%
\ifHyper@Nesting
  \hyper@anchor{#1}{#2}%
\else
  \hyper@anchor{#1}#2
\fi
}%
%    \end{macrocode}%
% |\hyperref| is more complicated, as it includes the concept of a
% category of link, used to make the name. This is not really used in this
% package.  |\hyperdef| sets up an anchor in the same way. They each have
% four parameters: URL, name, category and link name.
% If there is an optional first parameter to |\hyperdef|,
% it is the name of a \LaTeX\ label which can be used in 
% a short form of |\hyperref| later, to avoid
% remembering the name and category.
%    \begin{macrocode}
\def\hyperref{\@ifnextchar[{\label@hyperref}{\@hyperref}}
\def\@hyperref#1#2#3#4{% URL, category, name, text
  \bgroup
  \let\%\@percentchar
  \let\#\hyper@hash
  \let\~\hyper@tilde
  \ifx\\#2\\%
    \ifx\\#3\\%
     \hyper@link{#1}{}{#4}%
    \else
        \hyper@link{#1}{\hyper@hash#3}{#4}%
    \fi
  \else
    \hyper@link{#1}{\hyper@hash#2.#3}{#4}%
  \fi
  \egroup
}
\def\hyperdef{\@ifnextchar[{\label@hyperdef}{\@hyperdef}}
\def\@hyperdef#1#2#3#4{% URL, category, name, text
\ifHyper@Nesting
  \hyper@anchor{#1#2.#3}{#4}%
\else
  \hyper@anchor{#1#2.#3}#4
\fi
}%
%    \end{macrocode}%
% We also have a need to give a \LaTeX\ \emph{label} to a
% hyper reference, to ease the pain of referring to it later
%    \begin{macrocode}
\def\label@hyperref[#1]#2{%
  \expandafter\label@@hyperref{\csname r@#1\endcsname}{#1}{#2}%
}
\def\label@@hyperref#1#2#3{%
  \ifx#1\relax
   \protect\G@refundefinedtrue
    \@latex@warning{Hyper reference `#2' on page \thepage \space
             undefined}%
   \hyper@link{}{??}{#3}%
  \else
   \hyper@link{}{#1}{#3}%
  \fi
}
\def\label@hyperdef[#1]#2#3#4#5{% label name, url, category, name,
                                % anchor text
  \@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{#1}{#2\protect\hyper@hash#3.#4}}%
  \@esphack
  \ifHyper@Nesting
   \hyper@anchor{#2#3.#4}{#5}%
  \else
   \hyper@anchor{#2#3.#4}#5
  \fi
}%
%    \end{macrocode}%
% \subsection{Compatibility with the \emph{\LaTeX{}2html} package}%
% Map our macro names on to Nikos', so that documents prepared
% for that system will work without change.
%
% Note, however, that the whole complicated structure for
% segmenting documents is not supported; it is assumed that the user
% will load |html.sty| first, and then |hyperref.sty|, so that the
% definitions in |html.sty| take effect, and are then overridden
% in a few circumstances by this package.
%    \begin{macrocode}%
\let\htmladdimg\hyperimage
\def\htmladdnormallink#1#2{\href{#2}{#1}}
\def\htmladdnormallinkfoot#1#2{\href{#2}{#1}\footnote{#2}}
\def\htmlref#1#2{% anchor text, label
  \label@hyperref[#2]{#1}%
}%
%    \end{macrocode}%
% This is really too much. The \LaTeX2html package defines its own
% |\hyperref| command, with a different syntax. Was this always here?
% Its weird, anyway. We interpret it in the `printed' way, since
% we are about fidelity to the page.
%    \begin{macrocode}
\def\@@latextohtmlX{%
 \let\hhyperref\hyperref
 \def\hyperref##1##2##3##4{% anchor text for HTML
                     % text to print before label in print
                     % label
                     % post-label text in print
  ##2\ref{##4}##3}%
}%
%    \end{macrocode}%
% \subsection{Automated \LaTeX\ hypertext cross-references}%
% Emend |\@setref| to put out a hypertext link as well as its
% normal text (which is used as an anchor).
%    \begin{macrocode}%
\let\real@setref\@setref
\def\@setref#1#2#3{% csname, extract macro, ref
  \ifx#1\relax
   \protect\G@refundefinedtrue
   \nfss@text{\reset@font\bfseries ??}%
   \@latex@warning{Reference `#3' on page \thepage \space
             undefined}%
  \else
   \hyper@link{}{\hyper@hash\expandafter\@fourthoffour#1\@empty\@empty}%
       {\expandafter#2#1\@empty\@empty\null}%
  \fi}%
%    \end{macrocode}%
% Set |\pageref| to be a link.
% |\realpageref| is available for people who know about these things,
% which uses a copy of |\@setref|.
%    \begin{macrocode}
\def\pageref#1{\expandafter\@pagesetref\csname r@#1\endcsname
                                   \@secondoftwo{#1}}
\def\@pagesetref#1#2#3{% csname, extract macro, ref
  \ifx#1\relax
   \protect\G@refundefinedtrue
   \nfss@text{\reset@font\bfseries ??}%
   \@latex@warning{Reference `#3' on page \thepage \space
             undefined}%
  \else
   \hyper@link{}{\hyper@hash page.\expandafter\@secondoffour#1}%
       {\expandafter\@secondoffour#1}%
  \fi}
\def\realpageref#1{\expandafter\real@setref
  \csname r@#1\endcsname\@secondoffour{#1}}%
%    \end{macrocode}%
% Anything which can be referenced advances some counter; we overload
% this to put in a hypertext starting point (with no visible anchor),
% and make a note of that for later use in |\label|.
% This will fail badly if |\theH<name>|
% does not expand to a sensible reference. This means that classes
% or package which introduce new elements need to define
% an equivalent  |\theH<name>|  for every  |\the<name>|. We do make
% a trap to make |\theH<name>| be the same as |\arabic{<name>}|,
% if |\theH<name>| is not defined, but this is not necessarily a good idea.
%
% All the shenanigans is to make sure section numbers etc
% are always arabic, separated by dots. Who knows how people
% will set up |\@currentlabel|? If they put spaces in (quite legal)
% then the hypertext processors will get upset.
%
% But this is flaky, and open to abuse. Styles like
% |subeqn| will mess it up, for starters. Appendices are an issue, too.
% We just hope to cover most situations. We can at least cope
% with the standard sectioning structure, allowing for |\part|
% and |\chapter|.
%    \begin{macrocode}%
\@ifundefined{thepart}{}{\newcommand\theHpart{\arabic{part}}}%
\@ifundefined{thechapter}{%
     \newcommand\theHsection        {\arabic{section}}%
     \newcommand\theHfigure         {\arabic{figure}}%
     \newcommand\theHtable         {\arabic{table}}%
  }{%
  \@ifundefined{thepart}%
    {\newcommand\theHchapter       {\arabic{part}.\arabic{chapter}}}%
    {\newcommand\theHchapter       {\arabic{chapter}}}%
  \newcommand\theHfigure        {\theHchapter.\arabic{figure}}%
  \newcommand\theHtable        {\theHchapter.\arabic{table}}%
  \newcommand\theHsection       {\theHchapter.\arabic{section}}%
  }%
\newcommand\theHsubsection    {\theHsection.\arabic{subsection}}%
\newcommand\theHsubsubsection {\theHsubsection .\arabic{subsubsection}}%
\newcommand\theHparagraph     {\theHsubsubsection.\arabic{paragraph}}%
\newcommand\theHsubparagraph  {\theHparagraph.\arabic{subparagraph}}%
\newcommand\theHtheorem       {\theHsection.\arabic{theorem}}%
\newcommand\theHthm       {\theHsection.\arabic{thm}}%
%    \end{macrocode}%
% Thanks to Greta Meyer (gbd@pop.cwru.edu) for making me realize
% that enumeration starts at 0 for every list! But |\item|
% occurs inside |\trivlist|, so check if its a real |\item| before
% incrementing counters.
%    \begin{macrocode}%
\let\H@item\item
\newcounter{Item}
\def\theHItem{\arabic{Item}}
\def\item{\if@nmbrlist\refstepcounter{Item}\fi\H@item}%
\newcommand\theHenumi  {\theHItem}%
\newcommand\theHenumii  {\theHItem}%
\newcommand\theHenumiii  {\theHItem}%
\newcommand\theHenumiv  {\theHItem}%
\newcommand\theHequation {\theHsection.\arabic{equation}}%
\newcommand\theHHfootnote  {\arabic{Hfootnote}}%
\newcommand\theHmpfootnote  {\arabic{mpfootnote}}%
\let\theHHmpfootnote\theHHfootnote
\newcommand\theHslide  {\arabic{slide}}%
\let\orig@appendix\appendix
\def\appendix{\orig@appendix
\@ifundefined{thechapter}%
 {\renewcommand\theHsection{\Alph{section}}}%
 {\renewcommand\theHchapter{\Alph{chapter}}}%
}%
%    \end{macrocode}%
% Tanmoy asked for this default handling of undefined |\theH<name>|
% situations. It really isn't clear what would be ideal, whether to
% turn off hyperizing of unknown elements, to pick up the textual
% definition of the counter, or to default it to something like
% |\arabic{name}|. We take the latter course, slightly worriedly.
%    \begin{macrocode}%
\let\H@refstepcounter\refstepcounter
\def\refstepcounter#1{%
 \def\w@t{equation}%
 \H@refstepcounter{#1}%
 \ifx#1\w@t
  \make@stripped@name{\theequation}%
  \let\theHequation\newname
 \fi
 \@ifundefined{theH#1}{%
  \expandafter\def\csname theH#1\endcsname{\arabic{#1}}%
  %\typeout{theH#1 defaulted to arabic counter}%
 }{}%
 \hyper@makecurrent{#1}%
 \hyper@anchorstart{\@currentHref}\hyper@anchorend
}
\def\hyper@makecurrent#1{%
 \edef\@currentHlabel{\csname theH#1\endcsname}%
 \global\edef\@currentHref{#1.\expandafter
    \strip@prefix\meaning\@currentHlabel}%
}%
%    \end{macrocode}%
% \subsection{Equations}%
% We want to  make the whole equation a target anchor.
% Overload equation, temporarily reverting to original
% |\refstepcounter|. If, however, its in AMS math, we do not
% do anything, as the tag mechanism is used there (see section \ref{ams}).
%    \begin{macrocode}%
\let\new@refstepcounter\refstepcounter
\let\H@equation\equation
\let\H@endequation\endequation
\@ifpackageloaded{amsmath}{}{%
 \def\equation{%
 \let\refstepcounter\H@refstepcounter
 \H@equation
 \make@stripped@name{\theequation}%
 \let\theHequation\newname
 \hyper@makecurrent{equation}%
 \hyper@anchorstart{\@currentHref}%
 \let\refstepcounter\new@refstepcounter
 }\def\endequation{\hyper@anchorend\H@endequation}}%
%    \end{macrocode}%
% My goodness, why can't \LaTeX{} be consistent? Why is |\eqnarray|
% set up differently from other objects?
%    \begin{macrocode}%
\let\H@eqnarray\eqnarray
\let\H@endeqnarray\endeqnarray
\def\eqnarray{%
 \let\reserved@a\relax
 \H@eqnarray
 \make@stripped@name{\theequation}%
 \let\theHequation\newname
 \hyper@makecurrent{equation}%
 \hyper@anchorstart{\@currentHref}%
}
\def\endeqnarray{%
 \hyper@anchorend
 \H@endeqnarray
}%
%    \end{macrocode}%
% Then again, we have the \emph{subeqnarray}%
% package. Tanmoy provided some code for this:
%    \begin{macrocode}%
\@ifundefined{subeqnarray}{}%
{\let\H@subeqnarray\subeqnarray
 \let\H@endsubeqnarray\endsubeqnarray
 \def\subeqnarray{%
  \let\reserved@a\relax
  \H@subeqnarray
  \make@stripped@name{\theequation}%
  \let\theHequation\newname
  \hyper@makecurrent{equation}%
  \hyper@anchorstart{\@currentHref}%
  }%
  \def\endsubeqnarray{%
   \hyper@anchorend
   \H@endsubeqnarray
  }%
\newcommand\theHsubequation {\theHequation\alph{subequation}}%
}
\def\make@stripped@name#1{{%
 \escapechar\m@ne
 \global\let\newname\@empty
 \protected@edef\@tempa{#1}%
 \edef\@tempb{%
  \noexpand\@tfor\noexpand\@tempa:=\expandafter\strip@prefix\meaning\@tempa}%
 \@tempb\do{%
  \if{\@tempa\else
    \if}\@tempa\else
      \xdef\newname{\newname\@tempa}%
    \fi
  \fi}}}%
%    \end{macrocode}%
% \subsection{Footnotes}%
% The footnote mark is a hypertext link, and the text is a target.
% We separately number the footnotes sequentially through the
% text, separately from whatever labels the text assigns. Too hard
% to keep track of markers otherwise.
%    \begin{macrocode}%
\newcounter{Hfootnote}%
\let\H@@footnotemark\@footnotemark
\let\H@@footnotetext\@footnotetext
\let\H@@mpfootnotetext\@mpfootnotetext
\long\def\@mpfootnotetext#1{%
  \H@@mpfootnotetext{%
%    \end{macrocode}%
% If we cannot have nesting, just the first character.
%    \begin{macrocode}%
\ifHyper@Nesting
  \hyper@anchor{\@currentHref}{#1}%
\else
  \hyper@anchor{\@currentHref}#1%
\fi
  }%
}%
\long\def\@footnotetext#1{%
  \H@@footnotetext{%
%    \end{macrocode}%
% If we cannot have nesting, just the first character.
%    \begin{macrocode}%
\ifHyper@Nesting
  \hyper@anchor{\@currentHref}{#1}%
\else
  \hyper@anchor{\@currentHref}#1%
\fi
  }%
}
\def\@footnotemark{%
 \H@refstepcounter{Hfootnote}%
 \hyper@makecurrent{Hfootnote}%
 \hyper@linkstart{\hyper@hash\@currentHref}%
 \H@@footnotemark\hyper@linkend
}
\def\realfootnote{\@ifnextchar[\@xfootnote{\stepcounter{\@mpfn}%
     \protected@xdef\@thefnmark{\thempfn}%
     \H@@footnotemark\H@@footnotetext}}%
%    \end{macrocode}%
% But the special footnotes
% in |\maketitle| are much too hard to deal with
% properly. Let them revert to plain behaviour.
%    \begin{macrocode}%
\let\orig@maketitle\maketitle
\def\maketitle{%
 \let\H@@origfootnotemark\@footnotemark
 \let\H@@origfootnotetext\@footnotetext
 \let\@footnotemark\H@@footnotemark
 \let\@footnotetext\H@@footnotetext
 \orig@maketitle
 \ifx\@footnotemark\H@@footnotemark
  \let\@footnotemark\H@@origfootnotemark
 \fi
 \ifx\@footnotetext\H@@footnotetext
  \let\@footnotetext\H@@origfootnotetext
 \fi
}%
%    \end{macrocode}%
% \subsection{Float captions}%
% Make the float caption the hypertext anchor; curiously enough,
% we can't just copy the definition of |\@caption|. Its all to do
% with expansion. It screws up. Sigh.
%    \begin{macrocode}
\def\caption{\H@refstepcounter\@captype \@dblarg{\@caption\@captype}}%
\long\def\@caption#1[#2]#3{%
  \hyper@makecurrent{\@captype}%
  \par\addcontentsline{\csname
  ext@#1\endcsname}{#1}{\protect\numberline{\csname
  the#1\endcsname}{\ignorespaces #2}}\begingroup
    \@parboxrestore
    \normalsize
    \ifx\\#3\\\def\cap@contents{~}\else\def\cap@contents{#3}\fi
    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces
%    \end{macrocode}%
% If we cannot have nesting, just the first character of the caption
% is the anchor. If the caption is empty, make it an unbreakable space.
%    \begin{macrocode}%
\ifHyper@Nesting
 \hyper@anchorstart{\@currentHref}\cap@contents\hyper@anchorend
\else
 \hyper@anchor{\@currentHref}\cap@contents{}%
\fi
}\par
  \endgroup}%
%    \end{macrocode}%
% \subsection{Bibliographic references}%
% This is not very robust, since many styles redefine these things.
% The package used to redefine |\@citex| and the like; then we tried
% adding the hyperref call explicitly into the .aux file.
% Now we redefine |\bibcite|; this still breaks some citation packages
% so we have to work around them. But this remains extremely dangerous.
% Any or all of \emph{achemso}, \emph{chapterbib}, 
% and \emph{drftcite} may break. 
%
% However, lets make an attempt to get \emph{natbib} right, because
% thats a powerful, important package. Patrick Daly has
% provided hooks for us, so all we need to do is activate them.
%    \begin{macrocode}
\def\hyper@natlinkstart#1{%
  \Hyper@backout{#1}%
  \hyper@linkstart{\hyper@hash cite.#1}%
}
\def\hyper@natlinkend{%
  \hyper@linkend
}
\def\hyper@natanchorstart#1{%
     \hyper@anchorstart{cite.#1}%
}
\def\hyper@natanchorend{\hyper@anchorend}%
\@ifpackageloaded{natbib}{}{%
\def\bibcite#1#2{%
 \@newl@bel{b}{#1}{\hyper@link[Cite]{}{\hyper@hash cite.#1}{#2}}}%
%    \end{macrocode}%
% |\@BIBLABEL| is working around a `feature' of Rev\TeX.
%    \begin{macrocode}%
\providecommand{\@BIBLABEL}{\@biblabel}
\def\@lbibitem[#1]#2{%
 \item[\hyper@anchorstart{cite.#2}\@BIBLABEL{#1}\hyper@anchorend\hfill]%
 \if@filesw{\let\protect\noexpand
   \immediate\write\@auxout{%
     \string\bibcite{#2}{#1}}}%
  \fi
  \ignorespaces
}
\def\@bibitem#1{%
 \item
 \hyper@anchorstart{cite.#1}\relax\hyper@anchorend
 \if@filesw {\let\protect\noexpand
 \immediate\write\@auxout{%
   \string\bibcite{#1}{\the\value{\@listctr}}}}%
 \fi
 \ignorespaces
}%
}%
%    \end{macrocode}%
% Revtex (bless its little heart) takes over |\bibcite| and looks
% at the result to measure something. Make this a hypertext link
% and it goes ape. Therefore, make an anodyne result first, call
% its business, then go back to the real thing.
%    \begin{macrocode}%
\@ifclassloaded{revtex}{%
 \hyper@info{*** compatibility with revtex **** }%
 \def\revtex@checking#1#2{%
   \expandafter\let\expandafter\T@temp\csname b@#1\endcsname
   \expandafter\def\csname b@#1\endcsname{#2}%
   \@SetMaxRefLabel{#1}%
   \expandafter\let\csname b@#1\endcsname\T@temp
  }%
%    \end{macrocode}%
% Tanmoy provided this replacement for CITEX. Lord knows what it does.
%    \begin{macrocode}%
\@ifundefined{@CITE}{\def\@CITE{\@cite}}{}
\def\@CITEX[#1]#2{%
  \let\@citea\@empty
  \leavevmode\unskip$^{\scriptstyle
  \@CITE{\@for\@citeb:=#2\do
    {\@citea\def\@citea{,\penalty\@m\ }%
     \edef\@citeb{\expandafter\@firstofone\@citeb}%
     \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
     \@ifundefined{b@\@citeb}{\mbox{\reset@font\bfseries ?}%
       \G@refundefinedtrue
       \@latex@warning
         {Citation `\@citeb' on page \thepage \space undefined}}%
       {{\csname b@\@citeb\endcsname}}}}{#1}}$}%
%    \end{macrocode}%
% No, life is too short. I am not going to understand the
% Revtex |\@collapse| macro, I shall
% just restore the original behaviour of |\@citex|;
% sigh. This is SO vile.
%    \begin{macrocode}
\def\@citex[#1]#2{%
  \let\@citea\@empty
  \@cite{\@for\@citeb:=#2\do
    {\@citea\def\@citea{,\penalty\@m\ }%
     \edef\@citeb{\expandafter\@firstofone\@citeb}%
     \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
     \@ifundefined{b@\@citeb}{\mbox{\reset@font\bfseries ?}%
       \G@refundefinedtrue
       \@latex@warning
         {Citation `\@citeb' on page \thepage \space undefined}}%
       {\hbox{\csname b@\@citeb\endcsname}}}}{#1}}%
}{}%
%    \end{macrocode}%
% Override Peter Williams' Harvard package; we have to
% a) make each of the citation types into a link; b) make
% each citation write a backref entry, and c) kick off a backreference
% section for each bibliography entry.
%    \begin{macrocode}%
\@ifpackageloaded{harvard}{%
 \hyper@info{*** compatibility with harvard **** }%
 \Hyper@raiselinksfalse
 \def\harvardcite#1#2#3#4{%
  \global\@namedef{HAR@fn@#1}{\hyper@link[Cite]{}{\hyper@hash cite.#1}{#2}}%
  \global\@namedef{HAR@an@#1}{\hyper@link[Cite]{}{\hyper@hash cite.#1}{#3}}%
  \global\@namedef{HAR@yr@#1}{\hyper@link[Cite]{}{\hyper@hash cite.#1}{#4}}%
  \global\@namedef{HAR@df@#1}{\csname HAR@fn@#1\endcsname}%
 }%
 \def\HAR@citetoaux#1{%
   \if@filesw\immediate\write\@auxout{\string\citation{#1}}\fi%
   \ifHyper@Backref
    \ifx\@empty\@currentlabel\else
     \@bsphack
     \protected@write\@auxout{}%
     {\string\@writefile{brf}%
       {\string\backcite{#1}{{\@currentlabel}{\thepage}{\@currentHref}}}}%
     \@esphack
    \fi
  \fi
 }%
 \def\harvarditem{\@ifnextchar[{\@harvarditem}{\@harvarditem[\null]}}%
 \def\@harvarditem[#1]#2#3#4#5\par{%
 \item[]%
 \hyper@anchorstart{cite.#4}\relax\hyper@anchorend%
 \if@filesw{ \def\protect##1{\string ##1\space}%
 \ifthenelse{\equal{#1}{\null}}%
  {\def\next{{#4}{#2}{#2}{#3}}}%
  {\def\next{{#4}{#2}{#1}{#3}}}%
 \immediate\write\@auxout{\string\harvardcite\codeof\next}%
 }\fi%
 \protect\hspace*{-\labelwidth}\protect\hspace*{-\labelsep}\ignorespaces%
 #5
 \ifHyper@Backref
  \newblock
  \backref{\csname br@#4\endcsname}%
 \fi
 \par
}%
}{}%
%    \end{macrocode}%
% \subsection{Page numbers}%
% Give every page an automatic number anchor. This involves, sigh,
% overloading \LaTeX's output bits and pieces, which must be dangerous.
% This used to be |\@shipoutsetup|, now |\@begindvi|. We cannot even
% overload this, as it sets itself to null. SIGH.
%    \begin{macrocode}
\def\@begindvi{%
  \unvbox \@begindvibox
 \ifHyper@pageanchor
  \@hyperfixhead
  \global\let \@begindvi \@hyperfixhead
 \else
  \global\let \@begindvi \@empty
 \fi
}
\def\hyperpageanchor{%
   \ifHyper@Plainpages
     \hyper@anchorstart{page.\arabic{page}}\hyper@anchorend
   \else
     \hyper@anchorstart{page.\thepage}\hyper@anchorend
   \fi
 }%
\let\HYPERPAGEANCHOR\hyperpageanchor
\def\@hyperfixhead{%
 \let\H@old@thehead\@thehead
   \ifHyper@Plainpages
     \gdef\@foo{\hyper@anchor{page.\arabic{page}}}%
   \else
     \gdef\@foo{\hyper@anchor{page.\thepage}}%
   \fi
   \expandafter\ifx\expandafter\@empty\H@old@thehead
     \def\H@old@thehead{\hfil}\fi
  \def\@thehead{\@foo\relax\H@old@thehead}%
}%
%    \end{macrocode}%
%\subsection{Table of contents}%
%    \begin{macrocode}
\def\addcontentsline#1#2#3{%
  \ifx\@currentHref\@empty
  \hyper@warn{contentsline with no destination
     at line \the\inputlineno}\fi
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thepage}{\@currentHref}}%
}
\def\contentsline#1#2#3#4{%
%     \end{macrocode}%
% TV Raman noticed that people who add the list of figures into the TOC
% generate a bad or null link. Avoid that by checking if the destination
% is empty.
%     \begin{macrocode}%
 \ifx\\#4\\%
  \csname l@#1\endcsname{#2}{#3}%
\else
 \csname l@#1\endcsname{%
    \hyper@linkstart{\hyper@hash#4}{#2}\hyper@linkend}%
 {#3}%
\fi
}%
%    \end{macrocode}%
% \subsection{New counters}%
% The whole theorem business makes up new counters on the fly;
% we are going to intercept this. Sigh. Do it at the level where
% new counters are defined.
%     \begin{macrocode}%
\let\H@definecounter\@definecounter
\def\@definecounter#1{%
   \H@definecounter{#1}%
   \expandafter\def\csname theH#1\endcsname       {\arabic{#1}}%
}%
%    \end{macrocode}%
% But what if they have used the optional argument to e.g. |\newtheorem|
% to determine when the numbering is reset? OK, we'll trap that too.
%    \begin{macrocode}%
\let\H@newctr\@newctr
\def\@newctr#1[#2]{%
  \H@newctr#1[#2]%
  \expandafter\def\csname theH#1\endcsname
    {\csname the#2\endcsname.\arabic{#1}}%
}%
%    \end{macrocode}%
% \subsection{AMS \LaTeX\ compatibility}\label{ams}%
% Oh, no, they don't use anything as simple as |\refstepcounter|
% in the AMS! We need to intercept some low-level operations
% of theirs. Damned if we are going to try and work out what the hell
% they get up to. Just stick a label of `AMS' on the front, and use the
% label \emph{they} worked out. If that produces something invalid, I give
% up. They'll change all the code again anyway, I expect.
%    \begin{macrocode}%
\let\Hmake@df@tag@@\make@df@tag@@
\def\make@df@tag@@#1{%
 \Hmake@df@tag@@{#1}%
 \global\edef\@currentHref{AMS.\theequation}%
}%
\let\H@seteqlabel\@seteqlabel
\def\@seteqlabel#1{%
 \H@seteqlabel{#1}%
 \global\edef\@currentHref{AMS.\theequation}%
}%
%    \end{macrocode}%
% This code I simply cannot remember what I was trying to achieve.
% The final result seems to do nothing anyway.
%\begin{verbatim}%
%\let\H@tagform@\tagform@
%\def\tagform@#1{%
%  \maketag@@@{\hyper@anchor{\@currentHref}%
%  {(\ignorespaces#1\unskip)}}%
%}%
%\def\eqref#1{\textup{\H@tagform@{\ref{#1}}}}%
%\end{verbatim}%
% \subsection{Included figures}%
% Simply intercept the low level graphics package macro.
%    \begin{macrocode}%
\ifHyper@Figures
 \def\Gin@setfile#1#2#3{%
 \hyperimage{#3}%
 }%
\fi
%    \end{macrocode}%
%
% \subsection{Index entries}%
% Hyper-indexing works crudely, by forcing code onto the end of the index
% entry with the \verb+|+ feature; this puts a hyperlink around
% the printed page numbers. It will not proceed if the author has already
% used the \verb+|+ specifier for something like emboldening entries.
% That would make Makeindex fail (cannot have two \verb+|+ specifiers).
% The solution is for the author to use generic coding, and put in
% the requisite |\hyperPage| in his/her own macros along with the boldness.
%
% This section is poor stuff; it's open to all sorts of abuse. Sensible
% large projects will design their own indexing macros any bypass this.
%    \begin{macrocode}%
\ifHyper@Index
 \def\@wrindex#1{\@@wrindex#1||\\}%
 \def\@@wrindex#1|#2|#3\\{%
 \ifx\\#2\\%
   \protected@write\@indexfile{}%
      {\string\indexentry{#1|hyperpage}{\thepage}}%
 \else
   \protected@write\@indexfile{}%
      {\string\indexentry{#1|#2}{\thepage}}%
 \fi
 \endgroup
 \@esphack
}%
\fi
%    \end{macrocode}%
% This again is quite flaky, but allow for the common situation of a
% page range separated by en-rule. We split this into two different
% hyperlinked pages.
%    \begin{macrocode}
\def\hyperpage#1{\@hyperpage#1----\\}
\def\@hyperpage#1--#2--#3\\{%
 \ifx\\#2\\%
    \@commahyperpage{#1}%
 \else
  \hyperlink{page.#1}{#1}--\hyperlink{page.#2}{#2}%
 \fi
}
\def\@commahyperpage#1{\@@commahyperpage#1, ,\\}
\def\@@commahyperpage#1, #2,#3\\{%
 \ifx\\#2\\%
   \hyperlink{page.#1}{#1}%
 \else
   \hyperlink{page.#1}{#1}, \hyperlink{page.#2}{#2}%
 \fi
}%
%    \end{macrocode}%
%
%\subsection{Compatibility with seminar slide package}%
%    \begin{macrocode}%
\let\oldslide@heading\slide@heading
\def\slide@heading[#1]#2{%
 \@writetorep{#1}{slide.\theslide}{0}%
 \oldslide@heading[#1]{#2}%
}%
\@ifundefined{listofslides}{}{%
 \def\l@slide#1#2#3{%
    \slide@undottedcline{\slidenumberline{#3}{\hyperlink{slide.#2}{#2}}}{}}%
}%
%    \end{macrocode}%
% \subsection{Localized nullifying of package}%
% Sometimes we just don't want the wretched package interfering
% with us. Define an environment we can put in manually, or include
% in a style file, which stops the hypertext functions doing anything.
% This is used, for instance, in the Elsevier classes, to stop
% |hyperref| playing havoc in the front matter.
%    \begin{macrocode}
\def\NoHyper{%
  \def\hyper@anchor##1##2{##2}%
  \def\hyper@@link[##1]##2##3##4{##4}%
  \def\hyper@anchorstart##1{}%
  \def\hyper@anchorend{}%
  \def\hyper@anchorendspecial{}%
}
\def\endNoHyper{}%
%    \end{macrocode}%
% \subsection{Low-level utility macros}%
% We need unrestricted access to the |#|, |~| and |"| characters, so make
% them nice macros.
%    \begin{macrocode}%
\edef\hyper@hash{\string#}%
\edef\hyper@tilde{\string~}%
\edef\hyper@quote{\string"}
\def\@currentHref{}%
%    \end{macrocode}%
% \subsection{Setup}%
%    \begin{macrocode}%
\ifHyper@Figures
 \hyper@info{Hyper figures ON}%
\else
 \hyper@info{Hyper figures OFF}%
\fi
\ifHyper@Nesting
 \hyper@info{Link nesting ON}%
\else
 \hyper@info{Link nesting OFF}%
\fi
\ifHyper@Index
 \hyper@info{Hyperindex ON}%
\else
 \hyper@info{Hyperindex OFF}%
\fi
\ifHyper@Plainpages
 \hyper@info{Plain pages ON}%
\else
 \hyper@info{Plain pages OFF}%
\fi
\ifHyper@Backref
 \hyper@info{Backreferencing ON}%
\else
 \hyper@info{Backreferencing OFF}%
\fi
\ifHyper@colorlinks
    \hyper@info{Link coloring ON}%
\else
    \hyper@info{Link coloring OFF}%
\fi
\expandafter\def\csname /LNKcolor\endcsname{\LinkColor}%
\expandafter\def\csname /ANNcolor\endcsname{\LinkColor}%
\expandafter\def\csname /DESTcolor\endcsname{\AnchorColor}
\def\AnchorColor{blue}
\def\LinkColor{red}
\def\LinkColorURL{green}
\def\LinkColorFile{yellow}%
%    \end{macrocode}%
% We give the start of document a special label; this is used
% in backreferencing-by-section, to allow for cites before
% any sectioning commands.
%    \begin{macrocode}%
\AtBeginDocument{\hyper@anchorstart{Doc-Start}\hyper@anchorend}%
%    \end{macrocode}%
% \subsection{Compatibility of aux and toc files}%
% [This section is by David Carlisle].
% Some extra tests so that the hyperref package may be removed or added
% to a document without having to remove .aux and .toc files.
% All the code is delayed to |\begin{document}|
%    \begin{macrocode}%
\AtBeginDocument{%
%    \end{macrocode}%
% First the code to deal with removing the hyperref package from
% a document.
%
% Write some stuff into the aux file so if the next run is done
% without hyperref, then |\contentsline| and |\newlabel| are defined
% to cope with the extra arguments.
%    \begin{macrocode}%
  \immediate\write\@auxout{%
%    \end{macrocode}%
%
%    \begin{macrocode}%
    \string\ifx\string\hyper@anchor\string\@undefined^^J%
%    \end{macrocode}%
%
%    \begin{macrocode}%
       \global\let\string\oldcontentsline\string\contentsline^^J%
       \gdef\string\contentsline\string#1\string#2\string#3\string#4{%
           \string\oldcontentsline{\string#1}{\string#2}{\string#3}}^^J%
       \global\let\string\oldnewlabel\string\newlabel^^J%
       \gdef\string\newlabel\string#1\string#2{%
           \string\newlabelxx{\string#1}\string#2}^^J%
       \gdef\string\newlabelxx%
           \string#1\string#2\string#3\string#4\string#5{%
             \string\oldnewlabel{\string#1}{{\string#2}{\string#3}}}^^J%
%    \end{macrocode}%
%
% But the new aux file will be read again at the end, with the normal
% definitions expected, so better put things back as they were.
%    \begin{macrocode}%
       \string\AtEndDocument{%
          \let\string\contentsline\string\oldcontentsline^^J%
          \let\string\newlabel\string\oldnewlabel}^^J%
%    \end{macrocode}%
%
% If the document is being run with hyperref put this definition
% into the aux file, so we can spot it on the next run.
%    \begin{macrocode}%
    \string\else^^J%
       \global\let\string\hyper@last\relax^^J%
    \string\fi^^J%
    }%
%    \end{macrocode}%
%
% Now the code to deal with adding the hyperref package to a document
% with aux and toc written the standard way.
%
% If hyperref was used last time, do nothing. If it was not used
% Locally use the original definition of |\contentsline| while
% reading a toc file, and add two blank groups to any |\newlabel|.
%    \begin{macrocode}%
  \ifx\hyper@last\@undefined
    \def\@starttoc#1{%
      \begingroup
      \makeatletter
      \def\contentsline##1{\csname l@##1\endcsname}%
      \@input{\jobname.#1}%
      \if@filesw
        \expandafter\newwrite\csname tf@#1\endcsname
        \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
      \fi
      \@nobreakfalse
    \endgroup}%
%    \end{macrocode}%
%
%    \begin{macrocode}%
    \def\newlabel#1#2{\@newl@bel r{#1}{#2{}{}}}%
  \fi}%
%</package>
%<*pdftex>
%    \end{macrocode}%
% \section{Configuration files}%
% \subsection{pdftex}%
% This driver is for Han The Than's \TeX{} variant
% which produces PDF directly. This has new primitives
% to do PDF things, which usually translate almost directly to
% PDF code, so there is a lot of flexibility which we do not at
% present harness.
%
% First define the anchors; the choice here is the key for how
% the link will be presented. We chose |fitbh| by default. 
%    \begin{macrocode}
\def\new@pdflink#1{%
 \def\hyper@hash{}%
 \pdfdest name {#1!} \pdftexFitMethod}
\def\pdf@endanchor{}
\def\pdftexFitMethod{fitbh}%
%
%    \end{macrocode}%
% Now the links; the interesting part here is the set of attributes
% which define how the link looks. We probably want to add a border
% and color it, but there are other choices. This directly translates
% to PDF code, so consult the manual for how to change this. We will
% add an interface at some point.
%    \begin{macrocode}%
\AtBeginDocument{%
  \ifHyper@colorlinks
   \def\pdfBorderAttrs{/Border [0 0 0]}%
  \fi
}
\def\PDFBorders{0 0 1}
\def\pdfBorderAttrs{/Border [\PDFBorders]}
\def\find@pdflink#1{%
  \def\hyper@hash{}%
  \leavevmode\pdfannotlink
  attr{\pdfBorderAttrs /C [\CurrentBorderColor]}%
  goto name {#1!}%
  \bgroup\colorlink{\LinkColor}%
}
\def\close@pdflink{\egroup\pdfendlink}
\def\hyperbaseurl#1{\pdfcatalog uri {<< /Base (#1)>>}}
\def\hyper@@@anchor#1{\new@pdflink{#1}\anchor@spot\pdf@endanchor}
\def\hyper@anchorstart#1{\new@pdflink{#1}\@ActiveAnchortrue}
\def\hyper@anchorend{\@ActiveAnchorfalse\pdf@endanchor}
\def\hyper@linkstart#1{\find@pdflink{#1}}
\def\hyper@linkend{\close@pdflink}
\def\hyper@@link[#1]#2#3#4{%
  \ifx\\#2\\%
    \edef\CurrentBorderColor{\csname BorderColor@#1\endcsname}%
    \find@pdflink{#2#3}#4\close@pdflink
  \else
    \Externalpdfmark{#2}{#3}{#4}%
  \fi
}
\def\CurrentBorderColor{\BorderColor@Normal}
\def\@URLpdfmark#1#2{% 
  \def\hyper@hash{}%
   \leavevmode\pdfannotlink
   attr{\pdfBorderAttrs /C [\BorderColor@URL]}%
   user{/S /URI /URI (#2)}{\colorlink{\LinkColorURL}#1}%
   \pdfendlink
}
\def\@Filepdfmark#1#2#3{% 
  \def\hyper@hash{}%
  \leavevmode\pdfannotlink
  attr{\pdfBorderAttrs /C [\BorderColor@File]}%
  goto file{#3} name{#1}{\colorlink{\LinkColorFile}#2}%
  \pdfendlink
}%
%    \end{macrocode}%
% Let us explicitly turn on PDF generation; they can reverse 
% this decision in the document, but since we are emitting PDF
% links anyway, we \emph{must} be in PDF mode.
%    \begin{macrocode}%
\pdfoutput=1
\pdfcompresslevel=9
\pdfpagewidth\paperwidth
\pdfpageheight\paperheight
%</pdftex>
%<*hypertex>
%    \end{macrocode}%
% \subsection{hypertex}%
% The Hyper\TeX\ specification (this is
% borrowed from an article by Arthur Smith)
% says that conformant viewers/translators
% must recognize the following set of |\special| commands:
% \begin{description}%
% \item[href:] |html:<a href = "href_string">|
% \item[name:] |html:<a name = "name_string">|
% \item[end:] |html:</a>|
% \item[image:] |html:<img src = "href_string">|
% \item[base\_name:] |html:<base href = "href_string">|
% \end{description}%
%
% The \emph{href}, \emph{name} and \emph{end} commands are used to do
% the basic hypertext operations of establishing links between sections
% of documents. The \emph{image} command is intended (as with current
% html viewers) to place an image of arbitrary graphical
% format on the page in the current location.  The \emph{base\_name}%
% command is be used to communicate to the \emph{dvi} viewer the full (URL)
% location of the current document so that
% files specified by relative URL's may be retrieved correctly.
%
% The \emph{href} and \emph{name} commands must be paired with an
% \emph{end} command later in
% the \TeX{} file --- the \TeX{} commands between the two ends of a pair
% form an \emph{anchor} in the document. In the case of an \emph{href}%
% command, the \emph{anchor} is to be highlighted in the
% \emph{dvi} viewer, and
% when clicked on will cause the scene to shift to the destination
% specified by \emph{href\_string}. The \emph{anchor} associated with a
% name command represents a possible location to which other hypertext
% links may refer, either as local references (of the form
% \texttt{href="\#name\_string"} with the \emph{name\_string}%
% identical to the one in the name command) or as part of a URL (of the
% form \emph{URL\#name\_string}). Here \emph{href\_string} is a valid
% URL or local identifier, while name\_string could be any string at
% all: the only caveat is that `|"|' characters should be escaped with a
% backslash (|\|), and if it looks like a URL name it may cause
% problems.
%
%    \begin{macrocode}
\def\hyper@@@anchor#1{%
   {\let\protect=\string\special{html:<A name=\hyper@quote #1\hyper@quote>}}%
   \@ActiveAnchortrue
   \bgroup\colorlink{\AnchorColor}\anchor@spot\egroup
   \special{html:</A>}%
   \@ActiveAnchorfalse
}
\def\hyperbaseurl#1{%
 \special{html:<base href="#1">}%
}
\def\hyper@anchorstart#1{%
  \special{html:<A name=\hyper@quote#1\hyper@quote>}%
  \@ActiveAnchortrue
}
\def\hyper@anchorend{%
  \special{html:</A>}%
  \@ActiveAnchorfalse
}
\def\hyper@linkstart#1{%
  \bgroup
  \colorlink{\LinkColor}%
  \special{html:<A href=\hyper@quote#1\hyper@quote>}%
}
\def\hyper@linkend{%
  \special{html:</A>}%
  \egroup
}
\def\hyper@@link[#1]#2#3#4{%
%    \end{macrocode}%
% If we want to raise up the final link |\special|, we need to
% get its height; ask me why \LaTeX\ constructs make this totally
% foul up, and make us revert to basic \TeX. I do not know.
%    \begin{macrocode}%
 \ifHyper@raiselinks
  \setbox\@tempboxa=\hbox{#4}%
  \@linkdim\dp\@tempboxa
  \lower\@linkdim\hbox{\special{html:<A href=\hyper@quote#2#3\hyper@quote>}}%
  {\colorlink{\LinkColor}#4}%
  \@linkdim\ht\@tempboxa
%    \end{macrocode}%
% Because of the interaction with the dvihps processor, we have to subtract a
% little from the height. This is not clean, or checked. Check with Mark
% Doyle about what gives here. It may not be needed with 
% the new dvips (Jan 1997).
%    \begin{macrocode}%
  \advance\@linkdim by -6.5\p@
  \raise\@linkdim\hbox{\special{html:</A>}}%
 \else
  \special{html:<A href=\hyper@quote#2#3\hyper@quote>}%
  {\colorlink{\LinkColor}#4}%
   \special{html:</A>}%
 \fi
}
\def\hyperimage#1{%
  \bgroup
  \let\%\@percentchar
  \let\#\hyper@hash
  \let\~\hyper@tilde
  \special{html:<img src=\hyper@quote#1\hyper@quote>}%
  \egroup
}
\def\@writetorep#1#2#3{}
\def\pdfbookmark#1#2{}%
%</hypertex>
%<*dviwindo>
%    \end{macrocode}%
% \subsection{dviwindo}%
% [This is developed by David Carlisle].
% Within a file dviwindo hyperlinking is used, for external
% URL's a call to |\wwwbrowser| is made. (You can define
% this command before or after loading the hyperref package
% if the default |c:/netscape/netscape| is not suitable)
% Dviwindo could in fact handle external links to dvi files on
% the same machine without calling a web browser, but that would
% mean parsing the URL to recognise such, and this is currently
% not done.
%
% This was more or less blindly copied from the hypertex cfg.
% For dviwindo \TeX{} must specify the size of the active area
% for links. For some hooks this information is available
% but for some, the start and end of the link are
% specified separately in which case a fixed size area
% of 10000000sp wide by |\baselineskip| high is used.
%
% This has the path structure and internet browser hard wired\ldots
% You can redefine this to be something else before or after
% loading hyperref.
%    \begin{macrocode}%
\providecommand\wwwbrowser{%
 c:\string\netscape\string\netscape}
\def\hyper@@@anchor#1{%
   {\let\protect=\string\special{mark: \hyper@quote #1\hyper@quote}}%
   \@ActiveAnchortrue
   \bgroup\colorlink{\AnchorColor}\anchor@spot\egroup
   \@ActiveAnchorfalse
}
\def\hyperbaseurl#1{}
\def\hyper@anchorstart#1{%
  \special{mark: \hyper@quote#1\hyper@quote}%
  \@ActiveAnchortrue
}
\def\hyper@anchorend{%
  \@ActiveAnchorfalse
}
\def\hyper@linkstart#1{%
  \bgroup
  \colorlink{\LinkColor}%
  \external@check#1\@empty\external@check{10000000}{\number\baselineskip}}
\def\external@check#1#2\external@check#3#4{%
  \if#1\hyper@hash
% internal to this document, remove the hash.
    \special{button:  #3  #4 \hyper@quote#2\hyper@quote}%
  \else
% external call netscape to do the work....
    \special{button: #3 #4 launch: 
       \wwwbrowser\space \hyper@quote#1#2\hyper@quote}%
  \fi}
\def\hyper@linkend{%
  \egroup
}
\def\hyper@@link[#1]#2#3#4{%
 \ifHyper@raiselinks
  \setbox\@tempboxa=\hbox{#4}%
  \@linkdim\dp\@tempboxa
  \lower\@linkdim\hbox{%
     \external@check#2#3\external@check
         {\number\wd\@tempboxa}{\number\ht\@tempboxa}}%
  {\colorlink{\LinkColor}#4}%
  \@linkdim\ht\@tempboxa
  \advance\@linkdim by -6.5\p@
  \raise\@linkdim\hbox{}%
 \else
  \setbox\@tempboxa=\hbox{#4}%
  \external@check#2#3\external@check
              {\number\wd\@tempboxa}{\number\ht\@tempboxa}%
  {\colorlink{\LinkColor}#4}%
   %
 \fi
}
\def\hyperimage#1{}
\def\@writetorep#1#2#3{}
\def\pdfbookmark#1#2{}%
%</dviwindo>
%<*pdfmark|dvipdf>
%    \end{macrocode}%
% \subsection{Direct pdfmark support (dvipdf and pdfmark)}%
%    \begin{macrocode}
\def\hyper@@@anchor#1{%
   {\pdfmark[\anchor@spot]{pdfmark=/DEST,View=\pdfView,Dest=#1}}%
}%
%<*dvipdf>
\def\hyperbaseurl#1{}
\def\hyper@anchorstart#1{\@ActiveAnchortrue}
\def\hyper@anchorend{\@ActiveAnchorfalse}
\def\hyper@linkstart#1{%
   \bgroup
   \colorlink{\LinkColor}%
   \def\hyper@hash{}%
   \gdef\hyper@currentanchor{#1}%
}
\def\hyper@linkend{%
 \egroup
}%
%</dvipdf>
%<*pdfmark>
\def\hyperbaseurl#1{%
 \pdfmark{pdfmark=/DOCVIEW,URI=<< /Base (#1) >>}%
}
\def\hyper@anchorstart#1{%
  \PSHyperAnchorStart
  \gdef\hyper@currentanchor{#1}%
  \@ActiveAnchortrue
}
\def\hyper@anchorend{%
   \PSHyperAnchorEnd
   \pdfmark{pdfmark=/DEST,View=\pdfView,
       Dest=\hyper@currentanchor,
       Rect=\pdf@bbox}%
  \@ActiveAnchorfalse
}
\def\hyper@linkstart#1{%
   \ifHyper@breaklinks\else\mbox\bgroup\fi\bgroup
   \colorlink{\LinkColor}%
   \def\hyper@hash{}%
   \PSHyperLinkStart
   \global\edef\hyper@currentanchor{#1}%
}
\def\hyper@linkend{\@ifnextchar[{\hyper@@linkend}{\hyper@@linkend[Normal]}}
\def\hyper@@linkend[#1]{%
    \PSHyperLinkEnd
    \edef\@foo{\csname BorderColor@#1\endcsname}%
       \pdfmark{pdfmark=/ANN,Subtype=/Link,Dest=\hyper@currentanchor,
           Border=\PDFBorders,Color=\@foo,Rect=\pdf@bbox}%
   \egroup
   \ifHyper@breaklinks\else\egroup\fi
}%
%</pdfmark>
\def\hyperimage#1{%
  \bgroup
  \let\%\@percentchar
  \let\#\hyper@hash
  \let\~\hyper@tilde
  \@URLpdfmark{}{#1}%
  \egroup
}
\def\hyper@@link[#1]#2#3#4{%
  \ifx\\#2\\\def\hyper@hash{}%
    \edef\@foo{\csname BorderColor@#1\endcsname}%
%<dvipdf> \pdfmark[#4]{pdfmark=/LNK,{},Border=\PDFBorders,Color=\@foo,Dest=#3}%
%<pdfmark> \pdfmark[#4]{Color=\@foo,Border=\PDFBorders,pdfmark=/ANN,Subtype=/Link,Dest=#3}%
  \else
    \Externalpdfmark{#2}{#3}{#4}%
  \fi
}%
%    \end{macrocode}%
% We define a single macro, pdfmark, which uses the `keyval' system
% to define the various allowable keys; these are \emph{exactly}%
% as listed in the pdfmark reference for Acrobat 2.0. The only addition
% is \texttt{pdfmark} which specifies the type of pdfmark to create
% (like ANN, LINK etc). The
% surrounding round and square brackets in the pdfmark commands
% are supplied, but you have to put in / characters as needed for the
% values.
%
%    \begin{macrocode}%
\newcommand\pdfmark[2][]{%
    \edef\goforit{\noexpand\pdf@toks={ \the\pdf@defaulttoks}}%
    \goforit
    \let\pdf@type\relax
    \setkeys{PDF}{#2}%
    \ifx\pdf@type\relax
       \hyper@warn{no pdfmark type specified in #2!!}%
       \ifx\\#1\\\relax\else\pdf@rect{#1}\fi
    \else
       \bgroup
       \ifx\\#1\\\relax
       \else
         \colorlink{\@ifundefined{\pdf@type color}%
                      \LinkColor
                      {\csname\pdf@type color\endcsname}}%
         \pdf@rect{#1}%
       \fi
%<pdfmark>  \literalps@out{[\the\pdf@toks\space \pdf@type\space pdfmark}%
%<dvipdf>  \literalps@out{/ANN >>}%
       \egroup
    \fi
}%
%    \end{macrocode}%
% The complicated bit is working out the right enclosing rectangle of
% some piece of \TeX\ text, needed by the /Rect key. This solution originates
% with  Toby Thain (\texttt{tobyt@netspace.net.au}).
%    \begin{macrocode}%
\newsavebox{\pdf@box}
\def\pdf@rect#1{%
%<dvipdf>   \literalps@out{/ANN \pdf@type \the\pdf@toks\space <<}#1
   \leavevmode
   \sbox\pdf@box{#1}%
   \dimen@\ht\pdf@box
   \lower\dp\pdf@box\hbox{\PSHyperRectStart}%
%    \end{macrocode}%
% If the text has to be horizontal mode stuff then just unbox
% the saved box like this, which saves executing it twice, which can
% mess up counters etc (thanks DPC\ldots).
%    \begin{macrocode}%
   \ifHyper@breaklinks\unhbox\else\box\fi\pdf@box
%    \end{macrocode}%
% but if it can have multiple paragraphs you'd need one of these,
% but in that case the measured box size would be wrong anyway.
%   |\ifHyper@breaklinks#1\else\box\pdf@box\fi|
%   |\ifHyper@breaklinks{#1}\else\box\pdf@box\fi|
%    \begin{macrocode}%
   \raise\dimen@\hbox{\PSHyperRectEnd}%
   \pdf@addtoks{[\pdf@bbox]}{Rect}%
}%
%    \end{macrocode}%
% All the supplied material is stored in a token list; since I do not
% feel sure I quite understand these, things may not work as expected
% with expansion. We'll have to experiment.
%    \begin{macrocode}%
\newtoks\pdf@toks
\newtoks\pdf@defaulttoks
\pdf@defaulttoks={ }
\def\pdf@addtoks#1#2{%
   \edef\goforit{\pdf@toks{\the\pdf@toks\space /#2 #1}}%
   \goforit
}
\def\PDFdefaults#1{%
 \pdf@defaulttoks={#1}%
}%
%    \end{macrocode}%
% This is the list of allowed keys. See the Acrobat manual for an
% explanation.
%    \begin{macrocode}%
% what is the type of pdfmark?
\define@key{PDF}{pdfmark}{\def\pdf@type{#1}}%
% parameter is a name
\define@key{PDF}{Action}{\pdf@addtoks{#1}{Action}}%
% parameter is a array
\define@key{PDF}{Border}{\pdf@addtoks{[#1]}{Border}}%
% parameter is a array
\define@key{PDF}{Color}{\pdf@addtoks{[#1]}{Color}}%
% parameter is a string
\define@key{PDF}{Contents}{\pdf@addtoks{(#1)}{Contents}}%
% parameter is a integer
\define@key{PDF}{Count}{\pdf@addtoks{#1}{Count}}%
% parameter is a array
\define@key{PDF}{CropBox}{\pdf@addtoks{[#1]}{CropBox}}%
% parameter is a string
\define@key{PDF}{DOSFile}{\pdf@addtoks{(#1)}{DOSFile}}%
% parameter is a string or file
\define@key{PDF}{DataSource}{\pdf@addtoks{(#1)}{DataSource}}%
% parameter is a destination
\define@key{PDF}{Dest}{\ifx\\#1\\\else\pdf@addtoks{/#1}{Dest}\fi}%
% parameter is a string
\define@key{PDF}{Dir}{\pdf@addtoks{(#1)}{Dir}}%
% parameter is a string
\define@key{PDF}{File}{\pdf@addtoks{(#1)}{File}}%
% parameter is a int
\define@key{PDF}{Flags}{\pdf@addtoks{#1}{Flags}}%
% parameter is a array
\define@key{PDF}{ID}{\pdf@addtoks{[#1]}{ID}}%
% parameter is a string
\define@key{PDF}{MacFile}{\pdf@addtoks{(#1)}{MacFile}}%
% parameter is a string
\define@key{PDF}{ModDate}{\pdf@addtoks{(#1)}{ModDate}}%
% parameter is a string
\define@key{PDF}{Op}{\pdf@addtoks{(#1)}{Op}}%
% parameter is a Boolean
\define@key{PDF}{Open}{\pdf@addtoks{#1}{Open}}%
% parameter is a integer or name
\define@key{PDF}{Page}{\pdf@addtoks{#1}{Page}}%
% parameter is a name
\define@key{PDF}{PageMode}{\pdf@addtoks{#1}{PageMode}}%
% parameter is a string
\define@key{PDF}{Params}{\pdf@addtoks{(#1)}{Params}}%
% parameter is a array
\define@key{PDF}{Rect}{\pdf@addtoks{[#1]}{Rect}}%
% parameter is a integer
\define@key{PDF}{SrcPg}{\pdf@addtoks{#1}{SrcPg}}%
% parameter is a name
%<pdfmark>\define@key{PDF}{Subtype}{\pdf@addtoks{#1}{Subtype}}%
%<dvipdf>\define@key{PDF}{Subtype}{\pdf@addtoks{#1}{}}%
% parameter is a string
\define@key{PDF}{Title}{\pdf@addtoks{(#1)}{Title}}%
% parameter is a string
\define@key{PDF}{Unix}{\pdf@addtoks{(#1)}{Unix}}%
% parameter is a string
\define@key{PDF}{UnixFile}{\pdf@addtoks{(#1)}{UnixFile}}%
% parameter is a array
\define@key{PDF}{View}{\pdf@addtoks{[#1]}{View}}%
% parameter is a string
\define@key{PDF}{WinFile}{\pdf@addtoks{(#1)}{WinFile}}%
%    \end{macrocode}%
% These are the keys used in the DOCINFO section.
%    \begin{macrocode}
\define@key{PDF}{Author}{\pdf@addtoks{(#1)}{Author}}
\define@key{PDF}{CreationDate}{\pdf@addtoks{(#1)}{CreationDate}}
\define@key{PDF}{Creator}{\pdf@addtoks{(#1)}{Creator}}
\define@key{PDF}{Producer}{\pdf@addtoks{(#1)}{Producer}}
\define@key{PDF}{Subject}{\pdf@addtoks{(#1)}{Subject}}
\define@key{PDF}{Keywords}{\pdf@addtoks{(#1)}{Keywords}}
\define@key{PDF}{ModDate}{\pdf@addtoks{(#1)}{ModDate}}
\define@key{PDF}{Base}{\pdf@addtoks{(#1)}{Base}}
\define@key{PDF}{URI}{\pdf@addtoks{#1}{URI}}%
%    \end{macrocode}%
% And now for some useful examples:
%    \begin{macrocode}%
\newcommand\PDFNextPage[2][]{%
 \pdfmark[#2]{#1,Border=\PDFBorders,Color=.2 .1 .5,
  pdfmark=/ANN,Subtype=/Link,Page=/Next}}%
\newcommand\PDFPreviousPage[2][]{%
 \pdfmark[#2]{#1,Border=\PDFBorders,Color=.4 .4 .1,
  pdfmark=/ANN,Subtype=/Link,Page=/Prev}}
\def\PDFOpen#1{%
  \pdfmark{#1,pdfmark=/DOCVIEW}%
}%
%    \end{macrocode}%
% This is not as simple as it looks; if we make the argument of
% this macro eg |\pageref{foo}| and expect it to expand to `3',
% we need a special version of |\pageref|
% which does \emph{not} produce
% `3 \hbox{}'\ldots. David Carlisle looked at this bit and provided
% the solution, as ever!
%    \begin{macrocode}%
\newcommand\PDFPage[3][]{%
 \let\pageref\simple@pageref
 \pdfmark[#3]{#1,Page=#2,Border=\PDFBorders,
   Color=\BorderColor@Page,pdfmark=/ANN,Subtype=/Link}}
\def\simple@pageref#1{%
  \expandafter\ifx\csname r@#1\endcsname\relax
   0%
  \else
    \expandafter\expandafter\expandafter
          \@secondoffour\csname r@#1\endcsname
  \fi}%
%    \end{macrocode}%
% This will only work if you use Distiller 2.1 or higher.
%    \begin{macrocode}
\def\@URLpdfmark#1#2{%
%<pdfmark> \pdfmark[#1]{pdfmark=/ANN,Border=\PDFBorders,Color=\BorderColor@URL,
%<dvipdf> \pdfmark[#1]{pdfmark=/LNK,Border=\PDFBorders,Color=\BorderColor@URL,
%<pdfmark> Action=<< /Subtype /URI /URI (#2) >>,Subtype=/Link}%
%<dvipdf> Action=URI /URI (#2)}%
}
\def\@Filepdfmark#1#2#3{%
 \def\hyper@hash{}%
%<pdfmark> \pdfmark[#2]{pdfmark=/ANN,Subtype=/Link,
%<pdfmark> Border=\PDFBorders,Color=\BorderColor@File,Action=/GoToR,File=#3,Dest=#1}%
%<dvipdf> \pdfmark[#2]{pdfmark=/LNK,Border=\PDFBorders, Color=\BorderColor@File,
%<dvipdf>  Action=/GoToR,File=#3,Dest=#1}%
}%
%</pdfmark|dvipdf>
%<*dvipdf|pdfmark|pdftex>
%    \end{macrocode}%
% The problem here is that the first (URL) parameter may be a
% \texttt{file:} reference (in which case we can use it tell Acrobat to open
% the file), or a genuine URL, in which case we'll have to activate
% Weblink. Note a simple name is also a genuine URL, as that is interpreted
% as a relative file name.
%
% The URL opening only works with Distiller 2.1 or later,
% and the reader needs the Weblink plug-in.
%    \begin{macrocode}
\def\Externalpdfmark#1#2#3{%
%    \end{macrocode}%
% Parameters are:
% \begin{enumerate}%
% \item The URL or file
% \item The internal name
% \item The link string
% \end{enumerate}%
%    \begin{macrocode}%
 \@Externalpdfmark{#2}{#3}#1:::\\%
}
\def\@Externalpdfmark#1#2#3:#4:#5:#6\\{%
%    \end{macrocode}%
% Now (potentially), we are passed:
% 1) The internal name, 2) the link string,
% 3) the URL type (http, file etc)
% 4) the site 5) the port number
%    \begin{macrocode}%
 \def\@pdftempa{#3#6}\def\@pdftempb{file:}%
 \ifx\@pdftempa\@pdftempb
    \@Filepdfmark{#1}{#2}{#4}%
 \else
   \def\@pdftempb{file::}%
   \ifx\@pdftempa\@pdftempb
      \@Filepdfmark{#1}{#2}{{#4:#5}}%
   \else
      \ifx\\#6\\%
       \def\@url{#3#1}%
      \else
       \ifx\\#5\\\def\@url{#3:#4#1}\else\def\@url{#3:#4:#5#1}\fi
      \fi
      \@URLpdfmark{#2}{\@url}%
     \fi
 \fi
}
\def\BorderColor@Normal{1 0 0}
\def\BorderColor@URL{0 1 1}
\def\BorderColor@File{0 .5 .5}
\def\BorderColor@Cite{0 1 0}
\def\BorderColor@Page{1 1 0}%
%</dvipdf|pdfmark|pdftex>
%<*outlines>
%    \end{macrocode}%
% \section{Bookmarks in the PDF file}%
% This was originally developed by Yannis Haralambous 
% (it was the separate |repere.sty|); it needed
% the |repere| or |makebook.pl| post-processor to work properly. Now
% redundant, as it is done all in \LaTeX{} macros.
%
% To write out the current section title, and its rationalized number,
% we have to intercept the |\@sect| command, which is rather
% dangerous. But how else to see the information we need?
%    \begin{macrocode}%
\let\H@old@sect\@sect
\def\@sect#1#2#3#4#5#6[#7]#8{%
 \H@old@sect{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{#8}%
%<dvipdf>  \literalps@out{/BOOK <<}%%
%    \end{macrocode}%
% If the sectioning internal commands are abused and beaten, what
% are we to do? Answer, nothing if the first parameter is empty.
%    \begin{macrocode}%
 \ifx\\#1\\\else
%<!dvipdf>  \ifnum#2<\c@secnumdepth
   \edef\@thishlabel{\csname theH#1\endcsname}%
%<dvipdf>\literalps@out{/BOOK /title (#7) \space /level #1.\@thishlabel\space >> }%%
%<!dvipdf>\@writetorep{#7}{#1.\@thishlabel}{#2}\fi
 \fi
}%
%    \end{macrocode}%
% Unfortunately, that only works if the section headings use the
% standard |\@startsection| macros. Chapters and parts typically
% don't. This makes it almost impossible to get 100\% right, so we
% just intercept the code in the standard styles.
%    \begin{macrocode}%
\let\H@old@part\@part
\def\@part[#1]#2{%
%<dvipdf>\literalps@out{/BOOK << }%%
 \H@old@part[{#1}]{#2}%
%<dvipdf> \literalps@out{/BOOK /title (#1) /level part.\theHpart\space >> } %%
%<!dvipdf> \@writetorep{#1}{part.\theHpart}{-1}%
}%
\let\H@old@chapter\@chapter
\def\@chapter[#1]#2{%
%<dvipdf>\literalps@out{/BOOK << }%
 \H@old@chapter[{#1}]{#2}%
%<dvipdf> \literalps@out{/BOOK /title (#1) \space /level chapter.\theHchapter \space >>}%
%<!dvipdf>  \@writetorep{#1}{chapter.\theHchapter}{0}%
  }%
\expandafter\def\csname Parent-2\endcsname{}%
\expandafter\def\csname Parent-1\endcsname{}%
\expandafter\def\csname Parent0\endcsname{}%
\expandafter\def\csname Parent1\endcsname{}%
\newwrite\@outlinefile
\def\@writetorep#1#2#3{%
    \@tempcnta#3
    \expandafter\edef\csname Parent#3\endcsname{#2}%
    \advance\@tempcnta by -1
\ifx\WriteBookmarks\relax\else
    \protected@write\@outlinefile%
       {\let~\space
        \def\LaTeX{LaTeX}%
        \def\TeX{TeX}%
        \let\label\@gobble
        \let\index\@gobble
        \let\glossary\@gobble}%
       {%
  \protect\BOOKMARK{#2}{#1}{\csname Parent\the\@tempcnta\endcsname}}%
\fi
}%
%    \end{macrocode}%
% Tobias Oetiker rightly points out that we need a way to
% force a boomark entry. So we introduce |\pdfbookmark|, 
% with two parameters, the title, and a symbolic name.
%    \begin{macrocode}%
\newcommand{\pdfbookmark}[2]{%
 \@writetorep{#1}{#2.0}{0}%
 \hyper@anchorstart{#2.0}\hyper@anchorend}%
%    \end{macrocode}%
% The macros for calculating structure of outlines 
% are derived from those by  Petr Olsak used in the texinfopdf macros.
%    \begin{macrocode}%
\AtBeginDocument{\ReadBookmarks}
\def\ReadBookmarks{%
  \def\BOOKMARK ##1##2##3{\calc@bm@number{##3}}%
  \InputIfFileExists{\jobname.out}{
%<pdftex>\pdfcatalog pagemode {/UseOutlines}  
}{}%
  \def\BOOKMARK ##1##2##3{%
    \def\@tempx{##2}%
%<*pdftex>
     \pdfoutline goto
          name{##1!}count-\check@bm@number{##1}{%
  \expandafter\strip@prefix\meaning\@tempx}%
%</pdftex>
%<*pdfmark>
 \pdfmark{pdfmark=/OUT,Count=\check@bm@number{##1},
    Dest=##1,Title=\expandafter\strip@prefix\meaning\@tempx}%
%</pdfmark>
  }%
  {\def\WriteBookmarks{0}%
   \escapechar\m@ne\InputIfFileExists{\jobname.out}{}{}}%
   \ifx\WriteBookmarks\relax\else
    \immediate\openout\@outlinefile=\jobname.out
   \fi
}
\def\check@bm@number#1{\expandafter \ifx\csname#1\endcsname \relax 0%
    \else \csname#1\endcsname \fi}
\def\calc@bm@number#1{\@tempcnta=\check@bm@number{#1}\relax
    \advance\@tempcnta by1
    \expandafter\xdef\csname#1\endcsname{\the\@tempcnta}}%
%</outlines>
%<*repere>
\newwrite\@reperefile
\immediate\openout\@reperefile=\jobname.rep
\def\@writetorep#1#2#3{%
    \protected@write\@reperefile
       {\def\TeX{TeX}%
        \def\LaTeX{LaTeX}%
        \let\label\@gobble
        \let\index\@gobble
        \let\glossary\@gobble}%
       {(#2) <#1>}%
}%
%</repere>
%    \end{macrocode}%
% \subsection{Device dependent setup}%
% Unfortunately, some parts of the |pdfmark|
% PostScript code depend on vagaries
% of the dvi driver. We isolate here all the problems.
% \subsubsection{dvipsone}%
%    \begin{macrocode}%
%<*dvipsone>
\def\literalps@out#1{\special{ps:#1}}%
\begingroup
  \catcode`P=12
  \catcode`T=12
  \lowercase{\endgroup
\gdef\rem@ptetc#1.#2PT#3!{#1\ifnum#2>\z@.#2\fi}}
\def\strip@pt@and@otherjunk#1{\expandafter\rem@ptetc\the#1!}
\def\pdf@setheight{\literalps@out{%
  \strip@pt@and@otherjunk\baselineskip
  \space 2 sub
  PDFToDvips /HyperBase exch def
  }%
}%
%    \end{macrocode}%
% When |\pdfView| is used, a pair of square brackets are placed around it, but 
% either yandytex or dvipsone chooses to throw away the last! So I put
% in an extra ] --- must ask Berthold Horn about this.
%    \begin{macrocode}
\def\pdfView#1{%pdf@hoff pdf@voff null}%
/XYZ gsave revsclx currentpoint grestore 72 add
   exch pop null exch null]}%
%
%    \end{macrocode}%
% These are called at the start and end of unboxed links;
% their job is to leave available PS variables called
% |pdf@llx pdf@lly pdf@urx pdf@ury|, which are the coordinates
% of the bounding rectangle of the link, and |pdf@hoff pdf@voff|
% which are the PDF page offsets. These latter are currently not
% used in the dvipsone setup.
% The Rect pair are called at the LL and UR corners of a box
% known to \TeX. 
%    \begin{macrocode}
\def\PSHyperAnchorStart{\literalps@out{HyperStart }}
\def\PSHyperAnchorEnd{%
  \pdf@setheight
  \literalps@out{HyperAutoEnd HyperAutoVoff }%
}
\def\PSHyperLinkStart{\literalps@out{HyperStart }}
\def\PSHyperLinkEnd{%
  \pdf@setheight
  \literalps@out{HyperAutoEnd}%
}
\def\PSHyperRectStart{\literalps@out{HyperStart }}
\def\PSHyperRectEnd{\literalps@out{HyperEnd HyperVoff }}%
\special{headertext=
%    \end{macrocode}%
% dvipsone lives in scaled points; does this mean 65536 or 65781?
%    \begin{macrocode}%
/DvipsToPDF { 65781 div  } def
/PDFToDvips { 65781 mul } def
/HyperBorder  { 1 PDFToDvips } def
/HyperVoff {
   currentpoint exch pop DvipsToPDF /pdf@voff exch def
 } def
/HyperAutoVoff {
   currentpoint exch pop
    HyperBase sub % baseline skip
    DvipsToPDF /pdf@voff exch def
 } def
/HyperStart {
   currentpoint
    HyperBorder add /pdf@lly exch def
    dup   DvipsToPDF /pdf@hoff exch def
    HyperBorder sub /pdf@llx exch def
} def
/HyperAutoEnd  {
   currentpoint
    HyperBase sub /pdf@ury exch def
    /pdf@urx exch def
} def
/HyperEnd  {
   currentpoint
    HyperBorder sub /pdf@ury exch def
    HyperBorder add /pdf@urx exch def
 } def
 systemdict
 /pdfmark known not
 {userdict /pdfmark systemdict /cleartomark get put} if
}%
\AtBeginDocument{%
  \ifHyper@colorlinks
   \def\PDFBorders{0 0 0}%
  \fi
}
\def\PDFBorders{0 0 65781}%
%</dvipsone>
%<*dvips>
%    \end{macrocode}%
% \subsubsection{dvips}%
%
% dvips thinks in 10ths of a big point, its
% coordinate space is resolution dependent,
% and its $y$ axis starts at the top of the
% page. Other drivers can and will be different!
%
% The work is done in |SDict|, because we add in some header
% definitions in a moment.
%    \begin{macrocode}
\def\pdfView{/XYZ pdf@hoff pdf@voff null}
\def\literalps@out#1{\special{ps:SDict begin #1 end}}%
%    \end{macrocode}%
% 
% The calculation of upper left $y$ is done without
% raising the point in \TeX,
% by simply adding on the current |\baselineskip| to the current $y$.
% This is usually too much, so we remove a notional 2 points.
%
% We have to allow for |\baselineskip| having an optional
% stretch and shrink (you meet this in slide packages, for instance),
% so we need to strip off the junk. David Carlisle, of course,
% wrote this bit of code.
%    \begin{macrocode}%
\begingroup
  \catcode`P=12
  \catcode`T=12
  \lowercase{\endgroup
\gdef\rem@ptetc#1.#2PT#3!{#1\ifnum#2>\z@.#2\fi}}
\def\strip@pt@and@otherjunk#1{\expandafter\rem@ptetc\the#1!}%
% this works out the current baselineskip and converts it
% to the dvips coordinate system
\def\pdf@setheight{\literalps@out{%
  \strip@pt@and@otherjunk\baselineskip
  \space 2 sub dup
  /HyperBasePt exch def
  PDFToDvips /HyperBaseDvips exch def
  }%
}
\def\PSHyperAnchorStart{\literalps@out{HyperStart }}
\def\PSHyperAnchorEnd{%
  \pdf@setheight
  \literalps@out{HyperAutoEnd HyperAutoVoff }%
}
\def\PSHyperLinkStart{\literalps@out{HyperStart }}
\def\PSHyperLinkEnd{%
  \pdf@setheight
  \literalps@out{HyperAutoEnd}%
}
\def\PSHyperRectStart{\literalps@out{HyperStart }}
\def\PSHyperRectEnd{\literalps@out{HyperEnd HyperVoff }}%
\special{papersize=\special@paper}%
%    \end{macrocode}%
% Do not ask what this rubbish is. Trying to make |pdfmark|
% get the right destination for views.
%    \begin{macrocode}%
\special{!
%    \end{macrocode}%
% Unless I am going mad, this \emph{appears} to be the relationship
% between the default coordinate system (PDF), and dvips;
% \begin{verbatim}%
% /DvipsToPDF { .01383701 div Resolution div } def
% /PDFToDvips { .01383701 mul Resolution mul } def
% \end{verbatim}%
% the latter's coordinates are resolution dependent, but what that
% .01383701 is, who knows? well, almost everyone except me, I expect\ldots
% And yes, Maarten Gelderman \texttt{<mgelderman@econ.vu.nl>}%
% points out that its 1/72.27 (the number of points to an inch, big points 
% to inch is 1/72). This also suggests that the code would be more 
% understandable (and exact) if 0.013 div would be replaced by 72.27 mul,
% so here we go. If this isn't right, I'll revert it.
%    \begin{macrocode}%
/DvipsToPDF { 72.27 mul Resolution div } def
/PDFToDvips { 72.27 div Resolution mul } def
%    \end{macrocode}%
% The rectangle around the links starts off
% \emph{exactly} the size of the box;
% we will to make it slightly bigger, 1 point on all sides.
%    \begin{macrocode}%
/HyperBorder  { 1 PDFToDvips } def
% the distance from the top of the page to the current point, in
% PDF coordinates
/HyperVoff {
   currentpoint exch pop vsize 72 sub 
   exch DvipsToPDF sub /pdf@voff exch def
 } def
%    \end{macrocode}%
% the distance from the top of the page to a point
% |\baselineskip| above the current point in PDF coordinates
%    \begin{macrocode}%
/HyperAutoVoff {
   currentpoint exch pop
    vsize 72 sub exch DvipsToPDF
    HyperBasePt sub % baseline skip
    sub /pdf@voff exch def
 } def
%    \end{macrocode}%
% the $x$ and $y$ coordinates of the current point, in PDF coordinates
%    \begin{macrocode}%
/HyperStart {
   currentpoint
    HyperBorder add /pdf@lly exch def
    dup DvipsToPDF  /pdf@hoff exch def
    HyperBorder sub /pdf@llx exch def
} def
%    \end{macrocode}%
% the $x$ and $y$ coordinates of the current point, minus the baselineskip
%    \begin{macrocode}%
/HyperAutoEnd  {
   currentpoint
    HyperBaseDvips sub /pdf@ury exch def
    /pdf@urx exch def
} def
 /HyperEnd  {
   currentpoint
    HyperBorder sub /pdf@ury exch def
    HyperBorder add /pdf@urx exch def
 } def
 systemdict
 /pdfmark known not
 {userdict /pdfmark systemdict /cleartomark get put} if
}%
\AtBeginDocument{%
  \ifHyper@colorlinks
   \def\PDFBorders{0 0 0}%
  \fi
}
\def\PDFBorders{0 0 12}%
%</dvips>
%<*dvipdf>
\def\literalps@out#1{\special{pdf: #1}}
\def\pdfView{ /FitB }%
\AtBeginDocument{%
  \ifHyper@colorlinks
   \def\PDFBorders{0 0 0}%
  \fi
}
\def\PDFBorders{0 0 1}%
%</dvipdf>
%    \end{macrocode}%
%
% \Finale
%
\endinput

