1

This question already has an answer here:

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

enter image description 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.

marked as duplicate by Alan Munn, Mensch, Stefan Pinnow, barbara beeton, Marcel Krüger Jul 15 at 17:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Welcome to TeX.se. Macro names can only contain letters. So use \miii or something like that. It's also best to avoid using \def since it overwrites any existing definition. Use \newcommand instead, 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 at 16:54
  • I suppose this is a new change in latex? It used to work for me to use “m3” etc. I used this and similar kinds of def for my dissertation just fine. Would rather use “m3" than “metercubed”. Is there any way to use “m3”? – RMueller Jul 16 at 1:32
  • No this constraint has been there since the beginning of TeX. – Alan Munn Jul 16 at 1:45
  • See my revised post @AlanMunn. I somehow get it to work in a new document and it worked when I wrote my dissertation. I obviously didn’t learn a good standard of practice, but...it worked and can still work. – RMueller Jul 17 at 21:24
  • The difference between your MWE and the other code you posted is that you're trying to embed a command defined using $...$ 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 \ensuremath instead: \def\m3{\ensuremath{\mathrm{m}^3}} And as @siracusa said in their answer, you can't have both an \m3 and an \m3yr because the definition of \m3yr will override the definition of \m3 (or vice versa if in the other order.) – Alan Munn Jul 17 at 21:32
1

Your definition \def\m3 is actually a definition of the macro \m that assumes the character 3 follows immediately (it's part of the parameter text, in terms of TeX). It's not part of the macro name.

The same is true for the definition \def\m3yr. Here you override the definition of \m and give a different parameter text 3yr. As a consequence, each subsequent call of \m must be followed by the characters 3yr.

In your document you then try to call the first defintion \m3 without the following yr, which makes TeX report that the use of \m doesn't match its (second) definition.


EDIT: Either of your definitions does work (this is not a recommended syntax, though, you should define a command \miii or one that takes a parameter instead). But as soon as you use both definitions in your code, this concept will fail.

If you really have to use this syntax, you have to define a single \m command that looks ahead the following characters using \@ifnextchar:

\documentclass[a4paper,12pt]{article}

\makeatletter
\newcommand\m{\@ifnextchar{3}{\m@iii@}{m}}
\def\m@iii@ 3{\@ifnextchar{y}{\m@iii@y}{\m@iii}}
\def\m@iii@y y{\@ifnextchar{r}{\m@iii@yr}{\m@iii y}}
\def\m@iii@yr r{\textrm{m}$^3$\textrm{yr}$^{-1}$}
\def\m@iii{m$^3$}
\makeatother

\begin{document}
\m, \m3, \m3y, \m3yr, \m3{yr}
\end{document}

outputs

enter image description here

  • So how can I change the definition to that “m3” defines m^3? This worked just fine for me 5 years ago. – RMueller Jul 16 at 1:33
  • Please see the updated answer – siracusa Jul 16 at 15:30
  • Thanks for the updated post. This is definitely NOT what I did 5 years ago! ;-) Great insight, though. Thank you for sharing. – RMueller Jul 17 at 21:22
  • Thank you! I’ve tried the above method and it works. I’m still seeking a more simple solution, but I also appreciate having a solution that I can use. – RMueller Jul 17 at 21:43
  • As mentioned in the answer, you can't have the same macro with different parameter texts. This just isn't supported by the TeX engine. If it worked for you years ago, you either just had one definition in your code or you always only used the last of multiple definitions. – siracusa Jul 17 at 21:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.