4

This is a follow-up question to Xparse Adding backslash after each command.

xparse allows for conditional tokens to be inserted into the parameter text for a macro using the t<token> directive (from the documentation:

t An optional <token>, which will result in a value \BooleanTrue if <token> is present and \BooleanFalse otherwise. Given as t<token>.

However, why do the following two implementations - one using (La)TeX and the other using LaTeX3 - not produce the same result?

enter image description here

\documentclass{article}
\usepackage{amsmath,xparse}
\begin{document}

% LaTeX implementation
\makeatletter
\newcommand{\mycommand}[1]{%
  \begin{alignat}{1} #1\null \end{alignat}%
}%
\newcommand{\command}[1]{\text{#1} \@ifnextchar\null{}{\\}}%
\makeatother

Some text before.
\mycommand{
  \command{Hello}
  \command{Hello two}
  \command{Hello three}
}%
Some text after.

\noindent
\hrulefill

% LaTeX3 implementation
\RenewDocumentCommand{\mycommand}{ m }{%
  \begin{alignat}{1} #1\null \end{alignat}%
}%
\RenewDocumentCommand{\command}{ m t\null }{\text{#1} \IfBooleanF{#2}{\\}}%

Some text before.
\mycommand{
  \command{Hello}
  \command{Hello two}
  \command{Hello three}
}%
Some text after.

\end{document}

The conditional token seems to be picked up appropriately when adding \tracingmacros1 to the LaTeX3 section. However, I'm not sure why \null is "never found", leading to \BooleanFalse (and therefore a \\) even in the last statement.

Werner
  • 603,163

1 Answers1

5

The 2e command skips white space while looking for the character.

\documentclass{article}
\usepackage{amsmath,xparse}
\begin{document}

% LaTeX implementation
\makeatletter
\newcommand{\mycommand}[1]{%
  \begin{alignat}{1} #1\null \end{alignat}%
}%
\newcommand{\command}[1]{\text{#1} \@ifnextchar\null{}{\\}}%
\makeatother

Some text before.
\mycommand{
  \command{Hello}
  \command{Hello two}
  \command{Hello three}
}%
Some text after.

\noindent
\hrulefill

% LaTeX3 implementation
\RenewDocumentCommand{\mycommand}{ m }{%
  \begin{alignat}{1} #1\null \end{alignat}%
}%
\RenewDocumentCommand{\command}{ m t\null }{\text{#1} \IfBooleanF{#2}{\\}}%

Some text before.
\mycommand{
  \command{Hello}
  \command{Hello two}
  \command{Hello three}}%
Some text after.

\end{document}
David Carlisle
  • 757,742
  • ...even in math mode, strange. – Werner Jan 23 '15 at 20:11
  • @Werner \@ifnextchar doesn't know whether it is in math mode or not, (nor does xparse) they are looking for tokens. the fact that a space token doesn't produce a typeset result in vertical or math modes isn't relevant at this level. – David Carlisle Jan 23 '15 at 20:14