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 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}
  • 2
    Full expansion or once? – percusse Feb 5 '16 at 10:00
  • 1
    \expandafter\selectpet\expandafter{\mychoice} – David Carlisle Feb 5 '16 at 10:08
  • Perhaps this is easier with expl3 sequences code instead of patching? – user31729 Feb 5 '16 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. :-/ – LianTze Lim Feb 5 '16 at 13:24
  • @DavidCarlisle Ahem... an answer maybe ? – percusse Feb 5 '16 at 13:30
7

You want to expand mychoice before calling \selectpet so:

\expandafter\selectpet\expandafter{\mychoice}

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.