Let's say we have a command \foo
with 1 argument. Can we produce a command \setfoo{aaa}{<some thing>}
which redefines \foo
to:
if an argument of \foo
is equal to 'aaa', do ; otherwise do what \foo
was supposed to do previously? I would also like to use \setfoo
many times, so
\setfoo{a}{part1}
\setfoo{b}{part2}
\setfoo{c}{part3}
\foo{a} \foo{b} \foo{c}
would produce
part1 part2 part3
I've tried the following:
\documentclass{article}
\usepackage{ifthen}
\newcommand{\foo}[1]{}
%\let\oldfoo\foo%
\newcommand{\setfoo}[2]{
\let\oldfoo\foo %<- to save current \foo
\renewcommand{\foo}[1]{
\ifthenelse{\equal{##1}{#1}}{#2}{\oldfoo{##1}}%
}
}
\begin{document}
\setfoo{a}{part1}
\setfoo{b}{part2}
%\setfoo{c}{part3}
\foo{a} \foo{a} \foo{c}
\end{document}
But it works only if there are <=2 uses of \setfoo
. Otherwise, compilation doesn't stop for a few minutes so I guess something is not working. How can this be fixed? Thanks in advance.