2

Is there any way to put a string into a token list and have it be interpreted as if the string was a token list itself?

In the snippet below, I want/expect all 3 solutions to display the true-branch (3 times "well, hello there."), but the middle one displays "what?".

What can I do, to solve this? The string, I want to put left in the token list is a given and comes from another procedure - that's something I can not change (in my use case, not the example below, obviously).

\documentclass[varwidth,border=10pt]{standalone}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\clist_new:N \l_some_clist
\clist_put_right:Nn \l_some_clist {hello-world, bye-world}

% only token lists:
\tl_set:Nn \l_testBa_tl {hello}
\tl_set:Nn \l_testBb_tl {-world}
\tl_put_right:No \l_testBa_tl \l_testBb_tl
\clist_if_in:NoTF \l_some_clist \l_testBa_tl {
    well,~hello~there.
}{
    what?
}
\par
% token list with a string
\tl_set:Nn \l_testA_tl {hello}
\str_set:Nn \l_testA_str {-world}
\tl_put_right:No \l_testA_tl \l_testA_str
\clist_if_in:NoTF \l_some_clist \l_testA_tl {
    well,~hello~there.
}{
    what?
}
\par 
% token list with a string should be interpreted the same as if it would be entered manually:
\tl_set:Nn \l_testA_tl {hello}
\tl_put_right:Nn \l_testA_tl {-world}
\clist_if_in:NoTF \l_some_clist \l_testA_tl {
    well,~hello~there.
}{
    what?
}
\ExplSyntaxOff
\end{document}

PS.: I have seen Comparing LaTeX3 token list to string, but this does not seem to be about the LaTeX3-strings…

  • @Manuel as I tried to explain: I don't necessarily want to use a string, but I get one from \StringEncodingConvert from the package stringenc and want to compare this string to an item from a clist. – genericFJS Dec 1 '17 at 20:20
  • Removed the comment. Yes, if that's a given you need to work around that ;) – Manuel Dec 1 '17 at 20:21
4

When you use o arguments you should use braces. Plus in your cases sometimes what you want is V arguments (which do not need braces).

You can do \tl_set_rescan with the “given string”.

\str_set:Nn \l_given_str { -world } % this is the given string that you want to convert
                                    % to a normal token list

\tl_set:Nn \l_tmpa_tl { hello }
\tl_set_rescan:Nno \l_tmpb_tl { } { \l_given_str } % here you rescan to a normal token list
\tl_put_right:NV \l_tmpa_tl \l_tmpb_tl
\clist_if_in:NVTF \l_some_clist \l_tmpa_tl
 { well, ~ hello ~ there. }
 { what? }
  • Thanks for the answer and the additional tips regarding arguments. – genericFJS Dec 1 '17 at 20:23

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.