Finding if a label is undefined

People seem to want to know (at run time) if a label is undefined (I don't actually understand why, particularly: it's a transient state, and LaTeX deals with it quite well).

A resolved label is simply a command: \r@<label-name>; determining if the label is set is then simply a matter of detecting if the command exists. The usual LaTeX internal way of doing this is to use the command \@ifundefined:

\@ifundefined{r@lab-name}{undef-cmds}{def-cmds}
In which, <lab-name> is exactly what you would use in a \label command, and the remaining two arguments are command sequences to be used if the label is undefined (<undef-cmds>) or if it is defined (def-cmds).

Note that any command that incorporates \@ifundefined is naturally fragile, so remember to create it with \DeclareRobustCommand or to use it with \protect in a moving argument.

If you're into this game, you may well not care about LaTeX's warning about undefined labels at the end of the document; however, if you are, include the command \G@refundefinedtrue in <undef-cmds>.

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=labundef