[Tex/LaTex] Testing for a “if not defined” boolean

conditionals

How can I test for a not defined boolean?

I want to have

\usepackage{xr-hyper}
\usepackage{hyperref}

\providebool{is_single_book}
\booltrue{is_single_book}

in my main.tex and in each of the chapter TeX files.

if not defined is_single_book
  \externaldocument{volume1}[http://mybook.com/volume1.pdf]
fi

So when I compile the entire book, \externaldocument{volume1}[http://mybook.com/volume1.pdf] isn't executed.

It doesn't have to be a boolean if something else is simpler or doesn't require an extra package.

Best Answer

While you could check if a bool is defined by using \ifcsundef{if...} (see comment from moewe), it looks as if you actually want a boolean which is always false if you are in a chapter tex file.

This can be archived by using \providebool, which defines the bool as false, iff is is not defined already:

Use

\newbool{is_single_book}
\booltrue{is_single_book}

in your main file and

\providebool{is_single_book}
\ifbool{is_single_book}{}{%
  \externaldocument{volume1}[http://mybook.com/volume1.pdf]%
}

in your chapter files.

Even better would be to use toggles. They are almost the same but avoid name clashes and provide slightly better performance:

To use them, insert

\newtoggle{is_single_book}
\toggletrue{is_single_book}

in your main file and

\providetoggle{is_single_book}
\iftoggle{is_single_book}{}{%
  \externaldocument{volume1}[http://mybook.com/volume1.pdf]%
}

in the chapters.

Related Question