1

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

\let\oldhat\hat

with something in this format:

\let\old(ARGUMENT)\(ARGUMENT)

I tried

\newcommand{\makeold}[1]{
    \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.

2

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

\newcommand{\makeold}[1]{%
   \expandafter\let
       \csname old#1\expandafter\endcsname
       \csname#1\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 | |
1

It's simpler with etoolbox:

\usepackage{etoolbox}

\newcommand{\makeold}[1]{\csletcs{old#1}{#1}}
| 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.