# Why does \tl_if_in (and \clist_if_in) insert extra tokens with \mathrm

I encountered a strange behaviour I can’t explain. In my code for a highlighting macro I tried to use \tl_if_in to do a basic math class test. But when \mathrm is part of the tokens to be tested some extra tokes (i.e. what follows \mathrm{…}) are inserted.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\tl_const:Nn \c_pirkl_math_rel_symbols_tl { +-=/ }

\NewDocumentCommand{ \highlight }{ m }{
\tl_if_in:NnT \c_pirkl_math_rel_symbols_tl { #1 } {
% ...
}
#1
}

\ExplSyntaxOff

\begin{document}
Inserts what comes after \verb|\mathrm{...}|
$\highlight{x} + \highlight{\mathrm{d}x}$

$\highlight{x} + \highlight{{\mathrm{d}x}}$
\end{document}


It is the same with \clist_is_in, except that there is also an additional comma in the output.

note: Actually my question is kind of xy problem, I came to this while working on a solution for capturing the math class: http://tex.stackexchange.com/q/369903/4918.

update: with \tl_to_string added the test is allay true

\NewDocumentCommand{ \highlight }{ m }{
\tl_if_in:NnT \c_pirkl_math_rel_symbols_tl { \tl_to_string:n { #1 } } {
T
}
#1
}


update 2: with most TL stringified I can’t make it work either …

\tl_const:Nn \c_pirkl_math_rel_symbols_tl { \tl_to_string:n { +-=/ } }

\cs_generate_variant:Nn \tl_if_in:NnT { NfT }

\NewDocumentCommand{ \highlight }{ m }{
\tl_if_in:NfT \c_pirkl_math_rel_symbols_tl { \tl_to_string:n { #1 } } {
T
}
#1
}

• could you please enlarge the attached image? – Moriambar May 15 '17 at 20:43
• @Moriambar I’m already working on it ;-) trying to find out why it is that much scaled down while/after uploading … – Tobi May 15 '17 at 20:46

It's documented: the <token list> to search for cannot contain { or }.

Stringify both TL (constant and test) should do the trick, but you need two additional cs variants for this solution:

\cs_generate_variant:Nn \tl_if_in:NnT { NfT }

\tl_const:Nx \c_pirkl_math_rel_symbols_tl { \tl_to_str:n { +-=/ } }

\NewDocumentCommand{ \highlight }{ m }{
\tl_if_in:NfT \c_pirkl_math_rel_symbols_tl { \tl_to_str:n { #1 } } {
T
}
#1
}

• Damn … how stupid I am. I read the sentence multiple times but didn’t noticed that the replacement of #1 contains braces … any ideas how to work around? – Tobi May 15 '17 at 20:52
• Actually my question is kind of xy problem, I came to this while working on a solution for capturing the math class: tex.stackexchange.com/q/369903/4918 – Tobi May 15 '17 at 20:58
• Hm … when I add \tl_to_string (see OP edit) the test always is true – Tobi May 15 '17 at 21:03
• And how can I do this (see my try in the OP; update 2) – Tobi May 15 '17 at 21:11
• I saw that I used string instead of str. But I think you found a solution. I took the liberty to add it to your answer, hope this is ok?! – Tobi May 15 '17 at 21:20