3. Labels and References

LaTeX provides a powerful mechanism to deal with cross--references in a
document. When writing a document, any part of it can be marked with a
label, like `\label{mark}'. LaTeX records the current value of a
certain counter when a label is defined. Later references to this label
(like `\ref{mark}') will produce the recorded value of the
counter.

Labels can be used to mark sections, figures, tables, equations,
footnotes, items in enumerate lists etc. LaTeX is context sensitive in
doing this: A label defined in a figure environment automatically
records the figure counter, not the section counter.

Several different environments can share a common counter and therefore
a common label category. E.g. labels in both equation and
eqnarray environments record the value of the same counter - the
equation counter.

3.1 Creating Labels

In order to create a label in a LaTeX document, press C-c (
(reftex-label). Just like LaTeX, RefTeX is context sensitive
and will figure out the environment it currently is in and adapt the
label to that environment. A label usually consists of a short prefix
indicating the type of the label and a unique mark. RefTeX has
3 different modes to create this mark.

A label can be derived from context. This means, RefTeX takes
the context of the label definition and constructs a label from
that(1). This works best for section labels,
where the section heading is used to construct a label. In fact,
RefTeX's default settings use this method only for section
labels. You will be asked to confirm the derived label, or edit
it.

We may also use a simple unique number to identify a label. This is
mostly useful for labels where it is difficult to come up with a very
good descriptive name. RefTeX's default settings use this method
for equations, enumerate items and footnotes. The author of RefTeX
tends to write documents with many equations and finds it impossible
to come up with good names for each of them. These simple labels are
inserted without query, and are therefore very fast. Good descriptive
names are not really necessary as RefTeX will provide context to
reference a label (see section 3.2 Referencing Labels).

The third method is to ask the user for a label. This is most
useful for things which are easy to describe briefly and do not turn up
too frequently in a document. RefTeX uses this for figures and
tables. Of course, one can enter the label directly by typing the full
`\label{mark}'. The advantage of using reftex-label
anyway is that RefTeX will know that a new label has been defined.
It will then not be necessary to rescan the document in order to access
this label later.

If you want to change the way certain labels are created, check out the
variable reftex-insert-label-flags (see section 8.3 Creating Labels).

If you are using AUCTeX to write your LaTeX documents, you can
set it up to delegate the creation of labels to
RefTeX. See section 6.8 AUC TeX, for more information.

3.2 Referencing Labels

Referencing Labels is really at the heart of RefTeX. Press C-c
) in order to reference a label (reftex-reference). This will start a
selection process and finally insert the complete `\ref{label}'
into the buffer.

First, RefTeX will determine the label category which is required.
Often that can be figured out from context. For example, if you
write `As shown in eq.' and the press C-c ), RefTeX knows
that an equation label is going to be referenced. If it cannot figure
out what label category is needed, it will query for one.

You will then be presented with a label selection menu. This is a
special buffer which contains an outline of the document along with all
labels of the given label category. In addition, next to the label
there will be one line of context of the label definition, which is some
text in the buffer near the label definition. Usually this is
sufficient to identify the label. If you are unsure about a certain
label, pressing SPC will show the label definition point in
another window.

In order to reference a label, move to cursor to the correct label and
press RET. You can also reference several labels with a single
call to reftex-reference by marking entries with the m
key (see below).

Here is a list of special commands in the selection buffer. A summary
of this information is always available from the selection process by
pressing ?.

General

General

?
Show a summary of available commands.

0-9,-
Prefix argument.

Moving around

Moving around

n
Go to next label.

p
Go to previous label.

b
Jump back to the position where you last left the selection buffer.
Normally this should get you back to the last referenced label.

Displaying Context

Displaying Context

SPC
Show the surroundings of the definition of the current label in another
window. See also the f key.

f
Toggle follow mode. When follow mode is active, the other window will
always display the full context of the current label. This is similar
to pressing SPC after each cursor motion. Note that only context
in files already visited is shown. RefTeX will not visit a file
just for follow mode. See, however, the variable
reftex-revisit-to-follow.

.
Show insertion point in another window. This is the point from where you
called reftex-reference.

Selecting a label and creating the reference

Selecting a label and creating the reference

RET
Insert a reference to the label at point into the buffer from which the
selection process was started. When entries have been marked, RET
references all marked labels.

mouse-2
Clicking with mouse button 2 on a label will accept it like RET
would. See also variable reftex-highlight-selection, 8.11 Miscellaneous.

m - + ,
Mark the current entry. When several entries have been marked, pressing
RET will accept all of them and place them into several
\ref macros. The special markers `,-+' also store a
separator to be inserted before the corresponding reference. So marking
six entries with the keys `m , , - , +' will give a reference list
like this (see the variable reftex-multiref-punctuation)

In eqs. (1), (2), (3)--(4), (5) and (6)

u
Unmark a marked entry.

a
Accept the marked entries and put all labels as a comma-separated list
into one single\ref macro. Some packages like
`saferef.sty' support multiple references in this way.

l
Use the last referenced label(s) again. This is equivalent to moving to
that label and pressing RET.

TAB
Enter a label with completion. This may also be a label which does not
yet exist in the document.

v
Toggle between \ref and \vref macro for references. The
\vref macro is defined in the varioref LaTeX package.
With this key you can force RefTeX to insert a \vref
macro. The current state of this flag is displayed by the `S<>'
indicator in the mode line of the selection buffer.

V
Cycle between \ref, \fref and \Fref. The
\fref and \Fref macros are defined in the fancyref
LaTeX package. With this key you can force RefTeX to insert a
\fref or \Fref macro. The current state of this flag is
displayed by the `S<>' indicator in the mode line of the
selection buffer.

Exiting

Exiting

q
Exit the selection process without inserting any reference into the
buffer.

Controlling what gets displayed

Controlling what gets displayed
The defaults for the following flags can be configured with the variable
reftex-label-menu-flags (see section 8.4 Referencing Labels).

c
Toggle the display of the one-line label definition context in the
selection buffer.

F
Toggle the display of the file borders of a multifile document in the
selection buffer.

t
Toggle the display of the table of contents in the selection buffer.
With prefix arg, change the maximum level of toc entries displayed
to arg. Chapters are level 1, section are level 2.

#
Toggle the display of a label counter in the selection buffer.

%
Toggle the display of labels hidden in comments in the selection
buffers. Sometimes, you may have commented out parts of your document.
If these parts contain label definitions, RefTeX can still display
and reference these labels.

Updating the buffer

Updating the buffer

g
Update the menu. This will rebuilt the menu from the internal label
list, but not reparse the document (see r).

r
Reparse the document to update the information on all labels and rebuild
the menu. If the variable reftex-enable-partial-scans is
non-nil and your document is a multifile document, this will
reparse only a part of the document (the file in which the label at
point was defined).

C-u r
Reparse the entire document.

s
Switch the label category. After prompting for another label category,
a menu for that category will be shown.

x
Reference a label from an external document. With the LaTeX package
xr it is possible to reference labels defined in another
document. This key will switch to the label menu of an external
document and let you select a label from there (see section xr).

In order to define additional commands for the selection process, the
keymap reftex-select-label-map may be used.

3.3 Builtin Label Environments

RefTeX needs to be aware of the environments which can be referenced
with a label (i.e. which carry their own counters). By default, RefTeX
recognizes all labeled environments and macros discussed in The
LaTeX Companion by Goossens, Mittelbach & Samarin, Addison-Wesley
1994.. These are:

3.4 Defining Label Environments

RefTeX can be configured to recognize additional labeled
environments and macros. This is done with the variable
reftex-label-alist (see section 8.2 Defining Label Environments). If you are not familiar with Lisp, you can use the
custom library to configure this rather complex variable. To do
this, use

M-x customize-variable RET reftex-label-alist RET

Here we will discuss a few examples, in order to make things clearer.
It can also be instructive to look at the constant
reftex-label-alist-builtin which contains the entries for
all the builtin environments and macros (see section 3.3 Builtin Label Environments).

3.4.1 Theorem and Axiom Environments

Suppose you are using \newtheorem in LaTeX in order to define two
new environments, theorem and axiom

\newtheorem{axiom}{Axiom}
\newtheorem{theorem}{Theorem}

to be used like this:

\begin{axiom}
\label{ax:first}
....
\end{axiom}

So we need to tell RefTeX that theorem and axiom are new
labeled environments which define their own label categories. We can
either use Lisp to do this (e.g. in `.emacs') or use the custom
library. With Lisp it would look like this

The type indicator characters ?a and ?h are used for
prompts when RefTeX queries for a label type. ?h
was chosen for theorem since ?t is already taken by
table. Note that also ?s, ?f, ?e,
?i, ?n are already used for standard environments.

The labels for Axioms and Theorems will have the prefixes `ax:' and
`thr:', respectively. See section 6.8 AUC TeX, for information on how
AUCTeX can use RefTeX to automatically create labels when a new environment
is inserted into a buffer. Additionally, the following needs to be
added to one's .emacs file before AUCTeX will automatically create
labels for the new environments.

The following list of strings is used to guess the correct label type
from the word before point when creating a reference. E.g. if you
write: `As we have shown in Theorem' and then press C-c ),
RefTeX will know that you are looking for a theorem label and
restrict the menu to only these labels without even asking.

The final item in each entry is the level at which the environment
should produce entries in the table of context buffer. If the number is
positive, the environment will produce numbered entries (like
\section), if it is negative the entries will be unnumbered (like
\section*). Use this only for environments which structure the
document similar to sectioning commands. For everything else, omit the
item.

To do the same configuration with customize, you need to click on
the [INS] button twice to create two templates and fill them in
like this:

Depending on how you would like the label insertion and selection for
the new environments to work, you might want to add the letters `a'
and `h' to some of the flags in the variables
reftex-insert-label-flags (see section 8.3 Creating Labels)
and reftex-label-menu-flags (see section 8.4 Referencing Labels).

3.4.2 Quick Equation Macro

Suppose you would like to have a macro for quick equations. It
could be defined like this:

\newcommand{\quickeq}[1]{\begin{equation} #1 \end{equation}}

and used like this:

Einstein's equation is \quickeq{E=mc^2 \label{eq:einstein}}.

We need to tell RefTeX that any label defined in the argument of the
\quickeq is an equation label. Here is how to do this with lisp:

(setq reftex-label-alist '(("\\quickeq{}" ?e nil nil 1 nil)))

The first element in this list is now the macro with empty braces as an
image of the macro arguments. ?e indicates that this is
an equation label, the different nil elements indicate to use the
default values for equations. The `1' as the fifth element
indicates that the context of the label definition should be the 1st
argument of the macro.

Now we need to tell RefTeX that the 4th argument of the
\myfig macro is itself a figure label, and where to find
the context.

(setq reftex-label-alist
'(("\\myfig[]{}{}{*}{}" ?f nil nil 3)))

The empty pairs of brackets indicate the different arguments of the
\myfig macro. The `*' marks the label argument. ?f
indicates that this is a figure label which will be listed together with
labels from normal figure environments. The nil entries for
prefix and reference format mean to use the defaults for figure labels.
The `3' for the context method means to grab the 3rd macro argument
- the caption.

As a side effect of this configuration, reftex-label will now
insert the required naked label (without the \label macro) when
point is directly after the opening parenthesis of a \myfig macro
argument.

3.4.4 Adding Magic Words

Sometimes you don't want to define a new label environment or macro, but
just change the information associated with a label category. Maybe you
want to add some magic words, for another language. Changing only the
information associated with a label category is done by giving
nil for the environment name and then specify the items you want
to define. Here is an example which adds German magic words to all
predefined label categories.

3.4.5 Using \eqref

Another case where one only wants to change the information associated
with the label category is to change the macro which is used for
referencing the label. When working with the AMS-LaTeX stuff, you might
prefer \eqref for doing equation references. Here is how to
do this:

(setq reftex-label-alist '((nil ?e nil "~\\eqref{%s}" nil nil)))

RefTeX has also a predefined symbol for this special purpose. The
following is equivalent to the line above.

(setq reftex-label-alist '(AMSTeX))

Note that this is automatically done by the `amsmath.el' style file
of AUCTeX (see section 6.8.2 Style Files) - so if you use AUCTeX,
this configuration will not be necessary.

3.4.6 Non-standard Environments

Some LaTeX packages define environment-like structures without using the
standard `\begin..\end' structure. RefTeX cannot parse
these directly, but you can write your own special-purpose parser and
use it instead of the name of an environment in an entry for
reftex-label-alist. The function should check if point is
currently in the special environment it was written to detect. If so,
it must return a buffer position indicating the start of this
environment. The return value must be nil on failure to detect
the environment. The function is called with one argument bound.
If non-nil, bound is a boundary for backwards searches
which should be observed. We will discuss two examples.

Some people define abbreviations for
environments, like \be for \begin{equation}, and
\ee for \end{equation}. The parser function would have
to search backward for these macros. When the first match is
\ee, point is not in this environment. When the first match is
\be, point is in this environment and the function must return
the beginning of the match. To avoid scanning too far, we can also look
for empty lines which cannot occur inside an equation environment.
Here is the setup:

A more complex example is the `linguex.sty' package which defines
list macros `\ex.', `\a.', `\b.' etc. for lists which are
terminated by `\z.' or by an empty line.

\ex. \label{ex:12} Some text in an exotic language ...
\a. \label{ex:13} more stuff
\b. \label{ex:14} still more stuff
\a. List on a deeper level
\b. Another item
\b. and the third one
\z.
\b. Third item on this level.
... text after the empty line terminating all lists

The difficulty is that the `\a.' lists can nest and that an empty
line terminates all list levels in one go. So we have to count nesting
levels between `\a.' and `\z.'. Here is the implementation
for RefTeX.

3.4.7 Putting it all together

When you have to put several entries into reftex-label-alist, just
put them after each other in a list, or create that many templates in
the customization buffer. Here is a lisp example which uses several of
the entries described above:

3.5 Reference Info

When point is idle for more than reftex-idle-time seconds on the
argument of a \ref macro, the echo area will display some
information about the label referenced there. Note that the information
is only displayed if the echo area is not occupied by a different
message.

RefTeX can also display the label definition corresponding to a
\ref macro, or all reference locations corresponding to a
\label macro. See section 6. Viewing Cross--References, for more
information.

and we can make references to any labels defined in these
external documents by using the prefixes `V1-' and `V3-',
respectively.

RefTeX can be used to create such references as well. Start the
referencing process normally, by pressing C-c ). Select a label
type if necessary. When you see the label selection buffer, pressing
x will switch to the label selection buffer of one of the external
documents. You may then select a label as before and RefTeX will
insert it along with the required prefix.

For this kind of inter-document cross-references, saving of parsing
information and the use of multiple selection buffers can mean a large
speed-up (see section 6.7 Optimizations).

3.7 varioref: Variable Page References

varioref is a frequently used LaTeX package to create
cross--references with page information. When you want to make a
reference with the \vref macro, just press the v key in the
selection buffer to toggle between \ref and \vref
(see section 3.2 Referencing Labels). The mode line of the selection buffer
shows the current status of this switch. If you find that you almost
always use \vref, you may want to make it the default by
customizing the variable reftex-vref-is-default. If this
toggling seems too inconvenient, you can also use the command
reftex-varioref-vref(2).
Or use AUCTeX to create your macros (see section 6.8 AUC TeX).

3.8 fancyref: Fancy Cross References

fancyref is a LaTeX package where a macro call like
\fref{fig:map-of-germany} creates not only the number of
the referenced counter but also the complete text around it, like
`Figure 3 on the preceding page'. In order to make it work you
need to use label prefixes like `fig:' consistently - something
RefTeX does automatically. When you want to make a reference
with the \fref macro, just press the V key in the selection
buffer to cycle between \ref, \fref and \Fref
(see section 3.2 Referencing Labels). The mode line of the selection buffer
shows the current status of this switch. If this cycling seems
inconvenient, you can also use the commands reftex-fancyref-fref
and reftex-fancyref-Fref(3). Or use AUCTeX to create your macros
(see section 6.8 AUC TeX).