%======================================================================
% hyper-text support for MY OWN papers
% an attempt to produce parallel HTML, DVI and PDF versions
% Paul Taylor,  4 June 2003

% note that tth ignores \usepackage, so this must be \input instead

% to run TTH together with "tidy" first do
%    alias tthtidy  '/usr/local/bin/tth -i -r -L\!:1 < \!:1.tex \|
%    /usr/bin/tidy -indent -omit -upper -clean -f /dev/null > \!:1.html '

% BEFORE loading this, define \username \wwwname \urlfont

% This package defines

% \iftth \ifpdf - according as we're under tth, tex or pdftex
% \href@fmt - respectively html dvi and pdf

% \baseurl \baseurltext \baseurlhref - essentially \hostname/~\username/

% \href         {URL}{TEXT} - TEXT with link to URL
% \hreffmt     {FILE}{TEXT} - TEXT with link to FILE.\href@fmt
% \hrefix       {DIR}{TEXT} - TEXT with link to DIR/index.\href@fmt

% \pthref      {FILE}{TEXT} - TEXT with link to \baseurl/FILE
% \pthreffmt   {FILE}{TEXT} - TEXT with link to \baseurl/FILE.\href@fmt
% \pthrefix     {DIR}{TEXT} - TEXT with link to \baseurl/DIR/index.\href@fmt

% \pthrefref   {FILE} - link to \baseurl/FILE with the same as text
% \pthrefreffmt{FILE} - link to \baseurl/FILE.\href@fmt
% \pthrefrefix  {DIR} - link to \baseurl/DIR/index.\href@fmt

%======================================================================
% We assume that the catcodes of ~ and # are normal

% Who am I?
% NB: since ~ is \active it can re-define itself later

\ifx\username\undefined\def\username{pt}\fi

%======================================================================
% Establish whether we're DVI, HTML or PDF
% (We can't detect translation into PostScript,
% but assume that's only for hard copy anyway.)

\newif\iftth\tthfalse % TTH will set it true for itself
\newif\ifpdf\pdffalse

\iftth
  \def\href@fmt{html}%
  \ifx\typeout \undefined\def\typeout#1{}\fi
  \ifx\noexpand\undefined\def\noexpand{}\fi
  \ifx\mathbb  \undefined\def\mathbb{\mathbf}\fi
  \ifx\AtEndDocument\undefined\def\AtEndDocument#1{}\fi
\else
  \makeatletter
  \def\href@fmt{dvi}%
  \ifx\pdfoutput\undefined
  \else\ifnum\pdfoutput>0 
     \def\href@fmt{pdf}%
     \pdftrue
  \fi\fi
  \ifx\href\undefined
      \typeout{! you also need the "hyperref" package}%
      \def\href#1#2{#2}%
  \fi
\fi

%======================================================================

% default \username to pt
\ifx\username\undefined
   \typeout{! please define username for pthref}%
   \def\username{pt}%
\fi

% default \wwwname (normally eg www.di.unito.it) to localhost
\ifx\wwwname\undefined
   \typeout{! defaulting wwwname to localhost}%
   \def\wwwname{localhost}%
\fi

% how to print urls in text
\ifx\urlfont\undefined\def\urlfont{\texttt}\fi

% corresponding place in the Unix filesystem
\ifx\basedir\undefined
   \xdef\basedir{/homes/\username/public_html/}%
\fi

% how to embed URLS in HREFs and in text
\iftth
   \ifx\baseurl\undefined
      % default home URL
      \xdef\baseurl{\wwwname/~\username/}%
      \xdef\baseurltext{\wwwname/$\sim$\username/}
      \xdef\baseurlhref{http://\wwwname/~\username/}%
   \else
      \let\baseurltext\baseurl  % gets ~ wrong
      \xdef\baseurlhref{http://\baseurl}%
   \fi
\else
   {\catcode`\~=12
    \def\tilde{~}%
    \catcode`\~=\active
    \def~{$\sim$}%
    % default home URL
    \ifx\baseurl\undefined
         \xdef\baseurl{\wwwname/\noexpand~\username/}%
    \fi
    \xdef\baseurltext{\baseurl}%
    \let~\tilde
    \xdef\baseurlhref{http://\baseurl}%
   }
\fi

{%\def     \href#1#2{- see hyperref package -}
\xdef   \hreffmt#1#2{\noexpand\href{#1.\href@fmt}{#2}}%
\xdef    \hrefix#1#2{\noexpand\href{#1/index.\href@fmt}{#2}}%
\xdef    \pthref#1#2{\noexpand\href{\baseurlhref#1}{#2}}%
\xdef \pthreffmt#1#2{\noexpand\href{\baseurlhref#1.\href@fmt}{#2}}%
\xdef  \pthrefix#1#2{\noexpand\href{\baseurlhref#1/index.\href@fmt}{#2}}%
\xdef   \pthrefref#1{\noexpand\href{\baseurlhref#1}%
                               {\noexpand\urlfont{\baseurltext#1}}}%
\xdef\pthrefreffmt#1{\noexpand\href{\baseurlhref#1.\href@fmt}%
                               {\noexpand\urlfont{\baseurltext#1.\href@fmt}}}%
\xdef \pthrefrefix#1{\noexpand\href{\baseurlhref#1/index.\href@fmt}%
                               {\noexpand\urlfont{\baseurltext#1}}}%
}

%======================================================================
\iftth\else\makeatother\fi