# How can I force a compile error if a ref does not exist?

I was curious if there was a way to force the compile to fail if a ref was not already defined. I do not want the ref to actually output anything into the document, but rather just check the existence condition. For example, I want to be able to do something like:

\begin{definition} \label{def C} \requires{def A} \requires{def B}
A \underline{C} is an A and a B such that...  etc etc etc.
\end{definition}


and I do not want the \requires{def A} to actually produce output but just enforce that there was somewhere above a \label{def A}. Ideally, you could use this for theorems and definitions. This way I can just add any dependencies in my theorems/definitions and not have to worry about whether or not I already defined everything above, the compiler will tell me. Does cleveref provide this? If not, how hard would it be to add it? I looked very briefly at the source code but I am not too familiar with the language so it would take me a while.

-
Usually just use \PackageError{<package name>}{<Error message>}{<help text (simply keep empty)>} –  Martin Scharrer Oct 19 '11 at 16:34
But the compiler already tells you if there were references that did not exist in the form of a warning: LaTeX Warning: There were undefined references. –  Werner Oct 19 '11 at 16:35

If you really want to enforce that the label is before the \requires, then you will have to redefine the \label command, as the current definition writes the label only to the aux-files and so its location relative to the \ref doesn't matter. E.g.

\documentclass{article}
\makeatletter
\let\Orilabel\label
\renewcommand\label[1]{\@namedef{mylabel@#1}{}\Orilabel{#1}}
\newcommand\require[1]{%
\ifcsname mylabel@#1\endcsname
\else
\@latex@error{Reference #1 undefined}
\fi}
\begin{document}
\section{A}
\label{A}

\require{A}\require{B}

\ref{A},\ref{B} \label{B}
\end{document}

-
Yes, this is exactly the sort of thing I am looking for... Do I need to restore that 'at' with a \makeatother after the newcommand? –  Tim Schumacher Oct 19 '11 at 18:04
I'd wager you do ;) –  mpr Oct 19 '11 at 18:25
@Tim: Yes you should use \makeatother. Nothing will explode if you forget it, but one should be orderly ;-). –  Ulrike Fischer Oct 19 '11 at 18:37
@UlrikeFischer: Thanks! The code seems to tell me my reference is undefined regardless of whether or not I put the label above. When I get some more time I will investigate... Thanks again for pointing me in the right direction! –  Tim Schumacher Oct 19 '11 at 19:47