使用擴充巨集作為 \patchcmd 的搜尋模式

使用擴充巨集作為 \patchcmd 的搜尋模式

有沒有辦法使用擴展宏的值作為etoolboxs \patchcmd(或xpatchs\xpatchcmd等)的搜尋模式,以便可以透過更改catcodes 來獲得某種「動態」補丁?

這是一個最小工作範例(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}

答案1

您想mychoice在調用之前擴展\selectpet

\expandafter\selectpet\expandafter{\mychoice}

相關內容