From: "Daniel H. Luecking" <DL24794@UAFSYSB.UARK.EDU>
Newsgroups: comp.text.tex
Subject: Re: Own counters with Labels/ref
Date: Mon, 04 Jan 93 15:47:23 CST
Organization: Info-Tex<==>Comp.Text.Tex Gateway
X-Gateway-Source-Info: Mailing List


> From: iwelch@agsm.ucla.edu (Ivo Welch)
> Subject: Own Counters with Labels/Ref
>
> I would like to use \ref on a counter that I define myself. That is,
>   I want to
> be able to say somewhere,
>
>         This refers to \ref{hi}.
>
>         <some text>
>         setcounter{myctr}{20}
>         \newlabel{hi}{{\arabic{myctr}}}
>
> it would print the contents of myctr where the label was processed, i.e.
>
>         This refers to 20.
>
> It  works if the label  comes before the reference,  not thereafter.
>    That is,
> the information is not written to the aux  file. This is probably very
>    simple,
> but I have tried many variations and didn't get it.
>
> /ivo welch
>
>
  I use the following macros. I work in plain TeX, but the principle should
be the same in LaTeX if you do not insist on using LaTeX's own \ref command.
Or perhaps you could read latex.tex to discover how to create a hook to
the \ref command.
  There is something truly amazing about the following macros that I would
like to mention: These are the first definitions I have ever written using
any of \xdef, \expandafter, or \csname...\endcsname, and yet THEY WORKED
ON THE FIRST TRY!  (If you had ever seen me debbuging a tex file,
you would see how amazing this is.)  But, there is something
EVEN MORE AMAZING: in their first incarnation they did not include the
\ifx  test in the \myref  macro, but when I had to revise my paper I
added it for debugging purposes.  It was my first use of an \ifx in
any tex file and IT TOO worked on the first try!  (What was NOT amazing
was the number of tries it took to get the message to print with a simple
space after the page number!)

%%%%%%%%%%%%%%%%%%%% Start of macro listing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Usage: \mylabel{foo} creates a new control word \fooref
%  which is defined to expand to the current theorem number.
%  Then \myref{foo} invokes that control word. I use \mylabel at any point
%  after the theorem in question but before the next theorem.
%
%  Substitute your own \count registers for \thmnum below
%  and invoke \mylabel when your \count  is the value you wish to
%  reference later.
%
\def\mylabel#1{\expandafter\xdef\csname#1ref\endcsname{\the\thmnum}}
%
%  Similar definition for equation labels. \eqnum is the appropriate
%  \count register here.
%
\def\eqlabel#1{\expandafter\xdef\csname#1ref\endcsname{\the\eqnum}}
%
%  \myref{foo} first checks whether \fooref was previously defined,
%  emits a not very informative message if it was not, and places
%  two question marks in the text. If \fooref was defined, it is invoked
%  to produce the appropriate number.
%
\def\myref#1{%
  \expandafter\ifx\csname#1ref\endcsname\relax
  \message{Reference on or just after page \the\pageno\space is undefined.}%
  ??%
  \else
  \csname#1ref\endcsname\fi}
%%%%%%%%%%%%%%%%%%%%% End of macro listing %%%%%%%%%%%%%%%%%%%%%%%%%

Caveat: This listing is an edited version of the file I use, so typos
may have crept in. Probably the TeXperts in netland will know of more
fool-proof macros. For example, \label{my} would change the meaning
of \myref. I had originally appended "@@@" instead of "ref" but this
caused problems in making revisions ("@@@" is hardly mnemonic).
As I am the only fool using these,  I decided not to worry
too much about making them fool-proof.

Remarks: (1)  \mylabel and \myeqlabel could be subsumed into
one macro by including a test for math mode and/or display mode
(2) Since I do not use LaTeX I do not have to worry about conflict of names
and I actually call these macros \label, \eqlabel and \ref, respectively.

Dan Luecking