9

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?

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

|improve this answer|||||
  • 2
    I've fixed a few slips, hope you don't mind. – egreg Jun 13 '16 at 21:00

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.