# “Missing Number” error when using “switch” in “boolexpr”

\pdfoutput=1
\documentclass[12pt]{amsart}
\usepackage{boolexpr}

\def \p{1}
\def \q{1}

\begin{document}
\switch[\p=]
\case{\q}abc
\endswitch
def
\end{document}

Compiling the above I get the following error

! Missing number, treated as zero.
\bex@=0 truepart
l.12 \endswitch

But if we change the definition to

\def \q{2}

or change the line to

\switch[1=]

it will be OK. But only change the line to

\case{1}abc

yields the same error.

Any idea why this happens? Thank you in advance.

-
@Herbert has given a good answer, which solves the issue, but I'd say this might be regarded as a bug in boolexpr. I suggest you report it to the package author. –  Joseph Wright Dec 29 '11 at 9:46

It seems not to be well documented, but the examples show that a counter value is expected. You can fool the package by using \numexpr:

\switch[\numexpr\p\relax=]
\case{\q} abc
\endswitch

The problem is that one has to "announce" boolexpr the type of test to be performed in some way; also

\switch[\number\p=]

works.

-

\documentclass{amsart}
\usepackage{boolexpr}
\newcounter{p}\setcounter{p}{1}
\newcounter{q}\setcounter{q}{1}

\begin{document}
\switch[\value{p}=]
\case{\value{q}} abc
\endswitch
def
\end{document}
-
Only p must be declared as a counter ;-) –  Marco Daniel Dec 29 '11 at 9:59
Yes, that's quite strange. It seems that the expression before the equality sign must not be a macro. Using \value is a bit cumbersome, and I cannot even define a \v to replace it because that will yield the same error. Anyway, the answer doesn't give insight why this happens. –  Junyan Xu Dec 29 '11 at 10:07
I now find \pdfstrcmp more suitable for my purpose. I originally want to encode strings into numbers so that I can compare them, but it's no longer necessary. Still want an explanation to the curious behavior of boolexpr. Won't TeX expand out the macros before compilation? What's different between macros and numbers? –  Junyan Xu Dec 29 '11 at 10:23
@Marco: true, but it doesn't hurt :-) –  Herbert Dec 29 '11 at 10:44
@JunyanXu: What's different between macros and numbers? I think this is a new interesting question. –  Marco Daniel Dec 29 '11 at 10:48