6

I have a (recursive) function that accepts a bool as an argument and first copies this to some local boolean variable (using grouping so the value is restored after the function call). When making the recursion call, it passes the (possibly modified) value of this variable on.

Strangely, this results in a missing number treated as zero error when setting the boolean in the next recursion step.

I made this down to the following minimal working example:

\documentclass{article}

\begin{document}


\ExplSyntaxOn

\bool_new:N \my_bool

\bool_set_true:N \my_bool

\bool_set:Nn \my_bool { \my_bool }

\ExplSyntaxOff

\end{document}

This tries to set the \my_bool to itself. I would expect this to just do nothing.

So, why does this result in such an error? From the manual I conclude that \my_bool is a valid <boolean expression>, and also setting \l_tmpa_bool to this expression does the job, the misbehaviour seems to come from the self-reference.

Is there some other boolean set function that I should use for such a case? Or is there a better way to recursively pass on this boolean value that I have in my local variable?

1

1 Answer 1

6

Booleans are implemented as \chardef tokens and \bool_set:Nn is, under the hood, \chardef. Among the rules of TeX is that when performing

\chardef\cs=<number>

the \cs is temporarily set to \relax. This is to avoid things such as

\def\cs{100}
\chardef\cs=10\cs

See page 278 of the TeXbook.

So the answer is that the boolean you want to set cannot appear in the <boolean expression> in the argument to \bool_set:Nn, because at the time the expression is being evaluated the control sequence does not stand for a boolean.

In the particular case, the code eventually becomes, due to how the business is implemented,

\chardef\my_bool=\my_bool

which is illegal in TeX.

Workaround:

\bool_set:Nn \my_bool { \my_bool && \c_true_bool }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .