# Redefining Commands: Safety and Best Practices

TeX - LaTeX Asked on August 13, 2020

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, protecteddef, 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. letoldcmd=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):

defd#1{oalign{#1crcrhidewidth.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

## Related Questions

### Bra-ket notation typefaces

1  Asked on May 13, 2021 by eigenaus

### Problem MiKTeX 21.1 and TexStudio 3.0.4 – “Sorry, but pdflatex.exe did not succeed.” error

1  Asked on May 13, 2021 by rocco-gentile

### move legend in tikzpicture

1  Asked on May 13, 2021

### Pause in second Itemize doesn’t work like first one

1  Asked on May 13, 2021

### Removing right margin in Verse environment

1  Asked on May 13, 2021 by raphael-soares

### Forcing page number on a page with large figure

3  Asked on May 13, 2021 by bissi

### Drawing a Venn diagram with three circles in a certain style

3  Asked on May 13, 2021

### How to draw curve of $f(x)=dfrac{2x^2-4x+5}{x^2+1}$ in domain $-8:9$ with tikz?

1  Asked on May 13, 2021 by houcine

### Why are there on certain pages no headings?

2  Asked on May 13, 2021

### Macro: Storage and usage of abstract content does not work in cls

1  Asked on May 13, 2021

### Table fills most of the page, but there is space that can be used

0  Asked on May 13, 2021

### What are refhg, refmark, refhgb?

0  Asked on May 13, 2021 by dfrankow

### Typesetting Preparsed Verbatim Text/Code

1  Asked on May 12, 2021 by kelly-smith

### How to use Xelatex with vim-latexsuite?

1  Asked on May 12, 2021

### How to make “outline” frame in Beamer?

2  Asked on May 12, 2021

### Use of subequations and theorem environment

2  Asked on May 12, 2021

### Creating an ellipse-rectangle shaped node in tikz

2  Asked on May 12, 2021 by abdallah

### Automatically connecting adjacent tcolorboxes

1  Asked on May 12, 2021

### Left margin set different for every /section /subsection

0  Asked on May 12, 2021 by lvaro-ferrero

### Extra vertical space between two paragraphs added by figure

1  Asked on May 12, 2021 by euklidalexandria

### Ask a Question

Get help from others!