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.
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:where the original command is defined?
- defined in a package I'm loading, in the
documentclassI'm using, in the TeX engine I'm using, my own preamble, etc.
- defined in a package I'm loading, in the
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
\bgroupand\egroupthat does / doesn't make use of some / any functionality of the affected package(s).
- before or after
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> })
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> })
whether the old command is renamed with e.g.
\let\oldcmd=\cmd- how it is renamed?
how the old command was being made use of?
Redefining a TeX Primitive (
e.g.)...\neqor\par- Is it always a no-go, or does it depend on the specific primitive or "type" thereof?
- 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
\d. – egreg Aug 05 '20 at 21:43\dand\neqare not primitives. – egreg Aug 05 '20 at 21:46\sectionwhich also wind up in page-headers and/or the table of contents... – Ulrich Diez Aug 05 '20 at 22:50\nextis 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:07letltxmacrolooks great! (I must admit I haven't had the opportunity to use it yet.) It doesn't make packages that called for\cmdcall for\oldcmdif 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\newcommandand LaTeX-macros defined in terms of\DeclareRobustCommandunder 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\macroas a macro where some argument is delimited by the token\cmd. Having some sort of mechanism for replacing all instances of the token\cmdby the token\oldcmdmight cause\macroto deliver error-messagesusage of \macro does not match its definition. – Ulrich Diez Aug 06 '20 at 00:09