# How do I reuse a command name two or more times?

I found out how to reuse a command name.

But how can I reuse a command more times, each time using the previous definition? If I simply try something like

\newcommand{\aaa}{a}
\let\oldaaa\aaa
\renewcommand{\aaa}{\oldaaa b} % now \aaa is ab
\let\oldaaa\aaa
\renewcommand{\aaa}{\oldaaa c} % now \aaa should be abc


I get TeX capacity exceeded error.

This is a very simplified example. The problem came up when I was trying to write a command that would accomplish a similar task, ie. concatenate its arguments when called repetitively; so, ideally the code should be similar for each repetition.

-
Your second \let has \oldaaa referencing itself by way of \aaa. Remember, \let and \def are not the same. Depending on what you are stuffing in there, replacing the first \renewcommand with an \edef should wipe the slate clean, freeing up \oldaaa – Steven B. Segletes Mar 14 '13 at 1:31
This is not the best way for concatenating tokens. Can you try and describe the real problem? – egreg Mar 14 '13 at 1:32
I accepted the answer of @Werner because it is more generic. However, for my task I used the \appto command suggested by @egreg. – nplatis Mar 15 '13 at 7:43

Remember that (La)TeX is a macro programming language that has this quirky thing called "expansion". For example, if

\newcommand{\aaa}{a}


then \aaa expands to a. Alternatively put, a is the "replacement text" for \aaa. In a definition of

\let\oldaaa\aaa
\renewcommand{\aaa}{\oldaaa b} % now \aaa is ab


you are copying the definition of \aaa into \oldaaa (effectively similar to \def\oldaaa{<whatever \aaa is>}, but not the same as \def\oldaaa{\aaa}), followed by a redefinition of \aaa into \oldaaa b. During expansion - when you write \aaa, this is replaced by \oldaaa b, and since you \let\oldaaa\aaa, this is replaced by \aaa b, which is circular. Here's a stepwise showcase:

\newcommand{\aaa}{a}
\let\oldaaa\aaa
\renewcommand{\aaa}{\oldaaa b}

> \aaa
> \oldaaa b % Expansion of \aaa
> \aaa b % Expansion of \oldaaa
> \oldaaa b b % Expansion of \aaa
> \aaa b b % Expansion of \oldaaa
> \oldaaa b b b % Expansion of \aaa
> ...


What I think you're after is expanding the contents at the time of redefinition, which would look like this:

\newcommand{\aaa}{a}
\expandafter\renewcommand\expandafter\aaa\expandafter{\aaa b} % now \aaa is ab
\expandafter\renewcommand\expandafter\aaa\expandafter{\aaa c} % now \aaa should be abc


Note that since the replacement text of \aaa is expanded before making the assignment, so there's no need for using an additional macro like \oldaaa. This would also only work if the contents at redefinition is expandable itself.

-
And the \let instructions are useless: you can use \aaa in the redefinitions. Consider also \appto of etoolbox as a solution. – egreg Mar 14 '13 at 1:41

You can avoid \expandafter if you use the etoolbox package. This requires the e-TeX extensions to your TeX executable, but chances are extremely high that this is default in your installation.

\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\newcommand{\aaa}{a}
\show\aaa
\apptocmd{\aaa}{b}{\message{aaa successfully patched}}{\message{aaa not successfully patched}}
\show\aaa
\apptocmd{\aaa}{c}{\message{aaa successfully patched}}{\message{aaa not successfully patched}}
\show\aaa
\end{document}


The arguments to \apptocmd are macro name, stuff to add to the definition, then what to do on success or failure. I'm using the last to just to report to the console/log file that the patching succeeded or failed. \show will pause compilation and write the current value of the macro to the console. You have to hit enter to continue each time. The result is:

...
> \aaa=\long macro:
->a.
l.6 \show\aaa

? [ENTER]
aaa successfully patched
> \aaa=macro:
->ab.
l.8 \show\aaa

? [ENTER]
aaa successfully patched
> \aaa=macro:
->abc.
l.10 \show\aaa

? [ENTER]
(./aaa.aux) )
No pages of output.
...

-
For parameterless macros there's the simpler \appto. – egreg Mar 14 '13 at 10:05
Good point. You don't need the handlers for success/failure in that case. – Matthew Leingang Mar 14 '13 at 17:28