GeneralConditional output depending on a label being defined?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Conditional output depending on a label being defined?

Post by robertjlee »

Hi.

I'm working on a LaTeX package, which re-orders text in a document. It adds a line of text like "continued on page~\pageref{to#1}" when a block of text is moved.

Unfortunately, it doesn't guarantee that \label{to#1} will be defined in the document. Is there any way (without manually parsing the .aux file) to hide the line of text if the label is not defined?

Many thanks

Yours,

Robert Lee.

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Conditional output depending on a label being defined?

Post by Juanjo »

A label called foo gives rise to a command called \r@foo. You can check if this command is defined through the conditional \@ifundefined. Look at the following example:

Code: Select all

\documentclass{article}

\makeatletter
\newcommand{\testlabel}[1]{%
  \@ifundefined{r@#1}%
     {Label #1 is not defined}%
     {Label #1 is associated to the value \ref{#1} and the page \pageref{#1}}}%
\makeatother

\begin{document}

\section{A section}\label{sec:1}

Text, more text and an equation:
\begin{equation}\label{eq:1}
  a+b=c.
\end{equation}

\section{Other section}\label{sec:2}

Text, more text and a new equation:
\begin{equation}\label{eq:2}
  d+e=f.
\end{equation}

\newpage

\section{Last section}\label{sec:3}

Now we test several labels:\\
\testlabel{sec:1}\\
\testlabel{sec:2}\\
\testlabel{eq:1}\\
\testlabel{eq:2}\\
\testlabel{sec:3}\\
\testlabel{eq:3}.

\end{document}
Pay special attention to the second page of the resulting document. Compare its content after the first and the second run. If you don't see the difference, remove the .aux file and try again.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Re: Conditional output depending on a label being defined?

Post by robertjlee »

Juanjo,

Thanks a lot; that really worked.

Yours,

Robert Lee
Post Reply