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}
```