2

I want to use \token_if_eq_meaning:NN that compares meaning of just two tokens.

I have such a token list {\langle\rangle} that's saved via

\tl_set:Nn \my_tokens_list {\langle\rangle}

How can I extract first unbraced token from \my_tokens_list to \my_first_token variable and perform the comparison

\token_if_eq_meaning:NN \my_first_token \langle

2 Answers 2

1

You can extract any item you want (provided it is in the range) and make the comparison.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\comparetokenTF}{sO{1}mmmm}
 {
  % #2 = position
  % #3 = token to compare with
  % #4 = token list
  % #5 = true text
  % #6 = false text
  \IfBooleanTF{#1}
   {
    \antshar_comparetoken:nnVnn { #2 } { #3 } #4 { #5 } { #6 }
   }
   {
    \antshar_comparetoken:nnnnn { #2 } { #3 } { #4 } { #5 } { #6 }
   }
 }

\cs_new:Nn \antshar_comparetoken:nnnnn
 {
  \int_compare:nTF { 1 <= #1 <= \tl_count:n { #3 } }
   {
    \__antshar_comparetoken:Nenn #2 { \tl_item:nn { #3 } { #1 } } { #4 } { #5 }
   }
   {
    Exceeded~range
   }
 }
\cs_generate_variant:Nn \antshar_comparetoken:nnnnn { nnV }

\cs_new:Nn \__antshar_comparetoken:Nnnn
 {
  \token_if_eq_meaning:NNTF #1 #2 { #3 } { #4 }
 }
\cs_generate_variant:Nn \__antshar_comparetoken:Nnnn { Ne }
\ExplSyntaxOff

\begin{document}

\comparetokenTF{\langle}{\langle\rangle}{T}{F} should give T

\comparetokenTF{\rangle}{\langle\rangle}{T}{F} should give F

\comparetokenTF[2]{\rangle}{\langle\rangle}{T}{F} should give T

\newcommand{\mytl}{\langle\rangle}

\comparetokenTF*{\langle}{\mytl}{T}{F} should give T

\comparetokenTF*{\rangle}{\mytl}{T}{F} should give F

\comparetokenTF*[2]{\rangle}{\mytl}{T}{F} should give T

\comparetokenTF*[3]{\rangle}{\mytl}{T}{F} (exceeded)

\end{document}

The only complication is that e expansion provides the result in braces, but we can remove them using an auxiliary function.

enter image description here

1
  • Basically, you put \token_if_eq_meaning:NNTF inside a function that has e type of the second argument that does the job. I already knew that from your answer but I wanted to figure out whether it's possibly to make it directly, omitting nested functions. Anyway thank you for the answer. Accessing element from arbitrary position in token list is useful to find out.
    – antshar
    May 11, 2022 at 9:45
0

Apparently, you cannot save a single unbraced token, but what you can do is modifying the \token_if_eq_meaning:NN so that it can unbrace argument itself

\tl_set:Nx \first_token { \tl_head:n {\langle\rangle} }
\exp_last_unbraced:NV \token_if_eq_meaning:NNTF \first_token \langle {Tr}{Fl}

Or even in one line

\exp_last_unbraced:Nx \token_if_eq_meaning:NNTF { \tl_head:n {\langle\rangle} } \langle {Tr}{Fl}

You must log in to answer this question.

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