7

There's plenty of questions on this site about redefining specific commands, but I couldn't find a more general, comprehensive question about the safety and best practices of redefining commands in LaTeX.

Answers to specific parts of this question are welcome, but most likely have already been formulated on this site somewhere; links to those would also be welcome, of course.

Specific recommendations for further reading (chapters in books, specific content on other websites, etc.) are also welcome.


  1. Redefining a Command: what are the implications for packages I'm loading that use said command, for lower-level places that use said command (e.g. the documentclass, or for higher-level places (mostly meaning just calls to commands from packages which directly or indirectly depended upon the old command), and how do they depend on:

    1. where the original command is defined?

      • defined in a package I'm loading, in the documentclass I'm using, in the TeX engine I'm using, my own preamble, etc.
    2. where the redefined command is defined?

      • before or after \begin{document}?
      • before or after \usepackage{ <package that depends on the old command> }?
      • in a small space in the document enclosed with \bgroup and \egroup that does / doesn't make use of some / any functionality of the affected package(s).
    3. how the original command is defined?

      • \newcommand, \def, \edef, \protected\def, \NewDocumentCommand, \DeclareExpandableDocumentCommand, \DeclareRobustCommand, etc.
      • content of the original command (i.e. e.g. in the original \newcommand{\cmd}{ <old content> })
    4. how the redefined command is defined?

      • \renewcommand, \patchcmd, \xpatchcmd, \RenewDocumentCommand, \RenewExpandableDocumentCommand, \renewrobustcmd, etc.
      • content of the redefined command (i.e. e.g. \renewcommand{\cmd}{ <new content> })
    5. whether the old command is renamed with e.g. \let\oldcmd=\cmd

      • how it is renamed?
    6. how the old command was being made use of?

  2. Redefining a TeX Primitive (e.g. \neq or \par)...

    1. Is it always a no-go, or does it depend on the specific primitive or "type" thereof?
    2. Same questions as above

What led me to ask this question, which has popped into my head plenty of times before, was my intention to redefine the TeX primitive \d, whose original definition is (TeXbook p. 356):

\def\d#1{\oalign{#1\crcr\hidewidth.\hidewidth}}

I wanted to redefine it into an all-purpose derivative operator, that e.g. becomes \frac{\mathop{}\!\mathrm{d}f}{\mathop{}\!\mathrm{d}x} when given two arguments, \mathop{}\!\mathrm{d} when given one, uses \partial when starred, higher derivatives if given an optional argument, etc. How to achieve this particular behavior is, of course, not this question.


Related resources

steve
  • 2,154
  • 4
    This seems like the ultimate question on life, the universe and everything. You generally don't want to redefine commands. Sometimes it becomes necessary to patch one and there are various ways. You definitely don't want to redefine \d. – egreg Aug 05 '20 at 21:43
  • 1
    By the way, \d and \neq are not primitives. – egreg Aug 05 '20 at 21:46
  • When redefining commands while restricting the definition to a local scope, it may be necessary to ensure that the redefined variant is not used in so-called moving arguments that might wind up in places outside that scope. E.g., arguments of \section which also wind up in page-headers and/or the table of contents... – Ulrich Diez Aug 05 '20 at 22:50
  • 1
    As a rule of thumb: If you don't wish to change the behavior of the command in places, e.g., within formats/packages/classes/whatsoever input-files, where the command was/is used by other programmers but wish different behavior only with stuff written by you, then don't redefine the command but define another command and use that instead. Do very well document the difference in behavior of the two commands. – Ulrich Diez Aug 05 '20 at 22:57
  • 1
    In case a command is intended as a place-holder for action which depends on forking-mechanisms, redefining it should be safe - in the LaTeX-kernel the macro \next is used a lot in this way. Temporarily redefining scratch-macros like \mytempa , e.g. used for defining macros from arguments for further examination in terms of \ifx-comparison usually is safe as well. But you may need to make sure that no output routine using the same macros can be triggered while your redefinition is in effect/is needed as otherwise the output-routine might erroneously use and/or alter your redefinition... – Ulrich Diez Aug 05 '20 at 23:07
  • @egreg Thanks for the correction, I edited the question accordingly. And yes, I agree, it does seem (and feel) like the "ultimate question on life, the universe and everything". But I believe even those questions ought to be asked, if not for an answer then for the unsuspecting LaTeX-er (yours truly) to realize its depth... and to further my understanding of what's too broad / off-topic for this website. – steve Aug 05 '20 at 23:37
  • The package letltxmacro might be of interest to you. – Ulrich Diez Aug 05 '20 at 23:38
  • @UlrichDiez Thank you for your comments and correction! I know, I know, I shouldn't try to redefine commands if I don't want to actually redefine them, but instead merely use their names. But I was just tempted, and just wanted to ask the question out of curiosity, too, for what's behind the "danger zone" – steve Aug 05 '20 at 23:40
  • 1
    I like the question. It is an opportunity to point out possible pitfalls (moving arguments, output-routine, ...) related to (temporarily) redefining commands. ;-) – Ulrich Diez Aug 05 '20 at 23:41
  • @UlrichDiez Yes, letltxmacro looks great! (I must admit I haven't had the opportunity to use it yet.) It doesn't make packages that called for \cmd call for \oldcmd if I do \LetLtxMacro{\oldcmd}{\cmd} though, right? I mean something like that would probably solve everything anyways, but I wouldn't know if something like that existed – steve Aug 05 '20 at 23:51
  • letltxmacro does not change what packages do. LaTeX-macros with optional arguments defined in terms of \newcommand and LaTeX-macros defined in terms of \DeclareRobustCommand under the hood actually are not a single macro but are macro-mechanisms consisting of several macros, one of them having the name given in \newcommand/\DeclareRobustCommand's argument, the others having the same name but with a leading backslash or a trailing space. letltxmacro takes care of copying/renaming/patching all macros belonging to such a mechanism. – Ulrich Diez Aug 05 '20 at 23:58
  • Assume a package defining \macro as a macro where some argument is delimited by the token \cmd. Having some sort of mechanism for replacing all instances of the token \cmd by the token \oldcmd might cause \macro to deliver error-messages usage of \macro does not match its definition. – Ulrich Diez Aug 06 '20 at 00:09

0 Answers0