10

Is there any way to use the value of an expanded macro as the search pattern for etoolbox's \patchcmd (or xpatch's \xpatchcmd, etc), so that one can have some sort of a "dynamic" patch, by way of change catcodes?

Here's a minimum working example (MWE).

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xcolor}

\begin{document}

\newcommand{\wordlist}{cat dog parrot goldfish hamster}

\newcommand\selectpet[1]{%
  \bgroup
  \patchcmd{\wordlist}{#1}{\textcolor{red}{#1}}{}{}
  \wordlist
  \egroup
}

\selectpet{dog}  %% This works

\def\mychoice{dog}
\selectpet{\mychoice}  %% This doesn't

\end{document}
5
  • 2
    Full expansion or once?
    – percusse
    Commented Feb 5, 2016 at 10:00
  • 1
    \expandafter\selectpet\expandafter{\mychoice} Commented Feb 5, 2016 at 10:08
  • Perhaps this is easier with expl3 sequences code instead of patching?
    – user31729
    Commented Feb 5, 2016 at 10:51
  • Thanks @percusse, you pointed me in the right direction, and @David hit it on the head! @Christian Unfortunately my expl3-fu is not up to scratch at all. :-/
    – imnothere
    Commented Feb 5, 2016 at 13:24
  • @DavidCarlisle Ahem... an answer maybe ?
    – percusse
    Commented Feb 5, 2016 at 13:30

1 Answer 1

7

You want to expand mychoice before calling \selectpet so:

\expandafter\selectpet\expandafter{\mychoice}
0

You must log in to answer this question.

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