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?

0

1 Answer 1

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

1
  • 2
    I've fixed a few slips, hope you don't mind.
    – egreg
    Commented Jun 13, 2016 at 21:00

You must log in to answer this question.

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