5

Please consider the following example:

\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?

1
  • 2
    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, 2013 at 13:35

1 Answer 1

9

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.

3
  • I see, thanks. Follow-up question: would you know how to get the behavior I intended? Nov 3, 2013 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, 2013 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. Nov 3, 2013 at 13:44

You must log in to answer this question.

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