2

I'm trying to write some code that replaces a couple of placeholder tokens in a token list, but am running up against a wall since I am still very new to latex3.

Currently, I get no result and no errors, so I suspect I should use something other than token lists for the placeholder tokens...

\documentclass{article}

\begin{document}

\ExplSyntaxOn

% placeholder tokens
\tl_new:N \VALUE
\tl_new:N \SYMBOL

\cs_set:Npn \test:nnnn #1#2#3#4
{
    \tl_clear:N #4
    \tl_set_eq:NN #4 #1
    \tl_replace_all:Nnn #4 { \VALUE } { #2 }
    \tl_replace_all:Nnn #4 { \SYMBOL } { #3 }
}

\tl_new:N \l_result_tl
\test:nnnn { \VALUE ~ \SYMBOL } { 300 } { EUR } \l_result_tl 

result ~ should ~ be ~ "300 ~ EUR": ~ \tl_use:N \l_result_tl

\ExplSyntaxOff

\end{document}
3
  • 1
    You're defining \test:nnnn, not \test.
    – egreg
    Commented Oct 1, 2023 at 12:15
  • Haha, whoops! Thanks @egreg
    – meide
    Commented Oct 1, 2023 at 12:16
  • updated MVP and description of fail state
    – meide
    Commented Oct 1, 2023 at 12:31

1 Answer 1

2

You are defining \test:nnnn, so \test isn't defined.

On the other hand, \test should preferably be defined with \NewDocumentCommand, because it is a user level command and the inner function it calls should be protected, because it does assignments.

Also you want \tl_set:Nn #4 { #1 }.

And all should be done in the preamble. Don't abuse \cs_set(_protected).

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\test}{mmmm}
 {
  \meide_test:nnnn { #1 } { #2 } { #3 } { #4 }
 }

% placeholder tokens
\tl_new:N \VALUE
\tl_new:N \SYMBOL
% a suitable user level token list
\tl_new:N \RESULT

\cs_new_protected:Npn \meide_test:nnnn #1#2#3#4
  {
    \tl_set:Nn #4 { #1 }
    \tl_replace_all:Nnn #4 { \VALUE } { #2 }
    \tl_replace_all:Nnn #4 { \SYMBOL } { #3 }
  }

\ExplSyntaxOff

\begin{document}

\test{\VALUE\ \SYMBOL}{300}{EUR}\RESULT

result should be "300 EUR": \RESULT

\end{document}

enter image description here

Your attempt with \tl_set_eq:NN #4 #1 would produce, in the presented use case,

\tl_set_eq:NN \tl_result_tl \VALUE ~ \SYMBOL

and of course you get nothing in the final output, because \VALUE is empty, so the replacement simply don't happen; the space isn't actually there, because it follows (in the argument to \test:nnnn) it follows a control sequence name; finally \SYMBOL produces nothing because it's empty.

You must log in to answer this question.

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