2

See below for an example.

The expected behaviour of \foo was that of \fop but with \foo{\textbf{a}, \textbf{b}, \textbf{c}} I have [THE END] in places where there should not be.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\MappingFunction}{m}
{
  \quark_if_nil:NTF #1
  {[THE~END]~}
  {|#1|\quad}
}

\NewDocumentCommand \Foo
{ > { \SplitList { , } } m }
{ \ProcessList { #1 } { \MappingFunction } }

\NewDocumentCommand \foo { m }
{
  \Foo {#1,\q_nil}
}

%%%%% ---------------------------------------------------------------------
\NewDocumentCommand{\MappingFunctionI}{m}
{
  \tl_if_eq:nnTF {#1}{@@@}
  {[THE~END]~}
  {|#1|\quad}
}

\NewDocumentCommand \Fop
{ > { \SplitList { , } } m }
{ \ProcessList { #1 } { \MappingFunctionI } }

\NewDocumentCommand \fop { m }
{
  \Fop {#1,@@@}
}
\ExplSyntaxOff

\begin{document}
\fop{ a, b, c, d }\par
\fop{\textbf{a}, \textbf{b}, \textbf{c}}

\foo{ a, b, c, d }\par
\foo{\textbf{a}, \textbf{b}, \textbf{c}}
\end{document}

I would be glad to have an explanation.

1
  • You're not passing in an N-type argument to \quark_if_nil:NTF in the case of #1 being \textbf{a}, hence, what it sees is \quark_if_nil:NTF \textbf {a} {[THE~END]~} {|#1|\quad}, evaluates that \textbf is not the quark, inserts the second argument (being {[THE~END]~} here) and gobbles the first (being {a}). The {|#1|\quad} is never touched and hence stays where it is. – Skillmon Jun 22 at 8:27
4

The issue is that you're not passing in an N-type argument to \quark_if_nil:NTF. Instead what \quark_if_nil:NTF sees is:

\quark_if_nil:NTF \textbf
  {a}
  {[THE~END]~}

and after that a trailing {|\textbf{a}|\quad}. \quark_if_nil:NTF will determine that \textbf is not the quark, hence gobble the T branch (removing {a}) and use the F branch (leaving [THE~END]~ in the input stream). So [THE~END]~ is printed, and because there is a trailing {|\textbf{a}|\quad} as well that one is printed afterwards.

The correct approach would be to use \quark_if_nil:nTF to test whether an arbitrary token list is exactly \q_nil. So your code should be:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\MappingFunction}{m}
{
  \quark_if_nil:nTF {#1}
  {[THE~END]~}
  {|#1|\quad}
}

\NewDocumentCommand \Foo
{ > { \SplitList { , } } m }
{ \ProcessList { #1 } { \MappingFunction } }

\NewDocumentCommand \foo { m }
{
  \Foo {#1,\q_nil}
}

%%%%% ---------------------------------------------------------------------
\NewDocumentCommand{\MappingFunctionI}{m}
{
  \tl_if_eq:nnTF {#1}{@@@}
  {[THE~END]~}
  {|#1|\quad}
}

\NewDocumentCommand \Fop
{ > { \SplitList { , } } m }
{ \ProcessList { #1 } { \MappingFunctionI } }

\NewDocumentCommand \fop { m }
{
  \Fop {#1,@@@}
}
\ExplSyntaxOff

\begin{document}
\fop{ a, b, c, d }\par
\fop{\textbf{a}, \textbf{b}, \textbf{c}}

\foo{ a, b, c, d }\par
\foo{\textbf{a}, \textbf{b}, \textbf{c}}
\end{document}

And that will behave like you expected.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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