Paul Gaborit's answer to How to detect if option provided via \tikzset uses
\newbool
to define a new boolean\booltrue
to set a boolean, and\ifbool
to perform a conditional operation
all from the etoolbox
package.
Well, I have been using
\newtoggle
to define a new toggle\toggletrue
to set a toggle, and\iftoggle
to perform a conditional operation
also from the same package.
Is there a difference between the two? Is one preferable over the other?
Code:
\documentclass{article}
\usepackage{etoolbox}
\newtoggle{ToggleCondition}
\newbool{BooleanCondition}
\newcommand*{\CheckBoolean}{\ifbool{BooleanCondition}{true}{false}}
\newcommand*{\CheckToggle}{\iftoggle{ToggleCondition}{true}{false}}
\begin{document}
\toggletrue{ToggleCondition}
ToggleCondition is \CheckToggle.
\togglefalse{ToggleCondition}
ToggleCondition is \CheckToggle.
\medskip
\booltrue{BooleanCondition}
BooleanCondition is now \CheckBoolean.
\boolfalse{BooleanCondition}
BooleanCondition is now \CheckBoolean.
\end{document}
Best Answer
\newbool{mybool}
is almost the same as\newif
, but it checks whether\ifmybool
is already defined, which LaTeX doesn't:There's also
\providebool
which does nothing if the conditional is already defined.Similarly,
\ifbool
is a wrapper around the usual constructionwith a preliminary check whether the conditional is defined. So it can be used also for primitive conditionals:
Instead
\newtoggle{mytoggle}
is defined byso it checks whether the toggle is already defined and, if not, it sets it to "false". The constructed macro
\etb@tgl@mytoggle
is either\@firstoftwo
(corresponding to "true") or\@secondoftwo
(corresponding to "false"). When one saysetoolbox
simply checks whether the toggle is defined and, if it is, just puts\etb@tgl@mytoggle
in the input stream, thus doing "the right thing", because it chooses either<true>
or<false>
depending if it is\@firstoftwo
or\@secondoftwo
.Note that
\toggletrue
is defined asso it can be preceded by
\global
: indeed\ifcsdef
is expandable, so if the togglefoo
is not defined, after\global\toggletrue{foo}
we'd remain withwhich becomes
that effectively removes the
\global
by doing a harmless\let
(almost harmless, because it can impact on the stack memory, but since there's an error raised anyway, the user will correct it). If instead the toggle\foo
is defined, we remain withso, again,
where
\etb@toggletrue
is the same as\@firstoftwo
. The same holds, of course, for\togglefalse
.Which one to prefer? If you plan to use also the traditional
\if...
syntax, then\newbool
is needed. Otherwise it's just a matter of personal preference.Both booleans and toggles can be used in boolean expression (
\ifboolexpr
), so there's no difference for that usage either. Note that\global\booltrue
and\global\boolfalse
are legal too.