%  TITLEREF.STY  ver 2.0  1996-09-16   Donald Arseneau   <asnd@triumf.ca>
%  This package provides a "\titleref" command to cross-reference the titles 
%  of sections.  The "\label" command is changed to remember the title for 
%  such a reference.  Use "\titleref" just like "\ref" and "\pageref".  
%
%  Titles for numbered sectioning units and for floats with captions are
%  the same as the respective toc, lof, or lot entry (even when no toc is
%  printed).  When a short title is provided ("\section[short]{long}")
%  it will be used for the "\titleref".  This is especially useful for
%  figure captions.
%
%  Unnumbered sections take their title reference from the printed
%  title.  These rely on redefined versions of the "\section*" command(s)
%  ("\@ssect" actually).  If there is a conflict with some other package,
%  you can specify "\usepackage[nostar]{titleref}" and the `star' versions
%  of sectioning commands will not generate a title reference.  If you try
%  to refer to the title of an unnumbered section while "[nostar]" is in
%  effect, you will get the title of the previous section.
%
%  The current title will always be overridden by an "\addcontentsline"
%  command before the "\label", even for starred section commands under
%  the "[nostar]" option.
%
%  Enumerated lists do not have titles and simply inherit the title of
%  their section.
%
%  An annoying restriction is that "\titleref" will not refer to a
%  "\label" which was given in the title or caption itself; the label
%  must be placed after the sectioning command or the caption.  If you
%  make a title-reference to a label given in a title, you will get a
%  warning message.  (This restriction is a by-product of hooking into
%  the toc mechanism.  I could hook into the sectioning and caption
%  commands directly, but they typically get redefined by other packages
%  and this would lead to conflicts; especially for captions.)
%
%  The format of the title reference is controlled by the command
%  "\theTitleReference", which can be redefined with "\renewcommand".
%  It takes two parameters: the number and the title. The number is
%  just the regular "\ref" and it is wrong in unnumbered sections.
%  Beware!  The default definition is the unadorned title.  You could
%  do, for example, "\renewcommand{\theTitleReference}[2]{#1\ \emph{#2}}".
%
%  The title of the current section is also available without using "\label".
%  Use the command "\currenttitle" to generate a "\titleref" to the current
%  section.  If you have redefined "\theTitleReference" to print the number
%  with the title, be aware that the two may not correspond:  In a numbered
%  list the number will show the current item but the title will show the
%  current section.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{titleref}[1996/02/16 v2.0 \space Cross-references with titles.]

\newcommand{\theTitleReference}[2]{#2}
%
\def\label#1{\@bsphack
  \protected@write\@auxout{}{\string\newlabel{#1}{{\protect
    \TR@TitleReference{\@currentlabel}{\TR@currentTitle}}{\thepage}}}%
  \@esphack}%
%
\def\addcontentsline#1#2#3{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thepage}}%
  \TR@gettitle{\relax#3}}
%
\def\TR@gettitle#1{\begingroup \let\protect\@unexpandable@protect
    \let\label\TR@nestwarn \let\index\@gobble \let\glossary\@gobble
    \edef\@tempa{\noexpand\def\noexpand\TR@currentTitle{#1}}%
  \expandafter\endgroup\@tempa}
%
\DeclareRobustCommand\TR@nestwarn[1]{\PackageWarning{titleref}%
  {Label \string"#1\string" was put in a heading,\MessageBreak
   so the \string\titleref\space is incorrect}}
%
\let\TR@TitleReference\@firstoftwo
%
% Allow aux file to be used after titleref is removed:
\AtBeginDocument{\immediate\write\@mainaux{\string\let
  \string\TR@TitleReference\string\@firstoftwo}}
%
\DeclareRobustCommand{\titleref}[1]{\begingroup \let\numberline\@gobble
  \let\TR@TitleReference\TR@theTR % interrupt recursion of \ref
  \ref{#1}\endgroup}
%
\DeclareRobustCommand{\currenttitle}{\begingroup \let\numberline\@gobble
  \theTitleReference\@currentlabel\TR@currentTitle\endgroup}
%
\def\TR@theTR{\let\TR@TitleReference\@firstoftwo\theTitleReference}
%
\DeclareOption{nostar}{\endinput}\ProcessOptions\relax
% \relax prevents \ProcessOptions from reading up to the \AtBeginDocument line,
% thus preventing \endinput from happening too late.
%
%  Put hooks in "star" commands
\AtBeginDocument{\let\TR@old@ssect\@ssect
  \def\@ssect#1#2#3#4#5{\TR@old@ssect{#1}{#2}{#3}{#4}{#5}\TR@gettitle{#5}}}
\@ifundefined{@schapter}{}{\AtBeginDocument{\let\TR@old@schapter\@schapter
  \def\@schapter#1{\TR@old@schapter{#1}\TR@gettitle{#1}}}}
\@ifundefined{@spart}{}{\AtBeginDocument{\let\TR@old@spart\@spart
  \def\@spart#1{\TR@old@spart{#1}\TR@gettitle{#1}}}}
