6

LaTeX, of course, has a protective mechanism that prevents you from doing some stupid things:

\newcommand\a{a}
\newcommand\a{b} %% LaTeX Error: Command \a already defined.

Of course, if you know what you're doing, then you can circumvent it with \renewcommand. However, some commands have their own definition mechanisms:

\newcommand\a{a}
\DeclareMathOperator\a{a} %% LaTeX Error: Command \a already defined.

(and there is no \RedeclareMathOperator). I've been getting around this by doing something awkward like:

\newcommand\a{a}
\makeatletter
\DeclareMathOperator\@a{a}
\let\a=\@a %% \let (and \def) circumvents LaTeX's protection
\makeatother

(Of course, the best thing to do would be to delete that original definition, but, in practice, it's in another package that I must include but whose source I do not control.) However, it occurs to me that a hypothetical \undef command would do the job:

\newcommand\a{a}
\undef\a
\newcommand\a{b} %% No complaints

Does such a command exist? If not, is there an easy way to write it?

LSpice
  • 1,448
  • @Sigur, yes, it looks like it. (If you post it as an answer, then I'll be happy to accept it.) Sorry, that was a silly question! – LSpice Dec 30 '14 at 18:12
  • 3
    (Oops, Sigur's comment went away; it pointed out that \let\a=\relax would do what I wanted.) – LSpice Dec 30 '14 at 18:13
  • @LSpice. You can delete/answer your question if you wish. – Sigur Dec 30 '14 at 18:14
  • 1
    @Sigur, thanks. Is it better to delete it? In terms of an answer, I'd rather accept your answer than repeat it myself. – LSpice Dec 30 '14 at 18:15
  • 5
    \let\a\imareallyundefinedcommand would work too. – Ulrike Fischer Dec 30 '14 at 18:15
  • @ChristianHupfer, thanks; actually it looks like etoolbox defines exactly the \undef command for which I asked (taking a control sequence, rather than a name, as its argument). – LSpice Dec 30 '14 at 18:16
  • @LSpice: See egreg's answer to a former question of mine: http://tex.stackexchange.com/questions/199692/are-end-macro-names-reserved-in-latex2e -- it's somewhat related –  Dec 30 '14 at 18:21
  • 2
    @LSpice \newcommand and \DeclareMathOperator rely on \@ifdefinable which considers a command undefined in the following two cases: (1) it currently has no meaning; (2) it is equivalent to \relax; the same command rejects command names starting with \end... – egreg Dec 30 '14 at 18:25
  • @ChristianHupfer, thanks for the pointer, but that answer seems to be more about the mechanism by which LaTeX implements this check. (Indeed, I noticed that \renewcommand proceeds simply by \letting \@ifdefinable to \@rc@ifdefinable, and wondered if that might be appropriate; but that clobbers \@ifdefinable for everyone, and so I think \letting the one relevant symbol to \relax is probably better.) – LSpice Dec 30 '14 at 18:25
  • @LSpice: Yes, it was just for more information ;-) –  Dec 30 '14 at 18:28
  • @LSpice I'm not sure about your problem with \rc@ifdefinable. But \let\a\relax is really what you're looking for. – egreg Dec 30 '14 at 19:06
  • @greg, yes, I agree—it appeared about 5 minutes after I posted my question, in a comment by Sigur. – LSpice Dec 30 '14 at 20:18
  • @Werner, I agree that it looks like the same question, but I don't think that it really is a duplicate (except in the sense that the poser of that question knew the answer to this one). As Sigur, egreg, and others point out, the answer to my question is just "\let \a to \relax", whereas that poster specifically mentions that \letting to \relax didn't work. – LSpice Dec 30 '14 at 20:19

0 Answers0