# Behavior of xparse and “token”-type argument

\documentclass{minimal}

\usepackage{xparse}
\NewDocumentCommand{\f}{tx}{\IfBooleanTF{#1}{true}{false}}

\begin{document}
\f{x}

\f[x]

\f x
\end{document}


The result looks like this:

falsex
false[x]
true


Which isn't what I expected. I expected that one of the first two would print "true", and the last one to print "false x". What is happening here? Is this the intended behavior?

Follow-up question: is there a way to get the behavior, ie. an argument that gives False when absent, and True when present?

• The argument specifier tx means that an x just after \f sets the internal boolean to true; otherwise the boolean is set to false. In the first and second calls the x doesn't directly follow \f. – egreg Nov 3 '13 at 13:35

This is the intended behaviour, and is because there is a difference between an argument which happens to contain a token and using a token directly. When TeX reads

\foo x


the very next thing after \foo is x. On the other hand, with

\foo{x}


the next thing after \foo is a {, while in

\foo[x]


it is a [. There are some technical differences between \foo{x} and \foo[x] (as one is involves a TeX group while the other is probably a LaTeX-like optional argument and will be handled using delimited macros). However, from the point of view of this question that does not matter: all that is important is that there are 'extra' tokens. The t specifier is purely about the next token, not what might happen inside other arguments.

• I see, thanks. Follow-up question: would you know how to get the behavior I intended? – Najib Idrissi Nov 3 '13 at 13:40
• @nik You are checking the nature of three different arguments: doable but I think far from sensible. For example, what happens about \foo{y} or \foo[y]? – Joseph Wright Nov 3 '13 at 13:41
• I'm fine if only one of the two \f[x] or \f{x} works. I'm basing my question on constructions such as \ar[d] from the xymatrix package, for example. – Najib Idrissi Nov 3 '13 at 13:44