or
JouleV
Currently, I want to have a pair of command, say `\mycommand{}` and `\mapmycommand{}{}`, so that

```tex
% Initially
\mycommand{a} -> 
\mapmycommand{a}{b}
\mycommand{a} -> b
\mapmycommand{c}{d}
\mycommand{c} -> d
\mycommand{a} -> b
```

I have tried the following. All of them fail.

```tex
\documentclass{article}

\def\mycommand#1{}
\def\mapmycommand#1#2{%
  \edef\myoldcommand{\mycommand}%
  \edef\mycommand#1{\myoldcommand\ifstrequal{#1}{##1}{#2}{}}%
}%

\begin{document}
\mycommand{a}%
\mapmycommand{a}{b}%
\mycommand{a}%
\mapmycommand{c}{d}%
\mycommand{c}%
\mycommand{a}%
\end{document}
```

```tex
\documentclass{article}

\def\mycommand#1{}
\def\mapmycommand#1#2{%
  \let\myoldcommand\mycommand
  \def\mycommand#1{\myoldcommand\ifstrequal{#1}{##1}{#2}{}}%
}%

\begin{document}
\mycommand{a}%
\mapmycommand{a}{b}%
\mycommand{a}%
\mapmycommand{c}{d}%
\mycommand{c}%
\mycommand{a}%
\end{document}
```

I also know of `\g@addto@macro`, but so far I fail to apply this command correctly, because `\mycommand` takes one argument, and obviously the following never works:

```tex
\documentclass{article}

\def\mycommand#1{}
\def\mapmycommand#1#2{%
  \makeatletter%
  \g@addto@macro\mycommand{\ifstrequal{#1}{##1}{#2}{}}%
  \makeatother%
}%

\begin{document}
\mycommand{a}%
\mapmycommand{a}{b}%
\mycommand{a}%
\mapmycommand{c}{d}%
\mycommand{c}%
\mycommand{a}%
\end{document}
```

What should I do to get the desired behaviour?
Top Answer
Joseph Wright
This can all be done using a simple hash table lookup
```
\documentclass{article}
\makeatletter
\newcommand\mycommand[1]{%
  \@ifundefined{JV@cmd@\detokenize{#1}}
    {}
    {\@nameuse{JV@cmd@\detokenize{#1}}}%
}
\newcommand\mapmycommand[2]{%
  \@namedef{JV@cmd@\detokenize{#1}}{#2}%
}
\makeatother
\begin{document}

\mycommand{a}%
\mapmycommand{a}{b}%
\mycommand{a}%
\mapmycommand{c}{d}%
\mycommand{c}%
\mycommand{a}%

\end{document}
```
How to implement command mapping?
JouleV replying to Joseph Wright
Thank you very much! It can even "re-map", which my algorithm couldn't do. And of course, welcome to TopTeX!
Joseph Wright
Thanks all: I've answered questions on just about every TeX site/list/... I know of ;)
marmot
@Joseph A hearty welcome from the TikZlings on this site!
Skillmon
Yay, the advertising via account name seems to work out :) Welcome!
samcarter
@Joseph Welcome to TopTeX! Nice to see you here!