9

I'm interested in defining a macro to deprecate other macros. The system on which we are working goes through a lot of refactorings and is used by more than a couple people, so we want to be able to mark a command as deprecated before it is removed from the macros file entirely. Specifically, I'd like to generalize

\let\fooDeprecated\foo
\renewcommand{\foo}{\warn{Deprecated command used}\fooDeprecated}

or, for that matter, anything else that will work as a simple LaTeX proxy macro (in the design pattern sense of the word "proxy"). Unfortunately, I can't seem to figure out where to stick the \expandafter magic to make this work. That is,

\newcommand{\deprecate}[1]{%
    \let\csname #1Deprecated\endcsname\csname #1\endcsname%
    \renewcommand{\csname #1\endcsname}{\warn{Deprecated command used}\csname #1Deprecated\endcsname}%
}

clearly doesn't work. Is there a better way to do this? If not, how do I figure out how to expand these things?

Thanks!

  • Excellent, thanks. :) I didn't realize that this functionality was already provided. – Zach May 29 '13 at 14:19
8

The package amsmath does something like you want for the primitive \over and its siblings.

Define two helper commands:

\DeclareRobustCommand{\deprecate}[2]{%
  % #1 is the deprecated command
  % #2 is the new command to use
  % Issue a message at first usage
  \PackageWarning{zach}{%
    Deprecated command \@backslashchar#1;\MessageBreak
    \protect#2\space should be used instead\MessageBreak
  }
  % globally redefine "\#1" to "\deprecate@#1"
  \global\expandafter\let\csname#1\expandafter\endcsname
                         \csname deprecated@#1\endcsname
  % issue the old command
  \csname#1\endcsname
}

\newcommand{\@deprecate}[2]{%
  \expandafter\let\csname deprecated@#1\expandafter\endcsname\csname#1\endcsname
  \@namedef{#1}{\deprecate{#1}{#2}}%
}

Then you can give a list of the deprecated commands along with the new ones:

\@deprecate{foo}{\newfoo}

(notice that the deprecated command should be used "by name" in the first argument).

In the document, the first usage of \foo will type on the terminal and in the log file

Package zach Warning: Deprecated command \foo;
(zach)                \newfoo should be used instead
(zach)                 on input line 28.

whilst subsequent usages will not produce messages any more.

  • More or less what I was about to post but I have been too slow apparently :) – cgnieder May 28 '13 at 21:41

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.