11

If a command \commandA exists, and I do

\newcommand{\commandB}{}
\let\commandB\commandA
*a redefinition of \commandA*

the modification on \commandA also affects \commandB. But I would like to save "the original command A" as commandB.

How can I do that?

5
  • 6
    As long as \let\commandB\commandA occurs before the redefinition of \commandA, then the original definition of \commandA is saved in \commandB Commented Feb 9, 2017 at 19:08
  • The order of statements is important... Storing should occur very early, in most cases
    – user31729
    Commented Feb 9, 2017 at 19:09
  • With \let\commandB\commandA, the meaning of \commandB does not depend on later modifications of \commandA.
    – egreg
    Commented Feb 9, 2017 at 19:10
  • Thank you all. I now understand that my problem comes from the a redefinition of \commandA part.
    – gpst
    Commented Feb 9, 2017 at 19:14
  • See also. macros - Can I redefine a command to contain itself? - TeX - LaTeX Stack Exchange for some updates. e.g. the introduction of \NewCommandCopy command etc.
    – user202729
    Commented May 20, 2023 at 7:09

2 Answers 2

7

Perhaps this analogy will help to understand why one should use \let\commandB\commandA before \renewcommand{\commandA}{...}.

If a file should be kept before accidentally modifying (deleting?) it, it should be stored to some other place or copied to another name, so a cp -a fileA fileB (with Un*x commands) is making the copy of the original file and naming it fileB. For a short while both files do exist and are identical.

Now fileA can be edited etc.

The same is true for \let..., but \let is not like a pointer in the C programming language, pointing to the same memory location or like a symlink in the file system.

  1. First safe the definition of \commandA to some other name, say \commandB with

    \let\commandB\commandA
    

This will be done immediately, i.e. \commandB has the same definition like \commandA and will do the same operation like \commandA.

  1. Now do the eventual change of \commandA with \renewcommand{\commandA}{...} or \def etc.

This is a technique applied very often, in order to extent the definition of \commandA (i.e. before or after its code) to use its definition inside the redefinition, i.e. call \commandB to get the original effect.

Please note that for commands with optional arguments \LetLtxMacro from the letltxmacro package by Heiko Oberdiek should be used, not \let.

expl3 on the other hand provides \cs_set_eq:NN etc. commands.

1

Consider using the phfnote package. I'm going to show how to do with all of the escaped character codes such as the cedilla \c, some of which take arguments. In the preamble, use

\usepackage[preset=reset]{phfnote}

Before redefining anything, save all of the old commands:

\phfnoteSaveDefs{origcmds}{H,c,k,l,b,d,r,aa,u,v,t,o}

Then you are free to define them as you wish. Right before you need to use the old versions, write

\phfnoteRestoreDefs{origcmds}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .