Avoiding linebreak before number

Is there a way to avoid linebreaks before all numbers without explicitly using the tilde? It would be nice if it worked both for explicit numbers, as in Sample~1 as well as when a number is implied, as in Figure~\ref{fig:label}.

If not, I think I could write a macro in emacs that uses regex to find and fix all the instances of a number or \ref{} that should have a tilde.

-
have you looked at the cleveref package? it might be useful here (for the \ref part of your question)... – cmhughes Sep 23 '12 at 22:49
I looked it up, and cleveref seems to solve the problem, and more. As far as explicit numbers go, egreg makes some good points about cases where a linebreak before a number is fine. I think I'll use something like grep ' [0-9]' doc.tex to review these on a case by case basis, since they don't show up as often as references, at least in my writings. – Anthony Sep 24 '12 at 1:45

There's no practical way. One might think to make the digits active and define them as

\def1{\ifhmode\unskip~\fi\string1}


and similarly for the others. But this would prevent writing innocuos things such as

\hspace{1em}


and so this is ruled out.

Besides, ties are not always required in front of numbers:

as we have seen in cases 1~and~2


is better than cases~1 and~2.

Conversely one might redefine \ref so that it does the \unskip~ operation, but again this is not always the best way of writing by the same reason as before.

-