home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / text / tex / 14602 < prev    next >
Encoding:
Text File  |  1993-01-04  |  3.9 KB  |  99 lines

  1. Path: sparky!uunet!gatech!rpi!usc!news.service.uci.edu!network.ucsd.edu!mvb.saic.com!info-tex
  2. From: "Daniel H. Luecking" <DL24794@UAFSYSB.UARK.EDU>
  3. Newsgroups: comp.text.tex
  4. Subject: Re: Own counters with Labels/ref
  5. Message-ID: <9910816@MVB.SAIC.COM>
  6. Date: Mon, 04 Jan 93 15:47:23 CST
  7. Organization: Info-Tex<==>Comp.Text.Tex Gateway
  8. X-Gateway-Source-Info: Mailing List
  9. Lines: 88
  10.  
  11.  
  12. > From: iwelch@agsm.ucla.edu (Ivo Welch)
  13. > Subject: Own Counters with Labels/Ref
  14. >
  15. > I would like to use \ref on a counter that I define myself. That is,
  16. >   I want to
  17. > be able to say somewhere,
  18. >
  19. >         This refers to \ref{hi}.
  20. >
  21. >         <some text>
  22. >         setcounter{myctr}{20}
  23. >         \newlabel{hi}{{\arabic{myctr}}}
  24. >
  25. > it would print the contents of myctr where the label was processed, i.e.
  26. >
  27. >         This refers to 20.
  28. >
  29. > It  works if the label  comes before the reference,  not thereafter.
  30. >    That is,
  31. > the information is not written to the aux  file. This is probably very
  32. >    simple,
  33. > but I have tried many variations and didn't get it.
  34. >
  35. > /ivo welch
  36. >
  37. >
  38.   I use the following macros. I work in plain TeX, but the principle should
  39. be the same in LaTeX if you do not insist on using LaTeX's own \ref command.
  40. Or perhaps you could read latex.tex to discover how to create a hook to
  41. the \ref command.
  42.   There is something truly amazing about the following macros that I would
  43. like to mention: These are the first definitions I have ever written using
  44. any of \xdef, \expandafter, or \csname...\endcsname, and yet THEY WORKED
  45. ON THE FIRST TRY!  (If you had ever seen me debbuging a tex file,
  46. you would see how amazing this is.)  But, there is something
  47. EVEN MORE AMAZING: in their first incarnation they did not include the
  48. \ifx  test in the \myref  macro, but when I had to revise my paper I
  49. added it for debugging purposes.  It was my first use of an \ifx in
  50. any tex file and IT TOO worked on the first try!  (What was NOT amazing
  51. was the number of tries it took to get the message to print with a simple
  52. space after the page number!)
  53.  
  54. %%%%%%%%%%%%%%%%%%%% Start of macro listing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  55. %
  56. %  Usage: \mylabel{foo} creates a new control word \fooref
  57. %  which is defined to expand to the current theorem number.
  58. %  Then \myref{foo} invokes that control word. I use \mylabel at any point
  59. %  after the theorem in question but before the next theorem.
  60. %
  61. %  Substitute your own \count registers for \thmnum below
  62. %  and invoke \mylabel when your \count  is the value you wish to
  63. %  reference later.
  64. %
  65. \def\mylabel#1{\expandafter\xdef\csname#1ref\endcsname{\the\thmnum}}
  66. %
  67. %  Similar definition for equation labels. \eqnum is the appropriate
  68. %  \count register here.
  69. %
  70. \def\eqlabel#1{\expandafter\xdef\csname#1ref\endcsname{\the\eqnum}}
  71. %
  72. %  \myref{foo} first checks whether \fooref was previously defined,
  73. %  emits a not very informative message if it was not, and places
  74. %  two question marks in the text. If \fooref was defined, it is invoked
  75. %  to produce the appropriate number.
  76. %
  77. \def\myref#1{%
  78.   \expandafter\ifx\csname#1ref\endcsname\relax
  79.   \message{Reference on or just after page \the\pageno\space is undefined.}%
  80.   ??%
  81.   \else
  82.   \csname#1ref\endcsname\fi}
  83. %%%%%%%%%%%%%%%%%%%%% End of macro listing %%%%%%%%%%%%%%%%%%%%%%%%%
  84.  
  85. Caveat: This listing is an edited version of the file I use, so typos
  86. may have crept in. Probably the TeXperts in netland will know of more
  87. fool-proof macros. For example, \label{my} would change the meaning
  88. of \myref. I had originally appended "@@@" instead of "ref" but this
  89. caused problems in making revisions ("@@@" is hardly mnemonic).
  90. As I am the only fool using these,  I decided not to worry
  91. too much about making them fool-proof.
  92.  
  93. Remarks: (1)  \mylabel and \myeqlabel could be subsumed into
  94. one macro by including a test for math mode and/or display mode
  95. (2) Since I do not use LaTeX I do not have to worry about conflict of names
  96. and I actually call these macros \label, \eqlabel and \ref, respectively.
  97.  
  98. Dan Luecking
  99.