[Tex/LaTex] Define command that returns true/false (multiple conditions)


While using the datatool-package I encountered something strange. I have a loop, and each iteration it defines some variables that are later used by the command \DTLforeach from the datatool-package. The command has an optional argument, those are conditions which are different for each iteration and thus need to be declared using a command (instead of copying the table 14 times).

I'll use the package ifthen instead of the datatool-package because it's more commonly used.



\def\condition{\equal{\One}{\One} \AND \equal{\Two}{\Two}}


\ifthenelse{\condition}{% if condition returns true
}{% if condition returns false


This results in an error:

! Undefined control sequence
<argument> \AND

l.11 \ifthenelse{\condition}
                            {% if condition returns true

However, it works if you copy-paste the contents of \condition into the first argument of \ifthenelse. Replacing \AND with its lowercase version \and doesn't work either. The error message just gets more complex, but it's still an undefined control sequence.

A possible solution is to define 2 separate conditions. This is however not a preferred way of doing it because the number of conditions is not always limited to 2, I'll not always use \AND, I'd have to iterate through them inside the \ifthenelse (which I don't know if possible), and so on. Also, it would make the code less clear, so if there is a possible way of defining several conditions in one variable that would be great.

(I also tried the \setboolean, but it only accepts the strings true or false and no real conditions)

Best Answer

It is an expansion issue. You should expand \condition before pass it to \ifthenelse. Use

\expandafter\ifthenelse\expandafter{\condition}{% if condition returns true
}{% if condition returns false

you will get correct result.

Related Question