[Tex/LaTex] How to create *local* labels/refs in LaTeX

cross-referencinggroupingnamingtheorems

Concise question:

Is there any way to limit the scope in which a \label is visible to a \ref?

Motivation:

My current goal is to create a rather large document, containing many exercises and their associated proofs/solutions.

  • I'd like to be able to refer to any proof that I've completed before the current one. (i.e., if I've proved it, I wanna be able to use it!)
  • I'd like to be able to accumulate observations within the context of a proof and subsequently refer to them later in that proof. However, those observations (or assumptions, or introductions, etc.) don't need to be visible outside of that proof.

For example, in the following image of a sample Structured Derivation, I'd like to be able to label assumption #5 something like \label{x_is_nonneg}. But if I do that without any special packages or TeX-hackery, I'm unable to use "x_is_nonneg" as a label for any other object for the rest of the document, right? Which is too bad, because I'll often want to make very context-specific assertions, and it would be nice if I didn't have to name them something like \label{exer:8:asmp:5}.

a proof demonstrating local references

Best Answer

A solution: define two macros to add automatically a local prefix to your labels and references.

%%%%%%% in your preamble %%%%%%%%
% macro to define a local label
\newcommand\locallabel[1]{\label{\currentprefix:#1}}

% macro to use a local reference
\newcommand\localref[1]{\ref{\currentprefix:#1}}


%%%%%% in your document %%%%%%
% define a current prefix whenever you start a new proof
{
   \def\currentprefix{proof:10} % choose a different prefix for each proof!
   ...
   \locallabel{x_is_nonneg}
   ...
   \localref{x_is_nonneg}
}
Related Question