%\filename{xrefmacros.tex}
%\edited{last by stolfi on Sat Mar  7 02:43:35 1987}

% SYMBOLIC CROSS REFERENCES

\catcode`@=11 \endlinechar=-1

  % Usage: To get equations automatically numbered from zero in each
  %   subsection, say
  %
  %    \newnumbering\Eq\Subsec{{\rm(#1)}}{{\rm #2 (1)}}
  %    \startnumbering\Eq\from{0}
  %
  %  then:
  %
  %     1. The command \hereis\Eq{Foo} takes the next available
  %        equation number (say, 14) and the current subsection (say 23.5)
  %        and associates them to the label "Foo".
  %
  %     2. Until the next \hereis\Eq (or next subsection), 
  %          \numberofcurrent\Eq will expand to 14.
  %          \contextofcurrent\Eq will expand to {\rm {\rm 23}.5}.
  %          \globalreftocurrent\Eq will expand to {\rm {\rm {\rm 23}. 5} (14)}
  %
  %     3. At any later time,
  %          \numberof\Eq{Foo} will expand to 14. 
  %          \contextof\Eq{Foo} will expand to {\rm {\rm 23}.5}.
  %          \localrefto\Eq{Foo} will expand to {\rm (14)}.
  %          \globalrefto\Eq{Foo} will expand to {\rm \contextof\Eq{Foo} (14)}.
  %          \Eq{Foo} will expand to 
  %             \localrefto\Eq{Foo} or \globalref\Eq{Foo}, depending on
  %             whether \globalreftocurrent\Subsec = \contextof\Eq{Foo}.
  %
  %  The commands \newnumbering\Eq\Subsec... and \hereis\Eq{Foo} define
  %    the following internal control sequences (assuming "-" could be
  %    a valid character in a control sequence name):
  %
  %       \newcount\Eq-count     the counter whose value is \currentnumber\Eq
  %       \def\Eq-parent{\Subsec}
  %       \def\Eq-first-number{0}
  %       \def\Eq-local-fmt#1#2{{\rm (#1)}}
  %       \def\Eq-global-fmt#1#2{{\rm #2 (#1)}}
  %       \def\Eq-Foo-num{14}
  %       \def\Eq-Foo-ctx{{\rm {\rm 23}.5}}
  %
  %   and, until the next \hereis\Eq, sets the tokenlist variables
  %
  %       \Eq-cur-ref to {{\rm {\rm {\rm 23}.5} (14)}} 
  %       \Eq-cur-ctx to {{\rm {\rm 23}.5}}
  %
  %   which keep the value of \globalreftocurrent\Eq and
  %     \contextofcurrent\Eq as they were just after the last \hereis\Eq{...}.
  %     The latter is the value of \globalreftocurrent\Subsec at that time.
  %     (Note that all thre macros now may be returning different values, 
  %     for example if the section or chapter have changed. Hovever, the
  %     variables above will eventually be brought up-to-date before being
  %     used.
  %
  
  % Concat names: \catname\Eq{-count} expands in two steps to \Eq-count
  
  \def\catname#1#2{\csname\string#1#2\endcsname} 
  
  % Twice-expand-after: \expexp\def\foo{Bar} expands \foo{Bar} twice,
  %   then puts back the \def in front of it and marches on.
  
  \def\expexp{\expandafter\expandafter\expandafter} 
  
  % \catdef\Eq{-baz}{XXX} is the same as \def\Eq-baz{XXX}
  
  \def\catgdef#1#2{\expexp\gdef\catname#1{#2}}
  \def\catxdef#1#2{\expexp\xdef\catname#1{#2}}
  
  \outer\def\newnumbering#1#2#3{
    \def#1##1{\r@fto#1{##1}}
    \catgdef#1{-parent}{#2}
    \expexp\newcount\catname#1{-count}
    \catgdef#1{-local-fmt}##1##2{##3}
    \catgdef#1{-global-fmt}##1##2{##4}
    \expexp\newtoks\catname#1{-cur-ref} \catname#1{-cur-ref}{{???}}
    \expexp\newtoks\catname#1{-cur-ctx} \catname#1{-cur-ctx}{{???}}
    \startnumbering#1{1}
    }
    
  \def\startnumbering#1#2{\catgdef#1{-first-number}{#2}}
  
  % The following macro resets an item counter and redefines its
  
  \def\hereis#1#2{
    \checkcontextof#1  % Update \Eq-cur-ctx, resetting \Eq-count if needed
    \expexp\advance\catname#1{-count} by 1
    \count6=\catname#1{-count}
    \expexp\ifdef\catname#1{-num-#2}
      \expexp\ifnum\catname#1{-num-#2}=\count6
      \else
        \message{Cross reference error for \string#1{#2}}
      \fi
    \else
      \catxdef#1{-num-#2}{\the\count6}
    \fi
    ~(output the definitions to the tag file)
    ~(update \Eq-cur-ref)
  }
  
  ~ Other macros.
  
JUNK::::::

% Cross-references (To improve!). Usage:
%    \dimsection{Topological braunschweigers.} \here\xr[Brau]
%    ...
%      ... as we saw before \xr[Brau], this is silly ....


  \def\here\xr[#1]{
     \expandafter\xdef\csname Xr@f#1\endcsname
       {\chapno.\number\sectionct.\number\subsectionct}
     {\hmode\vadjust{\vbox to 0pt{\vss\hbox to \hsize
       {\hss\hbox to 0pt{\kern 1em[#1]\hss}}
     }}}
     }

  \def\xr[#1]{
    \expandafter\ifx\csname Xr@f#1\endcsname\relax
      {$^{\hbox{\smallsize\rm[#1]}}$}
    \else
      {$^{\hbox{\smallsize\rm[\csname Xr@f#1\endcsname:#1]}}$}
    \fi}

\catcode`@=12 \endlinechar=`\^^M

% END
