Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

\documentclass{article}
\usepackage{ifthen}

\def\One{abc}
\def\Two{cde}

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

\begin{document}

\ifthenelse{\condition}{% if condition returns true
  True.
}{% if condition returns false
  False.
}

\end{document}

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)

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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

\expandafter\ifthenelse\expandafter{\condition}{% if condition returns true
  True.
}{% if condition returns false
  False.
}

you will get correct result.

share|improve this answer
    
Thought it had to be something like that. Problem is I don't understand the command \expandafter exactly, and certainly not with commands. Mind telling me how to organize the \expandafters if I use \csname to define the condition? I hope this works the same for the datatool-command. Thanks already for the answer! –  Didii Jul 31 '12 at 9:32
    
Ok, with some guesswork I managed to find it, but seriously 6? \expandafter\expandafter\expandafter\ifthenelse\expandafter\expandafter\expanda‌​fer{\condition}.... Thanks again :) –  Didii Jul 31 '12 at 9:45
    
Yes, seriously, six! Appendix D of the TeXbook (titled 'Dirty Tricks') illustrates how to expand \a\b\c\d in reverse order, and it takes 11 \expandafter calls. Knuth remarks that this is "probably too lengthy to be of any use" (Appendix D should come with a straitjacket in a handy pocket at the end of the chapter). –  Norman Gray Jul 31 '12 at 10:20
    
Probably a good thing they are working on a new version then :) –  Didii Jul 31 '12 at 11:50
    
@Didii: That's why etoolbox and expl3 are useful. –  Leo Liu Jul 31 '12 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.