I am returning to LaTeX after a bit of a hiatus. Currently running Mac OS High Sierra (10.13.16) and TexShop 4.26. In the past, I have used the following definition command
% In pre-amble
\def\m3{m$^3$}
% In text
Blah...blah...blah..$\m3$
But I’m now getting the error: “Use of \m doesn’t match its definition.” LaTeX is not picking up on the “3” in “\m3”, for some reason.
I have also tried \newcommand in place of \def, but I’m not getting that to work either. At one point, this felt like a very basic LaTeX tool. What am I doing wrong?
Here are more details of my setup:
\documentclass[]{article}
\usepackage{amsmath}
\usepackage{natbib}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{url}
\usepackage{setspace}
%~~~~~~~~~~~~~~~~~~~~ PREAMBLE ~~~~~~~~~~~~~~~~~~~~~
% definitions
%--- units ---
\def\m3{m$^3$}
\def\m3yr{\textrm{m}$^3$\textrm{yr}$^{-1}$}
% ~~~~~~~~~~~~~~~~~~~~ MAIN TEXT ~~~~~~~~~~~~~~~~~~~~~
Over 26 million $\m3$....
Even though I can’t get this to work in my manuscript, the following stand-alone code does work:
\documentclass[]{article}
\def\m3{m$^3$}
\begin{document}
\m3
\end{document}
and produces the m^3 that I’m looking for, as shown here
So I suppose that I’m still unsure why it works in this stand-alone version and not in mine, though I’m hearing from a few of you that, either way, it’s not a practice in alignment with the form and function of definitions.
I would appreciate if those who flag this as duplicate link to the duplicated page because I did some searching before posting and didn’t seem to find what I was looking for. Seeing what is considered duplicate could help my learning. Thank you.


\miiior something like that. It's also best to avoid using\defsince it overwrites any existing definition. Use\newcommandinstead, which checks for conflicts. For future questions it's best not to post code fragments, but put the relevant code into a compilable minimal document. – Alan Munn Jul 15 '19 at 16:54$...$inside math mode (i.e., you're doing$\m3$), but this won't work like that because you can't embed math inside math like this. If you want a command that can be used inside both text and math, then you need to use\ensuremathinstead:\def\m3{\ensuremath{\mathrm{m}^3}}And as @siracusa said in their answer, you can't have both an\m3and an\m3yrbecause the definition of\m3yrwill override the definition of\m3(or vice versa if in the other order.) – Alan Munn Jul 17 '19 at 21:32