Renew command not working

I have the following command written in my preamble

\renewcommand{\d}{\;\ensuremath\mathrm{d}}


but get the error

LaTeX Warning: Command \d invalid in math mode on input line 11.


This only happens on a couple of my documents, and I can't figure out why. This also happens when I redefine \a, \b, \c to other things (like vectors).

• Without a MWE it's difficult to give any answer. It's most likely that the commands are refined later by some package. At least \c was predefined by tex for another purpose and possibly redefined by packages like fontspec. It is generally a very bad idea to define a global single letter macro in latex (or any programming language for that matter) – Yan Zhou Jun 23 '16 at 0:55
• @YanZhou It is to be hoped that no package is redefining these macros! – cfr Jun 23 '16 at 0:59
• @YanZhou aha! it was another package. Turns out hyperref redefines them. – user369210 Jun 23 '16 at 1:17
• @user321210 No. hyperref is absolutely 100% not doing that. Please see my answer below. You are overwriting basic macros at your peril. hyperref is not overwriting anything. If you were lucky, it would overwrite your redefinitions. Unfortunately, that is not happening - at least, not reliably, anyway. – cfr Jun 23 '16 at 2:09
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – user36296 Aug 1 '18 at 10:27

There are two issues, really. The first is that it is an extremely bad idea to redefine basic TeX and LaTeX macros unless you absolutely know what you are doing.

The second is that \; is not valid outside maths mode.

Perhaps you want something like

\documentclass{article}
\begin{document}
\newcommand{\drm}{\ensuremath{\;\mathrm{d}}}
\drm
\end{document}


EDIT

This is a response to the OP's comment

@YanZhou aha! it was another package. Turns out hyperref redefines them.

NO! It is not. hyperref is not doing this at all. Describing it this way is just wrong. I cannot emphasise this strongly enough: hyperref is not redefining these macros.

Redefining \d to be \mathrm{d} or making \a or \c into macros for vectors is not at all like what a package such as hyperref is doing.

What we see in hyperref's puenc.def, for example, are lines such as

\DeclareTextCommand{\d}{PU}[1]{#1\83\043}% U+0323


and

\DeclareTextCompositeCommand{\d}{PU}{\@empty}{\textdotbelow}%


These are telling TeX what \d does when the PU encoding is being used. This is fine because this macro is designed to be encoding-dependent. This is what you are meant to do if you are writing a new encoding which should support \d.

hyperref is not redefining \d. This is (or should be!) a new definition because the PU encoding doesn't even exist until hyperref sets it up.

What hyperref is doing, rather, is defining a new output encoding. Output encodings have to define certain things and may define more. hyperref is not overwriting existing definitions because the definitions it gives for \c and \d are part of the definition of the new encoding and that encodings definitions are isolated from the definitions of the same macros for other encodings.

That is, what hyperref is doing is fine. What you are trying to do is a Very Bad Idea. It is a Very Bad Idea precisely because these kinds of macros play a fundamental role in the typesetting process and redefining them will come back to bite you sooner or later.

• You mean "not valid outside math mode"? – Yan Zhou Jun 23 '16 at 0:56
• @Au101 I knew somebody would know somebody who ... That's why I thought I'd better qualify it ;). But, as you say, I don't think it applies here. I think the OP knows just enough physics to start the chain reaction but not quite enough to see why that might not be a great idea. – cfr Jun 23 '16 at 3:00
• @jon there is absolutely nothing wrong with using single letter names for personal shorthands. lots of people use \R for \mathbb{R} etc. The only issue is that you should not redefine the internals of the system that you are using. redefining \c is bad for the same reason that redefining \box is bad, the fact that c is one letter is not really relevant. – David Carlisle Jun 24 '16 at 8:18
• But (and I wish now I didn't delete the earlier comment) perhaps we can agree to disagree at this point. – jon Jun 25 '16 at 1:23