10

I have managed to create a command that takes an argument and does substitutions on it with l3regex. However, if the argument contains itself a command, the command name is printed in the output as if it was a string.

\documentclass{article}
\usepackage{l3regex}
\ExplSyntaxOn
\newcommand\mycommand[1]{
  \str_set:Nn \l_temp_str {#1}
  \regex_replace_all:nnN {[abc]} {\c{textbf}\cB\{\0\cE\}} \l_temp_str
  {\l_temp_str}
}
\ExplSyntaxOff

\begin{document}
\mycommand{wax\textit{b}ycz}
\end{document}

How should I correct the code so that the argument string can include any command?

lotomat
  • 527

1 Answers1

6

Try using a token list rather than a string:

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  %\str_set:Nn \l_temp_str {#1}
  \regex_replace_all:nnN {[abc]} {\c{textbf}\cB\{\0\cE\}} \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}
\mycommand{wax\textit{b}ycz}
\end{document}

This should treat commands as commands.

enter image description here

egreg
  • 1,121,712
rbrignall
  • 1,564