7

Is there a merged command \maybenewcommand that is either \newcommand or \renewcommand, depending on the situation: if the command is not yet defined, use \newcommand and use \renewcommand otherwise.

I currently merge multiple papers into a large document. I use the same commands in several of them, with slightly different meaning. I would now like to avoid to check for each \newcommand if it has been used in earlier places and then replacing them by \renewcommand by hand.

Thanks, Christian

Christian
  • 515
  • 3
    Are you looking for \providecommand or the combinations \providecommand\foo{}\renewcommand\foo... (which will always create a definition)? – Joseph Wright May 20 '14 at 09:24
  • 2
    Maybe the answer is here : http://tex.stackexchange.com/questions/28670/running-new-command-only-if-it-exists – Tarass May 20 '14 at 09:25
  • I wasn't aware of \providecommand. It's not quite what I want since I want to possibly update the behaviour. But adding a new command consisting of \providecommand\foo{} and \renewcommand\foo{...} should do the job. – Christian May 20 '14 at 09:41
  • An analogous construction should be working using \ifdefined. Thanks to both of you for the pointers! – Christian May 20 '14 at 09:43
  • @ChristianStump So just to clarify: you want a macro that overrides an existing value for that macro if there is one and declare a new value if there is no such existing value? Read: http://tex.stackexchange.com/questions/258/what-is-the-difference-between-let-and-def I'd go with \let – 1010011010 May 20 '14 at 10:24

1 Answers1

10

There are the TeX and the LaTeX version:

TeX:

\def\mycommand#1{...}% overwrites without any warning if  \mycommand exists

LaTeX:

 \newcommand\mycommand[1]{...}%  gives an error message if exists
 \renewcommand\mycommand[1]{...}%  gives an error message if not exists

 \providecommand\mycommand{}  
 \renewcommand\mycommand[1]{...}%  overwrites or defines command