0

Question. (The title should be sufficient to understand the question.)

Comments. (0) Normally, and justifiably so, pdflatex gives the error message "Command etc already defined."

(1) Leaving in multiple command definitions may not best practice, but there can be pragmatic reasons to do so, temporarily, and clean up the document in a later pass.

  • 3
    You can always use \renewcommand (or \def) if a command is already defined. – Ulrike Fischer May 18 '17 at 15:23
  • @Ulrike Fischer: Thanks, this is literally true: if a command is already defined, then changing each subsequenct "\newcommand" to "\renewcommand" will result in legal code. However, this does not solve the pragmatic problem hinted at: – Peter Heinig May 18 '17 at 15:32
  • (continued) suppose, for the sake of argument, you are confronted with a .tex file whose only reason for being illegal in pdflatex's eyes is that there is at least one doubly-defined command. With the solution you suggest, you would still have to make a pass from start to end, and change each duplicate definition into a "\renewcommand" (note that there will be a new type of error message "etc undefined" if you just replace each "\newcommand" with "\renewcommand". Would you please suggest another solution? – Peter Heinig May 18 '17 at 15:33
  • 1
    you can use \let\foo\relax\newcommand\foo{...} or \def\foo{..} – David Carlisle May 18 '17 at 15:35
  • @ David Carlisle: Thanks. This is literally true, and gets closer to a pragmatic soution, yet it solves the above-mentioned pragmatic problem only modulo not entirely trivial editor-skills: one has to make the replacement of "\newcommand{\foo}" by "\let\foo\relax\newcommand{\foo}" throughout the .tex file dependent on the string \foo. This can be done via a regular expression, but is not entirely trivial. Does there exist a solution via some global command or option? – Peter Heinig May 18 '17 at 15:43
  • Just place \def\foo{} once at the beginning of the inputs and force all (re)definitions to be of the type \renewcommand\foo... or \def\foo..., your choice. – Steven B. Segletes May 18 '17 at 15:49
  • 1
    If it is okay that every command is then robust, you could try \let\newcommand\DeclareRobustCommand. If not you use the \declarecommand from here https://tex.stackexchange.com/questions/228862/how-to-define-or-redefine-a-command-mixing-providecommand-renewcommand/228865#228865 and map \newcommand to it. – Ulrike Fischer May 18 '17 at 15:59

1 Answers1

3

as in comments you seem to not want to edit the body of the file you could do

\let\zzz\newcommand
\def\newcommand#1{\let#1\relax\zzz#1}

so \newcommand always undefines its argument before making the definition.

David Carlisle
  • 757,742