GeneralHow to check for existence of a cross-reference?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
knaldgas
Posts: 2
Joined: Sat Mar 20, 2010 2:58 pm

How to check for existence of a cross-reference?

Post by knaldgas »

I'm trying to achieve something along these lines:

Code: Select all

\newcommand{\checkref}[1]{%
  Label #1 is \ifthenelse{\defined{#1}}{defined}{not defined}%
}

\label{mylabel}
\checkref{mylabel}\\
\checkref{undefinedlabel}
Which should give (without warnings):
---
Label mylabel is defined
Label undefinedlabel is not defined
---
In short: How can I check if a label has previously been defined?
Last edited by knaldgas on Sat Mar 20, 2010 5:09 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

How to check for existence of a cross-reference?

Post by nlct »

You can check for the existence of \r@label where label is the name of the label:

Code: Select all

\documentclass{article}

\makeatletter
\newcommand{\checkref}[1]{%
  Label #1 is \@ifundefined{r@#1}{not defined}{defined}%
}
\makeatother

\begin{document}

\label{mylabel}
\checkref{mylabel}\\
\checkref{undefinedlabel}

\end{document}
You need a second run to ensure 'mylabel' is defined.

Regards
Nicola Talbot
knaldgas
Posts: 2
Joined: Sat Mar 20, 2010 2:58 pm

Re: How to check for existence of a cross-reference?

Post by knaldgas »

Great, that will work for me!
Thank you very much :D
Post Reply