\providebool
and \booltrue
makes me think you are using etoolbox
.
etoolbox
booleans are just a thin wrapper around TeX's \newif
.
That means that \newbool{<name>}
defines \if<name>
, so you can check if a boolean is defined by checking if \if<name>
is defined.
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{etoolbox}
\newcommand*{\ifboolundef}[1]{\ifcsundef{if#1}}
\newcommand*{\sabooleantest}[1]{%
bool \ttfamily{#1} is
\ifboolundef{#1}
{undefined}
{\ifbool{#1}
{true}
{false}}}
\begin{document}
\sabooleantest{flobbel}
\newbool{globbel}
\boolfalse{globbel}
\sabooleantest{globbel}
\newbool{hobbel}
\booltrue{hobbel}
\sabooleantest{hobbel}
\end{document}
bool flobbel
is undefined
bool globbel
is false
bool hobbel
is true
The question is whether you really need something that comes down to a three-valued logic. Is a two-valued boolean not enough?
If you want to avoid problems in case the boolean is undefined, you could just check if a macro is undefined or defined. This also has two states, but unlike with booleans there are no errors if the thing is not defined.
\newcommand*{\mybool}{}
\ifundef{\mybool}
{not defined}
{defined}
You could also define 'permissive toggles' that are false if they don't exist
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{etoolbox}
\newcommand*{\permissivetoggletrue}[1]{\csletcs{sa@ptogl@#1}{@empty}}
\newcommand*{\permissivetogglefalse}[1]{\csundef{sa@ptogl@#1}}
\newcommand*{\ifpermissivetoggle}[1]{\ifcsdef{sa@ptogl@#1}}
\begin{document}
flobbel
\ifpermissivetoggle{flobbel}
{true}
{false}
\permissivetoggletrue{globbel}
globbel
\ifpermissivetoggle{globbel}
{true}
{false}
\end{document}
This can also be done without etoolbox
if you like those sort of things
\makeatletter
\newcommand*{\permissivetoggletrue}[1]{%
\expandafter\let\csname sa@ptogl@#1\endcsname\@empty}
\newcommand*{\permissivetogglefalse}[1]{%
\expandafter\let\csname sa@ptogl@#1\endcsname\@sa@undef}% this assumes \@sa@undef is undefined
\newcommand*{\ifpermissivetoggle}[1]{%
\ifcsname sa@ptogl@#1\endcsname
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother
etoolbox
commands you can use\ifcsundef{ifis_single_book}{<undef>}{<def>}
to check if the booleanis_single_book
is undefined. Note that\providebool
makes sure the boolean is defined, so\ifcsundef{ifis_single_book}
will execute the false branch.