# Asserting reference inequality

Is there a way for including assertions in a LaTeX document? I want to check that some condition is true, and only allow compilation of the document if it is (or at least complain otherwise).

Specifically, a paragraph contains a reference to a chapter. Before compiling the final version, I'd like to be warned if the chapter is the current chapter, so that I could e.g. substitute the text with "in this chapter".

-

Here is an idea that goes a bit beyond your question. You can do this with the ifthen package and by defining a new command. The command \mychapterref{#1} checks if the argument (a reference) is the current chapter. If it is, it writes out the string this chapter, otherwise it writes out Chapter~\ref{#1}.

\documentclass{report}
\usepackage{ifthen}

\begin{document}

\newcommand{\mychapterref}[1]{%
\ifthenelse{\equal{\ref{#1}}{\arabic{chapter}}}{this chapter}{Chapter~\ref{#1}}
}

\chapter{begin}\label{chap1}
look in \mychapterref{chap1}

\chapter{next}
look in \mychapterref{chap1}

\end{document}


The code above outputs

look in this chapter

in Chapter 1 and

look in Chapter 1

in other chapters.

-
Does this solution constrain me in the way I have to use labels? Would this still work if I had used the label \label{chapter:begin}? – Christian Lindig Mar 5 '11 at 6:13
@Christian: 1. no, 2. yes - just test it! – Hendrik Vogt Mar 5 '11 at 8:19
I've taken the liberty to try and improve your nice answer a bit. A tip: you can use backticks  to mark your inline code. (Note the ~ and the %` I added; the latter prevents the space that your command inserted in the beginning.) – Hendrik Vogt Mar 5 '11 at 8:22
Thanks for solving my immediate problem. I posted another question for the other part of my question. – Tim N Mar 5 '11 at 10:10
@Tim: Happy to help! I have been helped often by solutions on this site so it was nice to be able to help someone else! @Hendrik: thanks for the tips... My first time here! – jaydev Mar 5 '11 at 15:00