ltxref.dtx

Johannes Braams  David Carlisle  Alan Jeffrey
Leslie Lamport  Frank Mittelbach  Chris Rowley

Rainer Schöpf

1995/12/07

1 Cross Referencing

The user writes \label{⟨foo⟩} to define the following cross-references:

\ref{⟨foo⟩}: value of most recently incremented referencable counter. in the current environment. (Chapter, section, theorem and enumeration counters counters are referencable, footnote counters are not.)

\pageref{⟨foo⟩}: page number at which \label{foo} command appeared. where foo can be any string of characters not containing ‘\’, ‘{’ or ‘}’.

Note: The scope of the \label command is delimited by environments, so

\begin{theorem} \label{foo} ... \end{theorem} \label{bar}

defines \ref{foo} to be the theorem number and \ref{bar} to be the current section number.

Note: \label does the right thing in terms of spacing – i.e., leaving a space on both sides of it is equivalent to leaving a space on either side.

1.1 Cross Referencing

1 ⟨∗2ekernel⟩

 2 \message{x-ref,}

This is implemented as follows. A referencable counter CNT is incremented by the command \refstepcounter{CNT} , which sets

  \@currentlabel == {CNT}{eval(\p@cnt\theCNT)}. The command

\label{FOO} then writes the following on file \@auxout :

        \newlabel{FOO}{{eval(\@currentlabel)}{eval(\thepage)}}
  \ref{FOO} ==

BEGIN

if \r@foo undefined

then @refundefined := G T ?? Warning: ’reference foo on page ... undefined’

        else \@car \eval(\r@FOO)\@nil

fi END

  \pageref{foo} =

BEGIN

if \r@foo undefined

then @refundefined := G T ?? Warning: ’reference foo on page ... undefined’

        else \@cdr \eval(\r@FOO)\@nil

fi END

\G@refundefinedtrue \@refundefined This does not save on name-space (since \G@refundefinedfalse was never

needed) but it does make the implmentation of such one-way switches more consistent. The extra macro to make the change is used since this change appears several times.

Note despite its name, \G@refundefinedtrue does not correspnd to an \if command, and there is no matching … false. It would be more natural to call the command \G@refundefined (as inspection of the change log will reveal) but unfortunately such a change would break any package that had defined a \ref-like command that mimicked the definition of \ref, calling \G@refundefinedtrue. Inspection of the TEX archives revealed several such packages, and so this command has been named … true so that the definition of \ref need not be changed, and the packages will work without change.

 3 % \newif\ifG@refundefined
 4 % \def\G@refundefinedtrue{\global\let\ifG@refundefined\iftrue}
 5 % \def\G@refundefinedfalse{\global\let\ifG@refundefined\iffalse}
 6 \def\G@refundefinedtrue{%
 7 \gdef\@refundefined{%
 8  \@latex@warning@no@line{There were undefined references}}}
 9 \let\@refundefined\relax

\ref \pageref \@setref Referencing a \label. RmS 91/10/25: added a few extra \reset@font, as sug-

gested by Bernd Raichle RmS 92/08/14: made \ref and \pageref robust RmS 93/09/08: Added setting of refundefined switch.

10 \def\@setref#1#2#3{%
11 \ifx#1\relax
12 \protect\G@refundefinedtrue
13 \nfss@text{\reset@font\bfseries ??}%
14 \@latex@warning{Reference `#3' on page \thepage \space
15  undefined}%
16 \else
17 \expandafter#2#1\null

18 \fi}
19 \def\ref#1{\expandafter\@setref\csname r@#1\endcsname\@firstoftwo{#1}}
20 \def\pageref#1{\expandafter\@setref\csname r@#1\endcsname
21  \@secondoftwo{#1}}

\newlabel This command will be written to the .aux file to pass label information from one run to another.

\@newl@bel The internal form of \newlabel and \bibcite.
22 \def\@newl@bel#1#2#3{%
23 \@ifundefined{#1@#2}%
24  \relax
25  {\gdef \@multiplelabels {%
26  \@latex@warning@no@line{There were multiply-defined labels}}%
27  \@latex@warning@no@line{Label `#2' multiply defined}}%
28 \global\@namedef{#1@#2}{#3}}
29 \def\newlabel{\@newl@bel r}
30 \@onlypreamble\@newl@bel

\if@multiplelabels \@multiplelabels This is redefined to produce a warning if at least one label is defined more than once. It is executed by the \enddocument command.

31 \let \@multiplelabels \relax
\label \refstepcounter The commands \label and \refstepcounter have been changed to allow

\protect’ed commands to work properly. For example,

   \def\thechapter{\protect\foo{\arabic{chapter}.\roman{section}}}

will cause a \label{bar} command to define \ref{bar} to expand to something like \foo{4.d}. Change made 20 Jul 88.

32 \def\label#1{\@bsphack
33 \protected@write\@auxout{}%
34  {\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
35 \@esphack}
36 \def\refstepcounter#1{\stepcounter{#1}%
37  \protected@edef\@currentlabel
38  {\csname p@#1\endcsname\csname the#1\endcsname}%

39 }

\@currentlabel For \label commands that come before any environment

40 \def\@currentlabel{}

41 ⟨/2ekernel⟩