# 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}.

-
 I'd say, the solution depends on your editor, at least a bit. Usually the editor offers completions of references. If this mechanism can not be adapted as well, any answer will not really help you! So: which editor are you using? – Keks Dose Oct 24 '12 at 14:21

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}}

% 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}
}

-

You could introduce the following new commands

\newcommand*\labellocal[1]{\label{exer:\theexample:#1}}
\newcommand*\reflocal[1]{\ref{exer:\theexample:#1}}
\newcommand*\eqreflocal[1]{exer:\eqref{\theexample:#1}


and use it as \labellocal{x_is_nonneg} and refer to it with \reflocal{x_is_nonneg} or \eqreflocal{x_is_nonneg}.

Internally (globally) the label would be exer:8:x_is_nonneq.
You can, of course, use more distinguishing commands like \thesection to have a more local label style.

-