I am hoping to create a command that automates this process below


with something in this format:


I tried

    \expandafter\let\csname old#1\endcsname\csname#1\endcsname}
\makeold{hat} %This should rename \hat to \oldhat

but this gives me an error that I have too many \endcsname's. Am I misunderstanding the purpose of \csname?

Edit: I had forgotten \let in my attempt.


You are missing a \let and an extra \expandafter. This definition works:

       \csname old#1\expandafter\endcsname

The execution of a command like this is a bit confusing so let me describe how this command works:

\makeold{hat} expands to

\expandafter\let\csname oldhat\expandafter\endcsname\csname#1\endcsname

Next the first \expandafter jumps over the \let and expands the \csname. Expanding the \csname recursively expands any macros until it hits \endcsname and forms a macro out of the tokens found this way. In particular, it expands the second \expandafter which jumps over the \endcsname to expand the second \csname, which turns \csname hat\endcsname into \hat. What's left over are the tokens \let\oldhat\hat which is what you wanted to do.

| improve this answer | |

It's simpler with etoolbox:


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.